0003-mdel-adapt-filenames-and-routing-36870.patch
passerelle/apps/mdel/mdel.py | ||
---|---|---|
25 | 25 |
from django.core.files.storage import default_storage |
26 | 26 |
from django.core.files.base import ContentFile |
27 | 27 | |
28 |
from utils import ElementFactory |
|
28 |
from utils import ElementFactory, MELODIE
|
|
29 | 29 | |
30 | 30 |
from passerelle.utils.jsonresponse import APIError |
31 | 31 | |
... | ... | |
71 | 71 |
def __init__(self, *args, **kwargs): |
72 | 72 |
self.flow_type = args[0] |
73 | 73 |
self.num = args[1] |
74 |
self.doc = kwargs.get('doc') |
|
74 | 75 |
super(Common, self).__init__() |
75 | 76 | |
76 | 77 |
def save(self, folder): |
... | ... | |
134 | 135 |
ElementFactory('FichierFormulaire') |
135 | 136 |
]) |
136 | 137 | |
137 |
if not form_files: |
|
138 |
if not form_files and not self.doc:
|
|
138 | 139 |
form_files = [self.code + '-doc-.xml'] |
140 |
if not form_files and self.doc: |
|
141 |
form_files = [self.doc.filename] |
|
139 | 142 | |
140 | 143 |
for f in form_files: |
141 | 144 |
document.find('FichierFormulaire').append( |
... | ... | |
154 | 157 | |
155 | 158 |
def __init__( |
156 | 159 |
self, flow_type, num, zip_code, date=None, |
157 |
platform_id='1', form_files=None, attached_files=None): |
|
160 |
platform_id='1', form_files=None, attached_files=None, software=None, doc=None):
|
|
158 | 161 | |
159 |
super(Message, self).__init__(flow_type, num) |
|
162 |
super(Message, self).__init__(flow_type, num, doc=doc)
|
|
160 | 163 | |
161 | 164 |
etree._namespace_map[self.ns_uri] = self.ns_prefix |
162 | 165 | |
... | ... | |
176 | 179 |
body = ElementFactory('Body', namespace=self.ns_uri) |
177 | 180 |
content = ElementFactory('Content', namespace=self.ns_uri) |
178 | 181 |
aller = ElementFactory('Aller', namespace=self.ns_uri) |
182 | ||
183 |
code = self.code |
|
184 |
if 'AEC-LA' in self.flow_type and software == MELODIE: |
|
185 |
aller.append(ElementFactory('NumeroDemarche', text='EtatCivil')) |
|
186 |
code = 'ActeEtatCivil-XML' |
|
187 | ||
179 | 188 |
aller.extend([ |
180 | 189 |
self.teledemarche_xml(num, date, platform_id), |
181 | 190 |
self.routage_xml(zip_code), |
182 |
self.document_xml(self.code, form_files)
|
|
191 |
self.document_xml(code, form_files) |
|
183 | 192 |
]) |
184 | 193 | |
185 | 194 |
[aller.append(f) for f in self.piecejointe_xml(attached_files) if f] |
... | ... | |
198 | 207 | |
199 | 208 |
def __init__( |
200 | 209 |
self, flow_type, num, zip_code, date=None, |
201 |
platform_id='1', form_files=[], attached_files=[]): |
|
210 |
platform_id='1', form_files=[], attached_files=[], |
|
211 |
software=None, step=None, doc=None |
|
212 |
): |
|
202 | 213 | |
203 | 214 |
super(Description, self).__init__( |
204 | 215 |
flow_type, num, zip_code, date, |
205 |
platform_id, form_files, attached_files |
|
216 |
platform_id, form_files, attached_files, doc=doc
|
|
206 | 217 |
) |
218 |
self.software = software |
|
219 |
self.step = step |
|
207 | 220 | |
208 | 221 |
root = ElementFactory('EnteteMetierEnveloppe', attrib={'xmlns': self.xmlns}) |
209 | 222 | |
223 |
code = self.code |
|
224 |
if 'AEC-LA' in self.flow_type and self.software == MELODIE: |
|
225 |
root.append(ElementFactory('NumeroDemarche', text='EtatCivil')) |
|
226 |
code = 'ActeEtatCivil-XML' |
|
227 | ||
210 | 228 |
root.extend([ |
211 | 229 |
self.teledemarche_xml(num, date, platform_id), |
212 | 230 |
self.routage_xml(zip_code), |
213 |
self.document_xml(self.code, form_files)
|
|
231 |
self.document_xml(code, form_files) |
|
214 | 232 |
]) |
215 | 233 | |
216 | 234 |
for f in self.piecejointe_xml(attached_files): |
... | ... | |
220 | 238 | |
221 | 239 |
@property |
222 | 240 |
def filename(self): |
241 |
if 'AEC-LA' in self.flow_type and self.software == MELODIE: |
|
242 |
return '%s-EtatCivil-ent-%s.xml' % (self.num, self.step) |
|
223 | 243 |
return '%s-ent-.xml' % self.code |
224 | 244 | |
225 | 245 | |
... | ... | |
227 | 247 |
"""Class describing a -doc.xml file |
228 | 248 |
""" |
229 | 249 | |
230 |
def __init__(self, demand_id, data): |
|
250 |
def __init__(self, demand_id, data, software=None, step=None):
|
|
231 | 251 |
self.data = self.data_validator(data) |
232 | 252 |
self.demand_id = demand_id |
253 |
self.software = software |
|
254 |
self.step = step |
|
233 | 255 | |
234 | 256 |
@property |
235 | 257 |
def filename(self): |
258 |
if 'AEC-LA' in self.demand_id and self.software == MELODIE: |
|
259 |
return '%s-doc-ActeEtatCivil-XML-1-%s.xml' % ( |
|
260 |
self.demand_id.replace('AEC-LA', 'EtatCivil'), self.step |
|
261 |
) |
|
236 | 262 |
return '%s-doc-.xml' % self.demand_id |
237 | 263 | |
238 | 264 |
def save(self, folder): |
... | ... | |
439 | 465 |
'titulaire_prenoms' |
440 | 466 |
] |
441 | 467 | |
442 |
def __init__(self, demand_id, data): |
|
468 |
def __init__(self, demand_id, data, software, demand_num):
|
|
443 | 469 |
self.root_element = 'EnveloppeMetierType' |
444 | 470 |
self.root_attributes = {'canal_utilise': '0'} |
445 | 471 | |
446 |
super(AECData, self).__init__(demand_id, data) |
|
472 |
super(AECData, self).__init__(demand_id, data, software, demand_num)
|
|
447 | 473 | |
448 | 474 | |
449 | 475 |
def json_to_xml(path, value, parent): |
passerelle/apps/mdel/models.py | ||
---|---|---|
179 | 179 | |
180 | 180 |
@property |
181 | 181 |
def name(self): |
182 |
if self.resource.software == MELODIE and self.flow_type == 'AEC-LA': |
|
183 |
return '%s-%s-%d' % (self.num, 'EtatCivil', self.step) |
|
182 | 184 |
return '%s-%s--%d' % (self.num, self.flow_type, self.step) |
183 | 185 | |
184 | 186 |
@property |
... | ... | |
318 | 320 |
formdata['qualite_demandeur'] = formdata['qualite_demandeur_autre'] |
319 | 321 |
formdata['qualite_demandeur_raw'] = 'Autre' |
320 | 322 | |
321 |
doc = mdel.AECData(self.demand_id, formdata) |
|
323 |
doc = mdel.AECData(self.demand_id, formdata, self.resource.software, self.step)
|
|
322 | 324 |
doc.save(inputs_dir) |
323 | 325 |
input_files['demande'] = doc.filename |
324 | 326 | |
325 | 327 |
submission_date = formdata.get('receipt_time', None) |
326 | 328 | |
327 |
message = mdel.Message(flow_type, demand_num, code_insee, date=submission_date) |
|
329 |
message = mdel.Message( |
|
330 |
flow_type, demand_num, code_insee, date=submission_date, |
|
331 |
software=self.resource.software, doc=doc |
|
332 |
) |
|
328 | 333 |
message.save(inputs_dir) |
329 | 334 |
input_files['message'] = message.filename |
330 | 335 | |
331 | 336 |
description = mdel.Description(flow_type, demand_num, code_insee, date=submission_date, |
332 |
attached_files=attached_files) |
|
337 |
attached_files=attached_files, |
|
338 |
software=self.resource.software, step=self.step, doc=doc) |
|
333 | 339 |
description.save(inputs_dir) |
334 | 340 |
input_files['enveloppe'] = description.filename |
335 | 341 |
tests/test_mdel.py | ||
---|---|---|
30 | 30 | |
31 | 31 |
from passerelle.apps.mdel.models import MDEL, Demand |
32 | 32 |
from passerelle.apps.mdel.mdel import Message, Description, AttachedFile, get_resource_base_dir |
33 |
from passerelle.apps.mdel.utils import parse_date |
|
33 |
from passerelle.apps.mdel.utils import parse_date, MELODIE
|
|
34 | 34 | |
35 | 35 |
import utils |
36 | 36 | |
... | ... | |
68 | 68 |
return utils.setup_access_rights(MDEL.objects.create(slug='test')) |
69 | 69 | |
70 | 70 | |
71 |
@pytest.fixture |
|
72 |
def melodie_conn(db): |
|
73 |
return utils.setup_access_rights(MDEL.objects.create(slug='test', software=MELODIE)) |
|
74 | ||
75 | ||
71 | 76 |
@pytest.fixture(params=[ |
72 | 77 |
json.loads(get_file_from_test_base_dir('formdata_aec_naiss.json')), |
73 | 78 |
json.loads(get_file_from_test_base_dir('formdata_aec_mariage.json')), |
... | ... | |
527 | 532 |
with pytest.raises(APIError) as error: |
528 | 533 |
date = parse_date('28-02-2018') |
529 | 534 |
assert 'date u\'28-02-2018\' not iso-formated' in str(error) |
535 | ||
536 | ||
537 |
def test_aec_melodie(app, melodie_conn): |
|
538 |
aec_payload = json.loads(get_file_from_test_base_dir('formdata_aec_naiss.json')) |
|
539 |
resp = app.post_json('/mdel/test/create', params=aec_payload, status=200) |
|
540 | ||
541 |
assert resp.json['data']['demand_id'] == '15-4-AEC-LA' |
|
542 | ||
543 |
basedir = os.path.join(get_resource_base_dir(), 'test', 'inputs', '15-4-EtatCivil-0') |
|
544 |
assert os.path.exists(basedir) |
|
545 |
check_zip_file( |
|
546 |
basedir + '.zip', |
|
547 |
['message.xml', '15-4-EtatCivil-ent-0.xml', '15-4-EtatCivil-doc-ActeEtatCivil-XML-1-0.xml'] |
|
548 |
) |
|
549 | ||
550 |
doc = os.path.join(basedir, '15-4-EtatCivil-doc-ActeEtatCivil-XML-1-0.xml') |
|
551 |
assert os.path.exists(doc) |
|
552 |
validate_schema(doc, AEC_XSD) |
|
553 | ||
554 |
ent = os.path.join(basedir, '15-4-EtatCivil-ent-0.xml') |
|
555 |
assert os.path.exists(ent) |
|
556 |
root = letree.parse(ent).getroot() |
|
557 |
num_dem = root[0] |
|
558 |
assert letree.QName(num_dem).localname == 'NumeroDemarche' |
|
559 |
assert num_dem.text == 'EtatCivil' |
|
560 |
ns = {'ns': 'http://finances.gouv.fr/dgme/gf/composants/teledemarchexml/donnee/metier'} |
|
561 |
assert root.find('ns:Document/ns:Code', namespaces=ns).text == 'ActeEtatCivil-XML' |
|
562 |
assert root.find('ns:Document/ns:Nom', namespaces=ns).text == 'ActeEtatCivil-XML' |
|
563 |
assert root.find('ns:Document/ns:FichierFormulaire/ns:FichierDonnees', namespaces=ns).text == \ |
|
564 |
'15-4-EtatCivil-doc-ActeEtatCivil-XML-1-0.xml' |
|
565 | ||
566 |
message = os.path.join(basedir, 'message.xml') |
|
567 |
assert os.path.exists(message) |
|
568 |
root = letree.parse(message).getroot() |
|
569 |
ns = { |
|
570 |
'ns1': 'http://finances.gouv.fr/dgme/gf/composants/teledemarchexml/donnee/metier', |
|
571 |
'ns2': 'http://finances.gouv.fr/dgme/pec/message/v1' |
|
572 |
} |
|
573 |
assert root.find('ns2:Body/ns2:Content/ns2:Aller/ns1:NumeroDemarche', namespaces=ns).text \ |
|
574 |
== 'EtatCivil' |
|
575 |
assert root.find('ns2:Body/ns2:Content/ns2:Aller/ns1:Document/ns1:Code', namespaces=ns).text \ |
|
576 |
== 'ActeEtatCivil-XML' |
|
577 |
assert root.find('ns2:Body/ns2:Content/ns2:Aller/ns1:Document/ns1:Nom', namespaces=ns).text \ |
|
578 |
== 'ActeEtatCivil-XML' |
|
579 |
assert root.find( |
|
580 |
'ns2:Body/ns2:Content/ns2:Aller/ns1:Document/ns1:FichierFormulaire/ns1:FichierDonnees', |
|
581 |
namespaces=ns |
|
582 |
).text == '15-4-EtatCivil-doc-ActeEtatCivil-XML-1-0.xml' |
|
530 |
- |