From 446eab605172aa778569914e7c43232c013d0d1a Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Mon, 28 Aug 2017 17:06:24 +0200 Subject: Imports: fix CSV file saving --- ishtar_common/models_imports.py | 28 +++++++++++-------------- ishtar_common/templates/ishtar/import_list.html | 10 ++++----- 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/ishtar_common/models_imports.py b/ishtar_common/models_imports.py index 2a594584e..cfc8857de 100644 --- a/ishtar_common/models_imports.py +++ b/ishtar_common/models_imports.py @@ -31,7 +31,7 @@ import zipfile from django.conf import settings from django.contrib.gis.db import models from django.core.exceptions import SuspiciousOperation -from django.core.files import File +from django.core.files.base import ContentFile from django.db.models.base import ModelBase from django.db.models.signals import pre_delete from django.template.defaultfilters import slugify @@ -861,29 +861,25 @@ class Import(models.Model): filename = slugify(self.importer_type.name) now = datetime.datetime.now().isoformat('-').replace(':', '') result_file = filename + "_result_%s.csv" % now - result_file = os.sep.join([self.result_file.storage.location, - result_file]) - with open(result_file, 'w') as fle: - fle.write(importer.get_csv_result().encode('utf-8')) - self.result_file = File(open(fle.name)) + self.result_file.save( + result_file, ContentFile(importer.get_csv_result().encode('utf-8'))) + if importer.errors: self.state = 'FE' error_file = filename + "_errors_%s.csv" % now - error_file = os.sep.join([self.error_file.storage.location, - error_file]) - with open(error_file, 'w') as fle: - fle.write(importer.get_csv_errors().encode('utf-8')) - self.error_file = File(open(fle.name)) + self.error_file.save( + error_file, + ContentFile(importer.get_csv_errors().encode('utf-8')) + ) else: self.state = 'F' self.error_file = None if importer.match_table: match_file = filename + "_match_%s.csv" % now - match_file = os.sep.join([self.match_file.storage.location, - match_file]) - with open(match_file, 'w') as fle: - fle.write(importer.get_csv_matches().encode('utf-8')) - self.match_file = File(open(fle.name)) + self.match_file.save( + match_file, + ContentFile(importer.get_csv_matches().encode('utf-8')) + ) self.save() def archive(self): diff --git a/ishtar_common/templates/ishtar/import_list.html b/ishtar_common/templates/ishtar/import_list.html index d5747fe88..c72c86d10 100644 --- a/ishtar_common/templates/ishtar/import_list.html +++ b/ishtar_common/templates/ishtar/import_list.html @@ -25,7 +25,7 @@ {{import.importer_type}} - {% trans "Source file" %} + {% trans "Source file" %} {{import.creation_date}} ({{import.user}}) @@ -47,18 +47,18 @@ {% endif %} {% if import.error_file %} - {% trans "Error file" %} + {% trans "Error file" %} {% endif %} {% if import.result_file %} - {% trans "Control file" %} + {% trans "Control file" %} {% endif %} {% if import.match_file %} - {% trans "Match file" %} + {% trans "Match file" %} {% endif %} {% endfor %} - + {% endif %} -- cgit v1.2.3