agenda: check all roles before importing (#42135)
This commit is contained in:
parent
19d9d90e8e
commit
e5138ec49e
|
@ -276,10 +276,7 @@ class Agenda(models.Model):
|
|||
real_agendas = data.pop('real_agendas')
|
||||
for permission in ('view', 'edit'):
|
||||
if permissions.get(permission):
|
||||
try:
|
||||
data[permission + '_role'] = Group.objects.get(name=permissions[permission])
|
||||
except Group.DoesNotExist:
|
||||
raise AgendaImportError(_('Missing "%s" role') % permissions[permission])
|
||||
data[permission + '_role'] = Group.objects.get(name=permissions[permission])
|
||||
resources_slug = data.pop('resources', [])
|
||||
resources_by_slug = {r.slug: r for r in Resource.objects.filter(slug__in=resources_slug)}
|
||||
for resource_slug in resources_slug:
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
import itertools
|
||||
|
||||
from django.contrib.auth.models import Group
|
||||
from django.db import transaction
|
||||
from django.db.models import Q
|
||||
|
||||
|
@ -39,8 +40,17 @@ def import_site(data, if_empty=False, clean=False, overwrite=False):
|
|||
Agenda.objects.all().delete()
|
||||
|
||||
results = {'created': 0, 'updated': 0}
|
||||
agendas = data.get('agendas', [])
|
||||
|
||||
role_names = {name for data in agendas for _, name in data.get('permissions', {}).items() if name}
|
||||
existing_roles = Group.objects.filter(name__in=role_names)
|
||||
|
||||
if existing_roles.count() != len(role_names):
|
||||
existing_roles_names = set(existing_roles.values_list('name', flat=True))
|
||||
raise AgendaImportError('Missing roles: "%s"' % ', '.join(role_names - existing_roles_names))
|
||||
|
||||
with transaction.atomic():
|
||||
for data in data.get('agendas', []):
|
||||
for data in agendas:
|
||||
created = Agenda.import_json(data, overwrite=overwrite)
|
||||
if created:
|
||||
results['created'] += 1
|
||||
|
|
|
@ -169,20 +169,20 @@ def test_import_export_permissions(app, some_data, meetings_agenda):
|
|||
|
||||
with pytest.raises(AgendaImportError) as excinfo:
|
||||
import_site(json.loads(output), overwrite=True)
|
||||
assert u'%s' % excinfo.value == u'Missing "gé1" role'
|
||||
assert 'gé1' in str(excinfo.value) and 'gé2' in str(excinfo.value)
|
||||
|
||||
group1 = Group(name=u'gé1')
|
||||
group1.save()
|
||||
with pytest.raises(AgendaImportError) as excinfo:
|
||||
import_site(json.loads(output), overwrite=True)
|
||||
assert u'%s' % excinfo.value == u'Missing "gé2" role'
|
||||
assert u'%s' % excinfo.value == u'Missing roles: "gé2"'
|
||||
|
||||
with tempfile.NamedTemporaryFile() as f:
|
||||
f.write(force_bytes(output))
|
||||
f.flush()
|
||||
with pytest.raises(CommandError) as excinfo:
|
||||
call_command('import_site', f.name)
|
||||
assert u'%s' % excinfo.value == u'Missing "gé2" role'
|
||||
assert u'%s' % excinfo.value == u'Missing roles: "gé2"'
|
||||
|
||||
group2 = Group(name=u'gé2')
|
||||
group2.save()
|
||||
|
|
|
@ -2942,7 +2942,8 @@ def test_import_agenda(app, admin_user):
|
|||
resp = resp.click('Import')
|
||||
resp.form['agendas_json'] = Upload('export.json', agenda_export, 'application/json')
|
||||
resp = resp.form.submit()
|
||||
assert u'Missing "gé1" role' in resp.text
|
||||
assert u'Missing roles: "gé1"' in resp.text
|
||||
del agenda_export_dict['agendas'][0]['permissions']['view']
|
||||
|
||||
# missing field
|
||||
del agenda_export_dict['agendas'][0]['kind']
|
||||
|
|
Loading…
Reference in New Issue