actesweb: fix unicode content writing into file (#28272)
Add Python3 support.
This commit is contained in:
parent
e06d33da0f
commit
f924283083
|
@ -18,6 +18,8 @@ from __future__ import unicode_literals
|
|||
import json
|
||||
import os
|
||||
import tempfile
|
||||
import six
|
||||
import contextlib
|
||||
|
||||
from django.core.files.storage import default_storage
|
||||
from django.template.loader import get_template
|
||||
|
@ -30,6 +32,15 @@ from passerelle.utils.api import endpoint
|
|||
from passerelle.utils.jsonresponse import APIError
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def named_tempfile(*args, **kwargs):
|
||||
with tempfile.NamedTemporaryFile(*args, **kwargs) as fp:
|
||||
if six.PY2:
|
||||
import codecs
|
||||
fp = codecs.getwriter('utf-8')(fp)
|
||||
yield fp
|
||||
|
||||
|
||||
class ActesWeb(BaseResource):
|
||||
category = _('Civil Status Connectors')
|
||||
|
||||
|
@ -70,7 +81,7 @@ class ActesWeb(BaseResource):
|
|||
|
||||
filename = '%s.DEM' % now().strftime('%Y-%m-%d_%H-%M-%S_%f')
|
||||
filepath = os.path.join(self.basepath, filename)
|
||||
with tempfile.NamedTemporaryFile(dir=tmp_dir, suffix='.DEM', delete=False) as tpf:
|
||||
with named_tempfile(dir=tmp_dir, suffix='.DEM', delete=False) as tpf:
|
||||
tpf.write(demand_content)
|
||||
tpf.flush()
|
||||
os.fsync(tpf.file.fileno())
|
||||
|
|
|
@ -5,9 +5,9 @@
|
|||
"applicant_address_county": "Meurthe-et-Moselle",
|
||||
"applicant_email": "chelsea@whatever.com",
|
||||
"applicant_phone": "+33 6 55 44 22 11",
|
||||
"applicant_address_street": "37 Rue du Cheval Blanc",
|
||||
"applicant_address_street": "169, rue du Château",
|
||||
"applicant_address_zipcode": "54000",
|
||||
"applicant_firstnames": "Kim Chelsea",
|
||||
"applicant_firstnames": "Zoé",
|
||||
"applicant_lastname": "Bar",
|
||||
"applicant_name_usage": "nom d'epouse",
|
||||
"applicant_status": "concerne",
|
||||
|
|
|
@ -20,6 +20,7 @@ import json
|
|||
import io
|
||||
import os
|
||||
import shutil
|
||||
import six
|
||||
|
||||
import pytest
|
||||
|
||||
|
@ -76,7 +77,10 @@ def get_demand_filepath(con, demand_id):
|
|||
def assert_file_content_values(filename, expectations):
|
||||
with io.open(filename, 'rb') as fp:
|
||||
for line in fp.readlines():
|
||||
field, value = line.split('=')
|
||||
if six.PY2:
|
||||
field, value = line.decode('utf-8').split('=')
|
||||
else:
|
||||
field, value = line.split('=')
|
||||
if field in expectations:
|
||||
assert value.strip() == expectations[field]
|
||||
|
||||
|
@ -120,11 +124,11 @@ def test_demand_creation(app, payload, actesweb):
|
|||
DEMANDEUR_CIVILITE="Madame",
|
||||
DEMANDEUR_NOM_USAGE="Whatever",
|
||||
DEMANDEUR_NOM="Bar",
|
||||
DEMANDEUR_PRENOMS="Kim Chelsea",
|
||||
DEMANDEUR_ADRESSE1="37 Rue du Cheval Blanc",
|
||||
DEMANDEUR_PRENOMS="Zoé",
|
||||
DEMANDEUR_ADRESSE1="169, rue du Château",
|
||||
DEMANDEUR_VILLE="Nancy",
|
||||
DEMANDEUR_PAYS="France",
|
||||
DEMANDEUR_TEL="+33 6 55 44 22 11",
|
||||
DEMANDEUR_TEL="+33 6 55 44 22 11",
|
||||
DEMANDEUR_ADR="chelsea@whatever.com",
|
||||
DEMANDE_NOM="Whatever",
|
||||
DEMANDE_PRENOMS="Kevin",
|
||||
|
|
Loading…
Reference in New Issue