From cf3957fb9280194b095b4cc939462a412d57210b Mon Sep 17 00:00:00 2001 From: Serghei Mihai Date: Mon, 15 Apr 2019 16:13:19 +0200 Subject: [PATCH] cityweb: do not compress demand files (#32091) --- passerelle/apps/cityweb/cityweb.py | 17 ++++++++------- tests/test_cityweb.py | 34 +++++++++++++++--------------- 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/passerelle/apps/cityweb/cityweb.py b/passerelle/apps/cityweb/cityweb.py index c58c4269..975bad95 100644 --- a/passerelle/apps/cityweb/cityweb.py +++ b/passerelle/apps/cityweb/cityweb.py @@ -15,7 +15,6 @@ # along with this program. If not, see . import os -import zipfile from lxml import etree, objectify as xobject from django.core.files.storage import default_storage @@ -23,6 +22,7 @@ from django.core.files.base import ContentFile from django.utils.dateparse import parse_date from passerelle.utils.jsonresponse import APIError +from passerelle.utils.files import atomic_write CERTIFICATE_TYPES = [ @@ -376,11 +376,12 @@ class CivilStatusApplication(ComplexType): def save(self, path): basename = os.path.join(path, self.identifiant) - archname = basename + '.zip' - filepath = basename + '.xml' + filename = basename + '.xml' content = etree.tostring(self.xml, pretty_print=True) - default_storage.save(filepath, ContentFile(content)) - with zipfile.ZipFile(archname, 'w', zipfile.ZIP_DEFLATED) as zipf: - zipf.write(filepath, os.path.basename(filepath)) - os.remove(filepath) - return archname + filepath = default_storage.path(filename) + dirname = os.path.dirname(filepath) + if not os.path.exists(dirname): + os.makedirs(dirname) + with atomic_write(filepath) as fd: + fd.write(content) + return filename diff --git a/tests/test_cityweb.py b/tests/test_cityweb.py index 74a1e082..b1c206d0 100644 --- a/tests/test_cityweb.py +++ b/tests/test_cityweb.py @@ -65,19 +65,19 @@ def payload(request): return request.param -def assert_xml_doc(archive, assertions): +def assert_xml_doc(filename, assertions): schema = etree.XMLSchema( etree.parse(open(os.path.join(get_test_base_dir('cityweb'), 'cityweb.xsd')))) - with zipfile.ZipFile(archive) as zfd: - content = zfd.read(zfd.filelist[0].filename) - xml_content = etree.fromstring(content) - assert len(xml_content.nsmap) == 1 - assert xml_content.nsmap['xs'] == "http://tempuri.org/XMLSchema.xsd" - schema.assertValid(xml_content) - root = xobject.fromstring(content) - for epath, value in assertions.items(): - path = xobject.ObjectPath('demandeEtatCivil.%s' % epath) - assert path.find(root) == value + + content = file(filename).read() + xml_content = etree.fromstring(content) + assert len(xml_content.nsmap) == 1 + assert xml_content.nsmap['xs'] == "http://tempuri.org/XMLSchema.xsd" + schema.assertValid(xml_content) + root = xobject.fromstring(content) + for epath, value in assertions.items(): + path = xobject.ObjectPath('demandeEtatCivil.%s' % epath) + assert path.find(root) == value @mock.patch('passerelle.apps.cityweb.models.default_storage.path', get_test_base_dir) @@ -86,7 +86,7 @@ def test_demand_creation(app, setup, payload): if 'birth' in payload: response = app.post_json(url, params=payload['birth']) assert response.json['data']['demand_id'] == 'N201610154' - archive = os.path.join(get_test_base_dir('cityweb'), 'test', 'N201610154.zip') + filename = os.path.join(get_test_base_dir('cityweb'), 'test', 'N201610154.xml') assertions = { 'identifiant': 'N201610154', 'demandeur.qualiteDemandeur': 'concerne', @@ -107,12 +107,12 @@ def test_demand_creation(app, setup, payload): 'evenement.interesse.mere.noms.nomDeFamille': 'Smith', 'evenement.interesse.mere.prenoms': 'Kim', } - assert_xml_doc(archive, assertions) + assert_xml_doc(filename, assertions) elif 'mariage' in payload: response = app.post_json(url, params=payload['mariage']) assert response.json['data']['demand_id'] == 'M201610161' - archive = os.path.join(get_test_base_dir('cityweb'), 'test', 'M201610161.zip') + filename = os.path.join(get_test_base_dir('cityweb'), 'test', 'M201610161.xml') assertions = { 'identifiant': 'M201610161', 'demandeur.qualiteDemandeur': 'concerne', @@ -138,11 +138,11 @@ def test_demand_creation(app, setup, payload): 'evenement.conjoint.mere.noms.nomDeFamille': 'Scaramucci', 'evenement.conjoint.mere.prenoms': 'Marguerite', } - assert_xml_doc(archive, assertions) + assert_xml_doc(filename, assertions) else: response = app.post_json(url, params=payload['death']) assert response.json['data']['demand_id'] == 'D201610171' - archive = os.path.join(get_test_base_dir('cityweb'), 'test', 'D201610171.zip') + filename = os.path.join(get_test_base_dir('cityweb'), 'test', 'D201610171.xml') assertions = { 'identifiant': 'D201610171', 'demandeur.qualiteDemandeur': 'concerne', @@ -159,7 +159,7 @@ def test_demand_creation(app, setup, payload): 'evenement.dateEvenement.dateDebut': '2012-07-14', 'evenement.lieuEvenement.ville': 'Nancy', } - assert_xml_doc(archive, assertions) + assert_xml_doc(filename, assertions) def test_date_type_parsing():