From 86e908728a7f192bc8aa2a409ed66c7f1b43d0ec Mon Sep 17 00:00:00 2001
From: Serghei Mihai <smihai@entrouvert.com>
Date: Mon, 15 Apr 2019 16:13:19 +0200
Subject: [PATCH] cityweb: do not compress demand files (#32091)

---
 passerelle/apps/cityweb/cityweb.py | 13 +++++-------
 passerelle/apps/cityweb/models.py  |  1 +
 tests/test_cityweb.py              | 34 +++++++++++++++---------------
 3 files changed, 23 insertions(+), 25 deletions(-)

diff --git a/passerelle/apps/cityweb/cityweb.py b/passerelle/apps/cityweb/cityweb.py
index c58c4269..22dd120b 100644
--- a/passerelle/apps/cityweb/cityweb.py
+++ b/passerelle/apps/cityweb/cityweb.py
@@ -15,7 +15,6 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 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,8 @@ 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
+        with atomic_write(default_storage.path(filename)) as fd:
+            fd.write(content)
+        return filename
diff --git a/passerelle/apps/cityweb/models.py b/passerelle/apps/cityweb/models.py
index 0474039a..0a8dd44f 100644
--- a/passerelle/apps/cityweb/models.py
+++ b/passerelle/apps/cityweb/models.py
@@ -19,6 +19,7 @@ import json
 
 from django.utils.translation import ugettext_lazy as _
 from django.core.files.storage import default_storage
+from django.db import models
 
 from passerelle.base.models import BaseResource
 from passerelle.utils.api import endpoint
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():
-- 
2.20.1

