agendas: fix import when missing exceptions or timeperiods (#42196)

This commit is contained in:
Lauréline Guérin 2020-04-28 16:49:04 +02:00
parent 746e027573
commit 4d5d9a7c3c
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
4 changed files with 25 additions and 3 deletions

View File

@ -749,8 +749,8 @@ class Desk(models.Model):
@classmethod
def import_json(cls, data):
timeperiods = data.pop('timeperiods')
exceptions = data.pop('exceptions')
timeperiods = data.pop('timeperiods', [])
exceptions = data.pop('exceptions', [])
instance, created = cls.objects.get_or_create(slug=data['slug'], agenda=data['agenda'], defaults=data)
if not created:
for k, v in data.items():

View File

@ -77,7 +77,6 @@ from .forms import (
)
from .utils import import_site
FUTURE_BOOKING_ERROR_MSG = _('This cannot be removed as there are bookings for a future date.')
@ -142,6 +141,9 @@ class AgendasImportView(FormView):
except AgendaImportError as exc:
form.add_error('agendas_json', u'%s' % exc)
return self.form_invalid(form)
except KeyError as exc:
form.add_error('agendas_json', _('Key "%s" is missing.') % exc.args[0])
return self.form_invalid(form)
if results.get('created') == 0 and results.get('updated') == 0:
messages.info(self.request, _('No agendas were found.'))

View File

@ -276,3 +276,14 @@ def test_import_export_virtual_agenda_with_included_agenda(app):
'This agenda does not have the same meeting types provided by the virtual agenda.'
in '%s' % excinfo.value
)
def test_import_export_desk_missing_fields(app, meetings_agenda):
output = get_output_of_command('export_site')
payload = json.loads(output)
del payload['agendas'][0]['desks'][0]['timeperiods']
del payload['agendas'][0]['desks'][0]['exceptions']
Agenda.objects.all().delete()
import_site(payload)
assert TimePeriod.objects.exists() is False
assert TimePeriodException.objects.exists() is False

View File

@ -2199,6 +2199,15 @@ def test_import_agenda(app, admin_user):
resp = resp.form.submit()
assert u'Missing "gé1" role' in resp.text
# missing field
del agenda_export_dict['agendas'][0]['kind']
agenda_export = json.dumps(agenda_export_dict).encode('utf-8')
resp = app.get('/manage/', status=200)
resp = resp.click('Import')
resp.form['agendas_json'] = Upload('export.json', agenda_export, 'application/json')
resp = resp.form.submit()
assert resp.context['form'].errors['agendas_json'] == ['Key "kind" is missing.']
def test_virtual_agenda_add(app, admin_user):
app = login(app)