From 695302f914fc045e75d8c721427ce2309e108cb4 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Tue, 20 Jun 2017 18:49:32 +0200 Subject: Imports: explicit message (instead of crash) on misconfiguration of importers --- ishtar_common/data_importer.py | 41 ++++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) (limited to 'ishtar_common/data_importer.py') diff --git a/ishtar_common/data_importer.py b/ishtar_common/data_importer.py index 10957e74d..e11e72449 100644 --- a/ishtar_common/data_importer.py +++ b/ishtar_common/data_importer.py @@ -1400,20 +1400,35 @@ class Importer(object): # get all related fields new_created = {} - for attribute in list(data.keys()): - c_c_path = c_path[:] - if not attribute: - data.pop(attribute) - continue - if not data[attribute]: - field_object, model, direct, m2m = \ - cls._meta.get_field_by_name(attribute) - if m2m: + try: + for attribute in list(data.keys()): + c_c_path = c_path[:] + if not attribute: data.pop(attribute) - continue - if attribute != '__force_new': - self.get_field(cls, attribute, data, m2ms, c_c_path, - new_created) + continue + if not data[attribute]: + field_object, model, direct, m2m = \ + cls._meta.get_field_by_name(attribute) + if m2m: + data.pop(attribute) + continue + if attribute != '__force_new': + self.get_field(cls, attribute, data, m2ms, c_c_path, + new_created) + except (ValueError, IntegrityError) as e: + message = e.message + try: + message = e.message.decode('utf-8') + except (UnicodeDecodeError, UnicodeDecodeError): + message = '' + try: + data = unicode(data) + except UnicodeDecodeError: + data = '' + raise ImporterError( + "Erreur d'import %s %s, contexte : %s, erreur : %s" + % (unicode(cls), unicode("__".join(path)), + unicode(data), message)) create_dict = copy.deepcopy(data) for k in create_dict.keys(): -- cgit v1.2.3