From 2d4e3a719d57d0dd0e52333409e5a2ef9c038c46 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Thu, 13 Feb 2020 13:49:46 +0100 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(-) diff --git a/passerelle/apps/mdel_ddpacs/abstract.py b/passerelle/apps/mdel_ddpacs/abstract.py index 65a4df17..2e8943b6 100644 --- a/passerelle/apps/mdel_ddpacs/abstract.py +++ b/passerelle/apps/mdel_ddpacs/abstract.py @@ -30,8 +30,6 @@ from django.http import HttpResponse from django.utils.translation import ugettext_lazy as _ from django.utils import six, functional -import xmlschema - import jsonfield from passerelle.base.models import BaseResource, SkipJob @@ -90,6 +88,7 @@ class Resource(BaseResource): doc_type = 'doc_type CHANGEME' zip_manifest = 'mdel/zip/manifest.json' code_insee_id = 'CODE_INSEE' + xmlschema_class = xml.JSONSchemaFromXMLSchema class Meta: abstract = True @@ -107,11 +106,11 @@ class Resource(BaseResource): base_dir = os.path.dirname(inspect.getfile(cls)) path = os.path.join(base_dir, cls.xsd_path) assert os.path.exists(path) - return xmlschema.XMLSchema(path, converter=xmlschema.UnorderedConverter) + return cls.xmlschema_class(path, cls.xsd_root_element) @classmethod def get_doc_json_schema(cls): - return xml.JSONSchemaFromXMLSchema(cls.get_doc_xml_schema(), cls.xsd_root_element).json_schema + return cls.get_doc_xml_schema().json_schema @classmethod def get_create_schema(cls): @@ -296,9 +295,7 @@ class Demand(models.Model): @property def document(self): xml_schema = self.resource.get_doc_xml_schema() - return ET.tostring( - xml_schema.elements[self.resource.xsd_root_element].encode( - self.data[self.resource.xsd_root_element], converter=xmlschema.UnorderedConverter)) + return ET.tostring(xml_schema.encode(self.data)) @property def status_url(self): diff --git a/passerelle/apps/mdel_ddpacs/models.py b/passerelle/apps/mdel_ddpacs/models.py index b00752cb..809fce2f 100644 --- a/passerelle/apps/mdel_ddpacs/models.py +++ b/passerelle/apps/mdel_ddpacs/models.py @@ -21,15 +21,48 @@ from django.db import models from django.utils.translation import ugettext_lazy as _ from passerelle.utils.api import endpoint +from passerelle.utils.xml import JSONSchemaFromXMLSchema + +import xmlschema from . import abstract +class DDPACSSchema(JSONSchemaFromXMLSchema): + type_map = { + 'CiviliteType': 'civilite', + } + civilite_map = { + 'Monsieur': 'M', + 'Madame': 'MME', + } + + @classmethod + def schema_civilite(cls): + return { + 'type': 'string', + 'enum': ['Madame', 'Monsieur'], + } + + def encode_civilite(self, obj): + try: + return self.civilite_map[obj] + except KeyError: + raise xmlschema.XMLSchemaValidationError(self, obj, reason='civilite invalide') + + def decode_civilite(self, data): + for key, value in self.civilite_map.items(): + if data.text == value: + return xmlschema.ElementData(tag=data.tag, text=key, content=data.content, attributes=data.attributes) + raise xmlschema.XMLSchemaValidationError(self, data, reason='civilite invalide %s') + + class Resource(abstract.Resource): category = _('Civil Status Connectors') xsd_root_element = 'PACS' flow_type = 'depotDossierPACS' doc_type = 'flux-pacs' + xmlschema_class = DDPACSSchema class Meta: verbose_name = _('PACS request (MDEL DDPACS)') diff --git a/tests/test_mdel_ddpacs.py b/tests/test_mdel_ddpacs.py index 7b712e50..9f2f9eda 100644 --- a/tests/test_mdel_ddpacs.py +++ b/tests/test_mdel_ddpacs.py @@ -24,6 +24,8 @@ import os import pytest import utils +import xmlschema + from passerelle.apps.mdel_ddpacs.models import Resource, Demand from passerelle.utils import json, sftp @@ -47,8 +49,9 @@ def resource(db): @pytest.fixture def ddpacs_payload(): - xmlschema = Resource.get_doc_xml_schema() - return json.flatten({'PACS': xmlschema.to_dict('tests/data/pacs-doc.xml')}) + schema = Resource.get_doc_xml_schema() + d = xmlschema.to_dict('tests/data/pacs-doc.xml', schema=schema.xml_schema) + return json.flatten({'PACS': d}) def test_create_demand(app, resource, ddpacs_payload, freezer, sftpserver, caplog): -- 2.24.0