workflows: add more attributes to the XML export

This commit is contained in:
Frédéric Péters 2014-04-25 15:50:25 +02:00
parent 01f7d953d6
commit 1bbb5bdb16
2 changed files with 62 additions and 7 deletions

View File

@ -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

View File

@ -80,7 +80,6 @@ def get_varnames(fields):
class Workflow(StorableObject):
_names = 'workflows'
name = None
details = None
possible_status = None
roles = None
@ -199,30 +198,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()
@ -549,14 +579,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']