Projet

Général

Profil

0002-workflows-add-more-attributes-to-the-XML-export.patch

Frédéric Péters, 25 avril 2014 15:51

Télécharger (5,6 ko)

Voir les différences:

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(-)
tests/test_workflow_import.py
148 148
    assert wf.possible_status[0].items[0].model_file.base_filename == 'bar'
149 149
    wf2 = assert_import_export_works(wf)
150 150
    assert wf2.possible_status[0].items[0].model_file.base_filename == 'bar'
151

  
152

  
153
def test_export_roles():
154
    wf = Workflow(name='roles')
155
    wf.roles = {'foo': 'Bar'}
156
    wf2 = assert_import_export_works(wf)
157
    assert wf2.roles == wf.roles
wcs/workflows.py
80 80
class Workflow(StorableObject):
81 81
    _names = 'workflows'
82 82
    name = None
83
    details = None
84 83
    possible_status = None
85 84
    roles = None
86 85

  
......
204 203
            form.store()
205 204
        StorableObject.remove_self(self)
206 205

  
207
    def export_to_xml(self):
206
    def export_to_xml(self, include_id=False):
208 207
        charset = get_publisher().site_charset
209 208
        root = ET.Element('workflow')
209
        if include_id and self.id and not str(self.id).startswith('_'):
210
            root.attrib['id'] = self.id
210 211
        ET.SubElement(root, 'name').text = unicode(self.name, charset)
212

  
213
        roles_node = ET.SubElement(root, 'roles')
214
        if self.roles:
215
            for role_id, role_label in self.roles.items():
216
                role_node = ET.SubElement(roles_node, 'role')
217
                role_node.attrib['id'] = role_id
218
                role_node.text = unicode(role_label, charset)
219

  
220
        if self.last_modification_time:
221
            elem = ET.SubElement(root, 'last_modification')
222
            elem.text = time.strftime('%Y-%m-%d %H:%M:%S', self.last_modification_time)
223
            if include_id:
224
                elem.attrib['user_id'] = str(self.last_modification_user_id)
225

  
211 226
        possible_status = ET.SubElement(root, 'possible_status')
212 227
        for status in self.possible_status:
213 228
            possible_status.append(status.export_to_xml(charset=charset))
214 229
        return root
215 230

  
216
    def import_from_xml(cls, fd):
231
    def import_from_xml(cls, fd, include_id=False):
217 232
        try:
218 233
            tree = ET.parse(fd)
219 234
        except:
220 235
            raise ValueError()
221
        return cls.import_from_xml_tree(tree)
236
        return cls.import_from_xml_tree(tree, include_id=include_id)
222 237
    import_from_xml = classmethod(import_from_xml)
223 238

  
224
    def import_from_xml_tree(cls, tree):
239
    def import_from_xml_tree(cls, tree, include_id=False):
225 240
        charset = get_publisher().site_charset
226 241
        workflow = cls()
227 242
        if tree.find('name') is None or not tree.find('name').text:
228 243
            raise ValueError()
229 244

  
245
        if include_id and tree.attrib.get('id'):
246
            workflow.id = tree.attrib.get('id')
247

  
230 248
        workflow.name = tree.find('name').text.encode(charset)
249

  
250
        if tree.find('roles') is not None:
251
            workflow.roles = {}
252
            for role_node in tree.findall('roles/role'):
253
                workflow.roles[role_node.attrib['id']] = role_node.text.encode(charset)
254

  
255
        if tree.find('last_modification') is not None:
256
            node = tree.find('last_modification')
257
            self.last_modification_time = time.strptime(node.text, '%Y-%m-%d %H:%M:%S')
258
            if include_id and elem.attrib.get('user_id'):
259
                self.last_modification_user_id = elem.attrib.get('user_id')
260

  
231 261
        workflow.possible_status = []
232 262
        for status in tree.find('possible_status'):
233 263
            status_o = WorkflowStatus()
......
554 584
        status = ET.Element('status')
555 585
        ET.SubElement(status, 'id').text = unicode(self.id, charset)
556 586
        ET.SubElement(status, 'name').text = unicode(self.name, charset)
587
        ET.SubElement(status, 'colour').text = unicode(self.colour, charset)
588

  
589
        if self.forced_endpoint:
590
            ET.SubElement(status, 'forced_endpoint').text = 'true'
591

  
592
        visibility_node = ET.SubElement(status, 'visibility')
593
        for role in self.visibility or []:
594
            ET.SubElement(visibility_node, 'role').text = str(role)
595

  
557 596
        items = ET.SubElement(status, 'items')
558 597
        for item in self.items:
559 598
            items.append(item.export_to_xml(charset=charset))
560 599
        return status
561 600

  
562 601
    def init_with_xml(self, elem, charset, include_id=False):
563
        self.id = elem.find('id').text.encode(charset)    
564
        self.name = elem.find('name').text.encode(charset)    
602
        self.id = elem.find('id').text.encode(charset)
603
        self.name = elem.find('name').text.encode(charset)
604
        if elem.find('colour') is not None:
605
            self.colour = elem.find('colour').text.encode(charset)
606
        if elem.find('forced_endpoint') is not None:
607
            self.forced_endpoint = (elem.find('forced_endpoint').text == 'true')
608

  
609
        self.visibility = []
610
        for visibility_role in elem.findall('visibility/role'):
611
            self.visibility.append(visibility_role.text)
612

  
565 613
        self.items = []
566 614
        for item in elem.find('items'):
567 615
            item_type = item.attrib['type']
568
-