Skip to content

Commit

Permalink
Merge pull request #322 from django-import-export/fix-127-csv-as-text
Browse files Browse the repository at this point in the history
Open csv with as text and not binary (FIX #127)
  • Loading branch information
bmihelac committed Sep 14, 2015
2 parents e77022e + d1add98 commit 5f7c030
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
11 changes: 1 addition & 10 deletions import_export/formats/base_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,10 @@ def is_binary(self):
return False


class CSV(TablibFormat):
"""
CSV is treated as binary in Python 2.
"""
class CSV(TextFormat):
TABLIB_MODULE = 'tablib.formats._csv'
CONTENT_TYPE = 'text/csv'

def get_read_mode(self):
return 'rU' if six.PY3 else 'rb'

def is_binary(self):
return False if six.PY3 else True


class JSON(TextFormat):
TABLIB_MODULE = 'tablib.formats._json'
Expand Down
2 changes: 2 additions & 0 deletions tests/core/exports/books-dos.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
id,name,author_email
1,Some book,test@example.com
17 changes: 14 additions & 3 deletions tests/core/tests/base_formats_tests.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from __future__ import unicode_literals

import os

from django.test import TestCase
from django.utils import six

from import_export.formats import base_formats

Expand All @@ -14,5 +15,15 @@ def test_binary_format(self):

class CSVTest(TestCase):

def test_binary_format(self):
self.assertEqual(base_formats.CSV().is_binary(), not six.PY3)
def setUp(self):
self.format = base_formats.CSV()

def test_import_dos(self):
filename = os.path.join(
os.path.dirname(__file__),
os.path.pardir,
'exports',
'books-dos.csv')
in_stream = open(filename, self.format.get_read_mode()).read()
expected = 'id,name,author_email\n1,Some book,test@example.com\n'
self.assertEqual(in_stream, expected)

0 comments on commit 5f7c030

Please sign in to comment.