python3: use text to manage csv content (#40911)
This commit is contained in:
parent
02f660ca5b
commit
55af5e1d5b
|
@ -2,6 +2,7 @@
|
|||
|
||||
import csv
|
||||
|
||||
from django.utils import six
|
||||
from django.utils.encoding import force_bytes
|
||||
from django.utils.six import StringIO
|
||||
|
||||
|
@ -32,6 +33,12 @@ def test_synchronize_federations(settings, app, nanterre_classic_family, admin):
|
|||
response = response.click('Rapport')
|
||||
|
||||
def check_csv_response(csv_response):
|
||||
if six.PY3:
|
||||
reader = csv.DictReader(StringIO(csv_response.text))
|
||||
reader.fieldnames = reader.reader.__next__()
|
||||
else:
|
||||
reader = csv.DictReader(StringIO(csv_response.content))
|
||||
reader.fieldnames = reader.reader.next()
|
||||
rows = list(reader)
|
||||
|
||||
def rows_by_action(action):
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from django import forms
|
||||
from django.utils.encoding import force_text
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.core.exceptions import ValidationError
|
||||
|
||||
|
@ -64,7 +65,7 @@ class SynchronizeFederationsForm(forms.Form):
|
|||
errors = []
|
||||
for i, line in enumerate(csv_uploaded):
|
||||
try:
|
||||
line.encode('ascii')
|
||||
force_text(line).encode('ascii')
|
||||
# works with pyhton2 and 3
|
||||
except (UnicodeEncodeError, UnicodeDecodeError) as e:
|
||||
errors.append(_(u'non-ASCII character on line {0} and column {1}').format(
|
||||
|
|
|
@ -23,6 +23,7 @@ from django.core.urlresolvers import reverse
|
|||
from django.conf import settings
|
||||
from django.db import DatabaseError
|
||||
from django.db.transaction import atomic
|
||||
from django.utils import six
|
||||
from django.utils.encoding import force_bytes
|
||||
from django.utils.six import StringIO
|
||||
|
||||
|
@ -109,7 +110,10 @@ class SynchronizeFederationsImport(object):
|
|||
writer.writerow(['RSU ID', 'prenoms', 'nom de naissance',
|
||||
'nom d\'usage', 'application', 'federation', 'action'])
|
||||
for action in self.actions:
|
||||
action = [force_bytes(v) for v in action]
|
||||
if six.PY3:
|
||||
action = [v for v in action]
|
||||
else:
|
||||
action = [force_bytes(v) for v in action]
|
||||
writer.writerow(action)
|
||||
setattr(self.action, target + '_csv_filename',
|
||||
self.action.csv_filename + '-report.csv')
|
||||
|
|
|
@ -17,7 +17,6 @@
|
|||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from __future__ import print_function
|
||||
import six
|
||||
import functools
|
||||
import uuid
|
||||
import io
|
||||
|
@ -45,6 +44,7 @@ from django.db import transaction
|
|||
from django.contrib.auth.hashers import make_password
|
||||
from django.http import HttpResponse
|
||||
|
||||
from django.utils import six
|
||||
from django.utils.timezone import now, make_aware
|
||||
from django.utils.encoding import force_bytes
|
||||
|
||||
|
@ -1268,7 +1268,7 @@ def individu_caption(individu):
|
|||
|
||||
|
||||
def csv_export_response(rows, filename):
|
||||
if sys.version >= (3,):
|
||||
if six.PY3:
|
||||
with io.StringIO(newline='') as f:
|
||||
writer = csv.writer(f)
|
||||
for row in rows:
|
||||
|
|
Loading…
Reference in New Issue