From 384804ebb527059f76412c7166668884632753d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Fri, 2 Sep 2016 16:32:27 +0200 Subject: [PATCH] formdefs: include user & backoffice submission roles in XML export (#12626) --- tests/test_formdef_import.py | 39 +++++++++++++++++++++++++++++++++ wcs/formdef.py | 52 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) diff --git a/tests/test_formdef_import.py b/tests/test_formdef_import.py index 20a1171..5ecb046 100644 --- a/tests/test_formdef_import.py +++ b/tests/test_formdef_import.py @@ -350,3 +350,42 @@ def test_geolocations(): assert fd2.geolocations == formdef.geolocations fd3 = assert_json_import_export_works(formdef) assert fd3.geolocations == formdef.geolocations + +def test_user_roles(): + Role.wipe() + + role = Role(name='blah') + role.store() + + formdef = FormDef() + formdef.name = 'foo' + formdef.fields = [] + formdef.roles = ['logged-users', role.id] + fd2 = assert_xml_import_export_works(formdef, include_id=True) + assert fd2.roles == formdef.roles + + formdef_xml = formdef.export_to_xml(include_id=True) + formdef_xml_no_id = formdef.export_to_xml(include_id=False) + role.remove_self() + fd2 = FormDef.import_from_xml_tree(formdef_xml, include_id=True) + assert fd2.roles == ['logged-users'] + fd2 = FormDef.import_from_xml_tree(formdef_xml, include_id=False) + assert fd2.roles == ['logged-users'] + + fd2 = FormDef.import_from_xml_tree(formdef_xml_no_id, include_id=True) + assert fd2.roles == ['logged-users'] + fd2 = FormDef.import_from_xml_tree(formdef_xml_no_id, include_id=False) + assert fd2.roles == ['logged-users'] + +def test_backoffice_submission_roles(): + Role.wipe() + + role = Role(name='blah') + role.store() + + formdef = FormDef() + formdef.name = 'foo' + formdef.fields = [] + formdef.backoffice_submission_roles = [role.id] + fd2 = assert_xml_import_export_works(formdef, include_id=True) + assert fd2.backoffice_submission_roles == formdef.backoffice_submission_roles diff --git a/wcs/formdef.py b/wcs/formdef.py index 7214b4a..09c9f0a 100644 --- a/wcs/formdef.py +++ b/wcs/formdef.py @@ -693,6 +693,30 @@ class FormDef(StorableObject): for field in self.fields or []: fields.append(field.export_to_xml(charset=charset, include_id=include_id)) + roles_elements = [ + ('roles', 'user-roles'), + ('backoffice_submission_roles', 'backoffice-submission-roles') + ] + for attr_name, node_name in roles_elements: + if not getattr(self, attr_name, None): + continue + roles = ET.SubElement(root, node_name) + for role_id in getattr(self, attr_name): + if role_id is None: + continue + role_id = str(role_id) + if role_id.startswith('_') or role_id == 'logged-users': + role = unicode(role_id, charset) + else: + try: + role = unicode(Role.get(role_id).name, charset) + except KeyError: + role = unicode(role_id, charset) + sub = ET.SubElement(roles, 'role') + if include_id: + sub.attrib['role_id'] = role_id + sub.text = role + if self.workflow_roles: roles = ET.SubElement(root, 'roles') for role_key, role_id in self.workflow_roles.items(): @@ -856,6 +880,34 @@ class FormDef(StorableObject): formdef.workflow_id = w.id break + roles_elements = [ + ('roles', 'user-roles'), + ('backoffice_submission_roles', 'backoffice-submission-roles') + ] + for attr_name, node_name in roles_elements: + if tree.find(node_name) is None: + continue + roles_node = tree.find(node_name) + roles = [] + setattr(formdef, attr_name, roles) + for child in roles_node.getchildren(): + role_id = None + value = child.text.encode(charset) + if value.startswith('_') or value == 'logged-users': + role_id = value + elif include_id: + role_id = child.attrib.get('role_id') + if role_id and not Role.has_key(role_id): + role_id = None + + if not role_id: + for role in Role.select(ignore_errors=True): + if role.name == value: + role_id = role.id + break + if role_id: + roles.append(role_id) + if tree.find('roles') is not None: roles_node = tree.find('roles') formdef.workflow_roles = {} -- 2.9.3