From 0f6b9b3f1874ea4d3290752daa6ffa5744230f0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Fri, 25 Apr 2014 15:50:25 +0200 Subject: [PATCH 2/2] workflows: add more attributes to the XML export --- tests/test_workflow_import.py | 7 +++++ wcs/workflows.py | 62 ++++++++++++++++++++++++++++++++++++++----- 2 files changed, 62 insertions(+), 7 deletions(-) diff --git a/tests/test_workflow_import.py b/tests/test_workflow_import.py index 95740c3..6af02cf 100644 --- a/tests/test_workflow_import.py +++ b/tests/test_workflow_import.py @@ -148,3 +148,10 @@ def test_export_to_model_action(): assert wf.possible_status[0].items[0].model_file.base_filename == 'bar' wf2 = assert_import_export_works(wf) assert wf2.possible_status[0].items[0].model_file.base_filename == 'bar' + + +def test_export_roles(): + wf = Workflow(name='roles') + wf.roles = {'foo': 'Bar'} + wf2 = assert_import_export_works(wf) + assert wf2.roles == wf.roles diff --git a/wcs/workflows.py b/wcs/workflows.py index 9838695..35d04fc 100644 --- a/wcs/workflows.py +++ b/wcs/workflows.py @@ -80,7 +80,6 @@ def get_varnames(fields): class Workflow(StorableObject): _names = 'workflows' name = None - details = None possible_status = None roles = None @@ -204,30 +203,61 @@ class Workflow(StorableObject): form.store() StorableObject.remove_self(self) - def export_to_xml(self): + def export_to_xml(self, include_id=False): charset = get_publisher().site_charset root = ET.Element('workflow') + if include_id and self.id and not str(self.id).startswith('_'): + root.attrib['id'] = self.id ET.SubElement(root, 'name').text = unicode(self.name, charset) + + roles_node = ET.SubElement(root, 'roles') + if self.roles: + for role_id, role_label in self.roles.items(): + role_node = ET.SubElement(roles_node, 'role') + role_node.attrib['id'] = role_id + role_node.text = unicode(role_label, charset) + + if self.last_modification_time: + elem = ET.SubElement(root, 'last_modification') + elem.text = time.strftime('%Y-%m-%d %H:%M:%S', self.last_modification_time) + if include_id: + elem.attrib['user_id'] = str(self.last_modification_user_id) + possible_status = ET.SubElement(root, 'possible_status') for status in self.possible_status: possible_status.append(status.export_to_xml(charset=charset)) return root - def import_from_xml(cls, fd): + def import_from_xml(cls, fd, include_id=False): try: tree = ET.parse(fd) except: raise ValueError() - return cls.import_from_xml_tree(tree) + return cls.import_from_xml_tree(tree, include_id=include_id) import_from_xml = classmethod(import_from_xml) - def import_from_xml_tree(cls, tree): + def import_from_xml_tree(cls, tree, include_id=False): charset = get_publisher().site_charset workflow = cls() if tree.find('name') is None or not tree.find('name').text: raise ValueError() + if include_id and tree.attrib.get('id'): + workflow.id = tree.attrib.get('id') + workflow.name = tree.find('name').text.encode(charset) + + if tree.find('roles') is not None: + workflow.roles = {} + for role_node in tree.findall('roles/role'): + workflow.roles[role_node.attrib['id']] = role_node.text.encode(charset) + + if tree.find('last_modification') is not None: + node = tree.find('last_modification') + self.last_modification_time = time.strptime(node.text, '%Y-%m-%d %H:%M:%S') + if include_id and elem.attrib.get('user_id'): + self.last_modification_user_id = elem.attrib.get('user_id') + workflow.possible_status = [] for status in tree.find('possible_status'): status_o = WorkflowStatus() @@ -554,14 +584,32 @@ class WorkflowStatus: status = ET.Element('status') ET.SubElement(status, 'id').text = unicode(self.id, charset) ET.SubElement(status, 'name').text = unicode(self.name, charset) + ET.SubElement(status, 'colour').text = unicode(self.colour, charset) + + if self.forced_endpoint: + ET.SubElement(status, 'forced_endpoint').text = 'true' + + visibility_node = ET.SubElement(status, 'visibility') + for role in self.visibility or []: + ET.SubElement(visibility_node, 'role').text = str(role) + items = ET.SubElement(status, 'items') for item in self.items: items.append(item.export_to_xml(charset=charset)) return status def init_with_xml(self, elem, charset, include_id=False): - self.id = elem.find('id').text.encode(charset) - self.name = elem.find('name').text.encode(charset) + self.id = elem.find('id').text.encode(charset) + self.name = elem.find('name').text.encode(charset) + if elem.find('colour') is not None: + self.colour = elem.find('colour').text.encode(charset) + if elem.find('forced_endpoint') is not None: + self.forced_endpoint = (elem.find('forced_endpoint').text == 'true') + + self.visibility = [] + for visibility_role in elem.findall('visibility/role'): + self.visibility.append(visibility_role.text) + self.items = [] for item in elem.find('items'): item_type = item.attrib['type'] -- 2.0.0.rc0