0001-agenda-check-all-roles-before-importing-42135.patch
chrono/agendas/models.py | ||
---|---|---|
275 | 275 |
real_agendas = data.pop('real_agendas') |
276 | 276 |
for permission in ('view', 'edit'): |
277 | 277 |
if permissions.get(permission): |
278 |
try: |
|
279 |
data[permission + '_role'] = Group.objects.get(name=permissions[permission]) |
|
280 |
except Group.DoesNotExist: |
|
281 |
raise AgendaImportError(_('Missing "%s" role') % permissions[permission]) |
|
278 |
data[permission + '_role'] = Group.objects.get(name=permissions[permission]) |
|
282 | 279 |
resources_slug = data.pop('resources', []) |
283 | 280 |
resources_by_slug = {r.slug: r for r in Resource.objects.filter(slug__in=resources_slug)} |
284 | 281 |
for resource_slug in resources_slug: |
chrono/manager/utils.py | ||
---|---|---|
16 | 16 | |
17 | 17 |
import itertools |
18 | 18 | |
19 |
from django.contrib.auth.models import Group |
|
19 | 20 |
from django.db import transaction |
20 | 21 |
from django.db.models import Q |
21 | 22 | |
... | ... | |
39 | 40 |
Agenda.objects.all().delete() |
40 | 41 | |
41 | 42 |
results = {'created': 0, 'updated': 0} |
43 |
agendas = data.get('agendas', []) |
|
44 | ||
45 |
role_names = {name for data in agendas for _, name in data.get('permissions', {}).items() if name} |
|
46 |
existing_roles = Group.objects.filter(name__in=role_names) |
|
47 | ||
48 |
if existing_roles.count() != len(role_names): |
|
49 |
existing_roles_names = set(existing_roles.values_list('name', flat=True)) |
|
50 |
raise AgendaImportError('Missing roles: "%s"' % ', '.join(role_names - existing_roles_names)) |
|
51 | ||
42 | 52 |
with transaction.atomic(): |
43 |
for data in data.get('agendas', []):
|
|
53 |
for data in agendas:
|
|
44 | 54 |
created = Agenda.import_json(data, overwrite=overwrite) |
45 | 55 |
if created: |
46 | 56 |
results['created'] += 1 |
tests/test_import_export.py | ||
---|---|---|
169 | 169 | |
170 | 170 |
with pytest.raises(AgendaImportError) as excinfo: |
171 | 171 |
import_site(json.loads(output), overwrite=True) |
172 |
assert u'%s' % excinfo.value == u'Missing "gé1" role'
|
|
172 |
assert 'gé1' in str(excinfo.value) and 'gé2' in str(excinfo.value)
|
|
173 | 173 | |
174 | 174 |
group1 = Group(name=u'gé1') |
175 | 175 |
group1.save() |
176 | 176 |
with pytest.raises(AgendaImportError) as excinfo: |
177 | 177 |
import_site(json.loads(output), overwrite=True) |
178 |
assert u'%s' % excinfo.value == u'Missing "gé2" role'
|
|
178 |
assert u'%s' % excinfo.value == u'Missing roles: "gé2"'
|
|
179 | 179 | |
180 | 180 |
with tempfile.NamedTemporaryFile() as f: |
181 | 181 |
f.write(force_bytes(output)) |
182 | 182 |
f.flush() |
183 | 183 |
with pytest.raises(CommandError) as excinfo: |
184 | 184 |
call_command('import_site', f.name) |
185 |
assert u'%s' % excinfo.value == u'Missing "gé2" role'
|
|
185 |
assert u'%s' % excinfo.value == u'Missing roles: "gé2"'
|
|
186 | 186 | |
187 | 187 |
group2 = Group(name=u'gé2') |
188 | 188 |
group2.save() |
tests/test_manager.py | ||
---|---|---|
2937 | 2937 |
resp = resp.click('Import') |
2938 | 2938 |
resp.form['agendas_json'] = Upload('export.json', agenda_export, 'application/json') |
2939 | 2939 |
resp = resp.form.submit() |
2940 |
assert u'Missing "gé1" role' in resp.text |
|
2940 |
assert u'Missing roles: "gé1"' in resp.text |
|
2941 |
del agenda_export_dict['agendas'][0]['permissions']['view'] |
|
2941 | 2942 | |
2942 | 2943 |
# missing field |
2943 | 2944 |
del agenda_export_dict['agendas'][0]['kind'] |
2944 |
- |