diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2017-02-14 20:47:35 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2017-02-14 20:47:35 +0100 |
commit | 5deda4ab4d3c9195cc21aeedbafd1b8d5d88ce45 (patch) | |
tree | 6676db2c7e898db0677f0440c5a979616827b8a8 | |
parent | 3c029cc9a2aafc9c6f05257f013770eb20ed0499 (diff) | |
download | Ishtar-5deda4ab4d3c9195cc21aeedbafd1b8d5d88ce45.tar.bz2 Ishtar-5deda4ab4d3c9195cc21aeedbafd1b8d5d88ce45.zip |
Importer: report bad configuration in a cleaner way
-rw-r--r-- | archaeological_operations/tests.py | 18 | ||||
-rw-r--r-- | ishtar_common/data_importer.py | 13 | ||||
-rw-r--r-- | ishtar_common/models.py | 14 |
3 files changed, 40 insertions, 5 deletions
diff --git a/archaeological_operations/tests.py b/archaeological_operations/tests.py index 040c7c3d8..7c141af93 100644 --- a/archaeological_operations/tests.py +++ b/archaeological_operations/tests.py @@ -35,7 +35,8 @@ import models from archaeological_operations import views from ishtar_common.models import OrganizationType, Organization, \ - ImporterType, IshtarUser, TargetKey, ImporterModel, IshtarSiteProfile, Town + ImporterType, IshtarUser, TargetKey, ImporterModel, IshtarSiteProfile, \ + Town, ImporterColumn from archaeological_context_records.models import Unit from ishtar_common import forms_common @@ -203,6 +204,21 @@ class ImportOperationTest(ImportTest, TestCase): self.assertTrue(last_ope == models.Operation.objects.order_by('-pk').all()[0]) + def test_bad_configuration(self): + importer, form = self.init_ope_import() + col = ImporterColumn.objects.get(importer_type=importer, col_number=1) + target = col.targets.all()[0] + target.target = "cody" # random and not appropriate string + target.save() + # self.init_ope() + # importer, form = self.init_ope_import() + impt = form.save(self.ishtar_user) + impt.initialize() + self.init_ope_targetkey(imp=impt) + impt.importation() + self.assertEqual(len(impt.errors), 1) + self.assertIn("Importer configuration error", impt.errors[0]['error']) + def test_model_limitation(self): importer, form = self.init_ope_import() importer.created_models.clear() diff --git a/ishtar_common/data_importer.py b/ishtar_common/data_importer.py index a03f4de34..0b6662e1c 100644 --- a/ishtar_common/data_importer.py +++ b/ishtar_common/data_importer.py @@ -29,7 +29,7 @@ import zipfile from django.conf import settings from django.contrib.auth.models import User -from django.core.exceptions import ImproperlyConfigured +from django.db.models.fields import FieldDoesNotExist from django.core.files import File from django.db import IntegrityError, DatabaseError, transaction from django.template.defaultfilters import slugify @@ -733,7 +733,7 @@ class Importer(object): q = ImporterModel.objects.filter(klass=cls_name) if q.count(): cls_name = q.all()[0].name - return ImproperlyConfigured( + return ImporterError( unicode(self.ERRORS['improperly_configured']).format(cls_name)) def _get_does_not_exist_in_db_error(self, model, data): @@ -1217,8 +1217,13 @@ class Importer(object): c_row.append(u" ; ".join([v for v in c_values])) def get_field(self, cls, attribute, data, m2ms, c_path, new_created): - field_object, model, direct, m2m = \ - cls._meta.get_field_by_name(attribute) + try: + field_object, model, direct, m2m = \ + cls._meta.get_field_by_name(attribute) + except FieldDoesNotExist: + raise ImporterError(unicode( + _(u"Importer configuration error: field \"{}\" does not exist " + u"for {}.")).format(attribute, cls._meta.verbose_name)) if m2m: many_values = data.pop(attribute) if hasattr(field_object, 'rel'): diff --git a/ishtar_common/models.py b/ishtar_common/models.py index 90e2bd6f6..c1edc13c6 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -22,6 +22,7 @@ Models description """ from cStringIO import StringIO import copy +import csv import datetime from PIL import Image from importlib import import_module @@ -2302,6 +2303,19 @@ class Import(models.Model): return bool(TargetKey.objects.filter(associated_import=self, is_set=False).count()) + @property + def errors(self): + if not self.error_file: + return [] + errors = [] + with open(self.error_file.path, 'rb') as csvfile: + reader = csv.DictReader(csvfile, fieldnames=['line', 'column', + 'error']) + reader.next() # pass the header + for row in reader: + errors.append(row) + return errors + def get_actions(self): """ Get available action relevant with the current status |