mdel_ddpacs: use custom converter to redefine CiviliteType (#39818)

This commit is contained in:
Benjamin Dauvergne 2020-02-13 13:49:46 +01:00
parent fabe3b9d98
commit fab1bc4c17
3 changed files with 42 additions and 9 deletions

View File

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

View File

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

View File

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