diff --git a/mayan/apps/importer/management/commands/import.py b/mayan/apps/importer/management/commands/import.py index 86d32da44d..2ddfba6ea1 100644 --- a/mayan/apps/importer/management/commands/import.py +++ b/mayan/apps/importer/management/commands/import.py @@ -70,8 +70,10 @@ class Command(management.BaseCommand): self.stderr.write('Must specify a CSV file path.') exit(1) else: - with open(options['filelist']) as csv_datafile: - csv_reader = csv.reader(csv_datafile) + with open(options['filelist'], mode='r') as csv_datafile: + csv_reader = csv.reader( + csv_datafile, delimiter=',', quotechar='"' + ) for row in csv_reader: # Increase row count here even though start index is 0 # purpose is to avoid losing row number increments on @@ -79,7 +81,7 @@ class Command(management.BaseCommand): row_count = row_count + 1 if row_count - 1 not in rows_to_ignore: try: - with open(row[options['document_path_column']]) as file_object: + with open(row[options['document_path_column']], mode='rb') as file_object: document_type_label = row[options['document_type_column']] if document_type_label not in document_types: diff --git a/mayan/apps/importer/tests/test_management_commands.py b/mayan/apps/importer/tests/test_management_commands.py index 156a4c1c8d..2d6871c63b 100644 --- a/mayan/apps/importer/tests/test_management_commands.py +++ b/mayan/apps/importer/tests/test_management_commands.py @@ -3,7 +3,7 @@ from __future__ import unicode_literals import csv from django.core import management -from django.utils.encoding import force_bytes +from django.utils.encoding import force_bytes, force_text from mayan.apps.documents.models import DocumentType, Document from mayan.apps.documents.tests.base import GenericDocumentTestCase @@ -27,13 +27,13 @@ class ImportManagementCommandTestCase(GenericDocumentTestCase): super(ImportManagementCommandTestCase, self).tearDown() def _create_test_csv_file(self): - self.test_csv_file_descriptor, self.test_csv_path = mkstemp() + self.test_csv_path = mkstemp()[1] print('Test CSV file: {}'.format(self.test_csv_path)) - with open(self.test_csv_path, mode='wb') as csvfile: + with open(self.test_csv_path, mode='w', newline='') as file_object: filewriter = csv.writer( - csvfile, delimiter=force_bytes(','), quotechar=force_bytes('"'), + file_object, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL ) print( @@ -58,10 +58,7 @@ class ImportManagementCommandTestCase(GenericDocumentTestCase): ) def _destroy_test_csv_file(self): - fs_cleanup( - filename=self.test_csv_path, - file_descriptor=self.test_csv_file_descriptor - ) + fs_cleanup(filename=self.test_csv_path) def test_import_csv_read(self): self.test_document_type.delete()