0001-formdefs-include-user-backoffice-submission-roles-in.patch
tests/test_formdef_import.py | ||
---|---|---|
350 | 350 |
assert fd2.geolocations == formdef.geolocations |
351 | 351 |
fd3 = assert_json_import_export_works(formdef) |
352 | 352 |
assert fd3.geolocations == formdef.geolocations |
353 | ||
354 |
def test_user_roles(): |
|
355 |
Role.wipe() |
|
356 | ||
357 |
role = Role(name='blah') |
|
358 |
role.store() |
|
359 | ||
360 |
formdef = FormDef() |
|
361 |
formdef.name = 'foo' |
|
362 |
formdef.fields = [] |
|
363 |
formdef.roles = ['logged-users', role.id] |
|
364 |
fd2 = assert_xml_import_export_works(formdef, include_id=True) |
|
365 |
assert fd2.roles == formdef.roles |
|
366 | ||
367 |
formdef_xml = formdef.export_to_xml(include_id=True) |
|
368 |
formdef_xml_no_id = formdef.export_to_xml(include_id=False) |
|
369 |
role.remove_self() |
|
370 |
fd2 = FormDef.import_from_xml_tree(formdef_xml, include_id=True) |
|
371 |
assert fd2.roles == ['logged-users'] |
|
372 |
fd2 = FormDef.import_from_xml_tree(formdef_xml, include_id=False) |
|
373 |
assert fd2.roles == ['logged-users'] |
|
374 | ||
375 |
fd2 = FormDef.import_from_xml_tree(formdef_xml_no_id, include_id=True) |
|
376 |
assert fd2.roles == ['logged-users'] |
|
377 |
fd2 = FormDef.import_from_xml_tree(formdef_xml_no_id, include_id=False) |
|
378 |
assert fd2.roles == ['logged-users'] |
|
379 | ||
380 |
def test_backoffice_submission_roles(): |
|
381 |
Role.wipe() |
|
382 | ||
383 |
role = Role(name='blah') |
|
384 |
role.store() |
|
385 | ||
386 |
formdef = FormDef() |
|
387 |
formdef.name = 'foo' |
|
388 |
formdef.fields = [] |
|
389 |
formdef.backoffice_submission_roles = [role.id] |
|
390 |
fd2 = assert_xml_import_export_works(formdef, include_id=True) |
|
391 |
assert fd2.backoffice_submission_roles == formdef.backoffice_submission_roles |
wcs/formdef.py | ||
---|---|---|
693 | 693 |
for field in self.fields or []: |
694 | 694 |
fields.append(field.export_to_xml(charset=charset, include_id=include_id)) |
695 | 695 | |
696 |
roles_elements = [ |
|
697 |
('roles', 'user-roles'), |
|
698 |
('backoffice_submission_roles', 'backoffice-submission-roles') |
|
699 |
] |
|
700 |
for attr_name, node_name in roles_elements: |
|
701 |
if not getattr(self, attr_name, None): |
|
702 |
continue |
|
703 |
roles = ET.SubElement(root, node_name) |
|
704 |
for role_id in getattr(self, attr_name): |
|
705 |
if role_id is None: |
|
706 |
continue |
|
707 |
role_id = str(role_id) |
|
708 |
if role_id.startswith('_') or role_id == 'logged-users': |
|
709 |
role = unicode(role_id, charset) |
|
710 |
else: |
|
711 |
try: |
|
712 |
role = unicode(Role.get(role_id).name, charset) |
|
713 |
except KeyError: |
|
714 |
role = unicode(role_id, charset) |
|
715 |
sub = ET.SubElement(roles, 'role') |
|
716 |
if include_id: |
|
717 |
sub.attrib['role_id'] = role_id |
|
718 |
sub.text = role |
|
719 | ||
696 | 720 |
if self.workflow_roles: |
697 | 721 |
roles = ET.SubElement(root, 'roles') |
698 | 722 |
for role_key, role_id in self.workflow_roles.items(): |
... | ... | |
856 | 880 |
formdef.workflow_id = w.id |
857 | 881 |
break |
858 | 882 | |
883 |
roles_elements = [ |
|
884 |
('roles', 'user-roles'), |
|
885 |
('backoffice_submission_roles', 'backoffice-submission-roles') |
|
886 |
] |
|
887 |
for attr_name, node_name in roles_elements: |
|
888 |
if tree.find(node_name) is None: |
|
889 |
continue |
|
890 |
roles_node = tree.find(node_name) |
|
891 |
roles = [] |
|
892 |
setattr(formdef, attr_name, roles) |
|
893 |
for child in roles_node.getchildren(): |
|
894 |
role_id = None |
|
895 |
value = child.text.encode(charset) |
|
896 |
if value.startswith('_') or value == 'logged-users': |
|
897 |
role_id = value |
|
898 |
elif include_id: |
|
899 |
role_id = child.attrib.get('role_id') |
|
900 |
if role_id and not Role.has_key(role_id): |
|
901 |
role_id = None |
|
902 | ||
903 |
if not role_id: |
|
904 |
for role in Role.select(ignore_errors=True): |
|
905 |
if role.name == value: |
|
906 |
role_id = role.id |
|
907 |
break |
|
908 |
if role_id: |
|
909 |
roles.append(role_id) |
|
910 | ||
859 | 911 |
if tree.find('roles') is not None: |
860 | 912 |
roles_node = tree.find('roles') |
861 | 913 |
formdef.workflow_roles = {} |
862 |
- |