Projet

Général

Profil

0002-mdel_ddpacs-use-custom-converter-to-redefine-Civilit.patch

Benjamin Dauvergne, 13 février 2020 13:49

Télécharger (4,65 ko)

Voir les différences:

Subject: [PATCH 2/2] mdel_ddpacs: use custom converter to redefine
 CiviliteType (#39818)

 passerelle/apps/mdel_ddpacs/abstract.py | 11 +++------
 passerelle/apps/mdel_ddpacs/models.py   | 33 +++++++++++++++++++++++++
 tests/test_mdel_ddpacs.py               |  7 ++++--
 3 files changed, 42 insertions(+), 9 deletions(-)
passerelle/apps/mdel_ddpacs/abstract.py
30 30
from django.utils.translation import ugettext_lazy as _
31 31
from django.utils import six, functional
32 32

  
33
import xmlschema
34

  
35 33
import jsonfield
36 34

  
37 35
from passerelle.base.models import BaseResource, SkipJob
......
90 88
    doc_type = 'doc_type CHANGEME'
91 89
    zip_manifest = 'mdel/zip/manifest.json'
92 90
    code_insee_id = 'CODE_INSEE'
91
    xmlschema_class = xml.JSONSchemaFromXMLSchema
93 92

  
94 93
    class Meta:
95 94
        abstract = True
......
107 106
        base_dir = os.path.dirname(inspect.getfile(cls))
108 107
        path = os.path.join(base_dir, cls.xsd_path)
109 108
        assert os.path.exists(path)
110
        return xmlschema.XMLSchema(path, converter=xmlschema.UnorderedConverter)
109
        return cls.xmlschema_class(path, cls.xsd_root_element)
111 110

  
112 111
    @classmethod
113 112
    def get_doc_json_schema(cls):
114
        return xml.JSONSchemaFromXMLSchema(cls.get_doc_xml_schema(), cls.xsd_root_element).json_schema
113
        return cls.get_doc_xml_schema().json_schema
115 114

  
116 115
    @classmethod
117 116
    def get_create_schema(cls):
......
296 295
    @property
297 296
    def document(self):
298 297
        xml_schema = self.resource.get_doc_xml_schema()
299
        return ET.tostring(
300
            xml_schema.elements[self.resource.xsd_root_element].encode(
301
                self.data[self.resource.xsd_root_element], converter=xmlschema.UnorderedConverter))
298
        return ET.tostring(xml_schema.encode(self.data))
302 299

  
303 300
    @property
304 301
    def status_url(self):
passerelle/apps/mdel_ddpacs/models.py
21 21
from django.utils.translation import ugettext_lazy as _
22 22

  
23 23
from passerelle.utils.api import endpoint
24
from passerelle.utils.xml import JSONSchemaFromXMLSchema
25

  
26
import xmlschema
24 27

  
25 28
from . import abstract
26 29

  
27 30

  
31
class DDPACSSchema(JSONSchemaFromXMLSchema):
32
    type_map = {
33
        'CiviliteType': 'civilite',
34
    }
35
    civilite_map = {
36
        'Monsieur': 'M',
37
        'Madame': 'MME',
38
    }
39

  
40
    @classmethod
41
    def schema_civilite(cls):
42
        return {
43
            'type': 'string',
44
            'enum': ['Madame', 'Monsieur'],
45
        }
46

  
47
    def encode_civilite(self, obj):
48
        try:
49
            return self.civilite_map[obj]
50
        except KeyError:
51
            raise xmlschema.XMLSchemaValidationError(self, obj, reason='civilite invalide')
52

  
53
    def decode_civilite(self, data):
54
        for key, value in self.civilite_map.items():
55
            if data.text == value:
56
                return xmlschema.ElementData(tag=data.tag, text=key, content=data.content, attributes=data.attributes)
57
        raise xmlschema.XMLSchemaValidationError(self, data, reason='civilite invalide %s')
58

  
59

  
28 60
class Resource(abstract.Resource):
29 61
    category = _('Civil Status Connectors')
30 62
    xsd_root_element = 'PACS'
31 63
    flow_type = 'depotDossierPACS'
32 64
    doc_type = 'flux-pacs'
65
    xmlschema_class = DDPACSSchema
33 66

  
34 67
    class Meta:
35 68
        verbose_name = _('PACS request (MDEL DDPACS)')
tests/test_mdel_ddpacs.py
24 24
import pytest
25 25
import utils
26 26

  
27
import xmlschema
28

  
27 29
from passerelle.apps.mdel_ddpacs.models import Resource, Demand
28 30

  
29 31
from passerelle.utils import json, sftp
......
47 49

  
48 50
@pytest.fixture
49 51
def ddpacs_payload():
50
    xmlschema = Resource.get_doc_xml_schema()
51
    return json.flatten({'PACS': xmlschema.to_dict('tests/data/pacs-doc.xml')})
52
    schema = Resource.get_doc_xml_schema()
53
    d = xmlschema.to_dict('tests/data/pacs-doc.xml', schema=schema.xml_schema)
54
    return json.flatten({'PACS': d})
52 55

  
53 56

  
54 57
def test_create_demand(app, resource, ddpacs_payload, freezer, sftpserver, caplog):
55
-