Removed usage of unicodecsv

This commit is contained in:
Karl Hobley 2015-06-02 00:18:30 +01:00
parent 3b26ddc162
commit 5ea9e8a7eb
2 changed files with 22 additions and 11 deletions

View file

@ -360,6 +360,24 @@ class TestFormsSubmissions(TestCase, WagtailTestUtils):
data_line = response.content.decode().split("\n")[1]
self.assertIn('new@example.com', data_line)
def test_list_submissions_csv_export_with_unicode(self):
unicode_form_submission = FormSubmission.objects.create(
page=self.form_page,
form_data=json.dumps({
'your-email': "unicode@example.com",
'your-message': 'こんにちは、世界',
}),
)
unicode_form_submission.submit_time = '2014-01-02T12:00:00.000Z'
unicode_form_submission.save()
response = self.client.get(reverse('wagtailforms_list_submissions', args=(self.form_page.id, )), {'date_from': '01/02/2014', 'action': 'CSV'})
# Check response
self.assertEqual(response.status_code, 200)
data_line = response.content.decode('utf-8').split("\n")[1]
self.assertIn('こんにちは、世界', data_line)
class TestIssue798(TestCase):
fixtures = ['test.json']

View file

@ -1,16 +1,12 @@
import datetime
try:
import unicodecsv as csv
using_unicodecsv = True
except ImportError:
import csv
using_unicodecsv = False
import csv
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.core.exceptions import PermissionDenied
from django.http import HttpResponse
from django.shortcuts import get_object_or_404, render
from django.utils.encoding import smart_str
from wagtail.wagtailcore.models import Page
from wagtail.wagtailforms.models import FormSubmission, get_forms_for_user
@ -69,10 +65,7 @@ def list_submissions(request, page_id):
response = HttpResponse(content_type='text/csv; charset=utf-8')
response['Content-Disposition'] = 'attachment;filename=export.csv'
if using_unicodecsv:
writer = csv.writer(response, encoding='utf-8')
else:
writer = csv.writer(response)
writer = csv.writer(response)
header_row = ['Submission date'] + [label for name, label in data_fields]
@ -81,7 +74,7 @@ def list_submissions(request, page_id):
data_row = [s.submit_time]
form_data = s.get_data()
for name, label in data_fields:
data_row.append(form_data.get(name))
data_row.append(smart_str(form_data.get(name)))
writer.writerow(data_row)
return response