summaryrefslogtreecommitdiff
path: root/ishtar_common/data_importer.py
diff options
context:
space:
mode:
Diffstat (limited to 'ishtar_common/data_importer.py')
-rw-r--r--ishtar_common/data_importer.py39
1 files changed, 36 insertions, 3 deletions
diff --git a/ishtar_common/data_importer.py b/ishtar_common/data_importer.py
index 09ef64c26..b669995e3 100644
--- a/ishtar_common/data_importer.py
+++ b/ishtar_common/data_importer.py
@@ -21,6 +21,7 @@ import copy
import csv
import datetime
import io
+import os
import logging
import re
import sys
@@ -222,6 +223,7 @@ class YearFormater(Formater):
except (ValueError, AssertionError):
raise ValueError(_(u"\"%(value)s\" is not a valid date") % {
'value': value})
+ return value
class YearNoFuturFormater(Formater):
@@ -235,6 +237,7 @@ class YearNoFuturFormater(Formater):
except (ValueError, AssertionError):
raise ValueError(_(u"\"%(value)s\" is not a valid date") % {
'value': value})
+ return value
class IntegerFormater(Formater):
@@ -498,6 +501,8 @@ class FileFormater(Formater):
f.write(z.read())
f = open(filename, 'r')
my_file = File(f)
+ # manualy set the file size because of an issue with TempFile
+ my_file.size = os.stat(filename).st_size
return my_file
except KeyError:
raise ValueError(_(u"\"%(value)s\" is not a valid path for the "
@@ -1159,7 +1164,7 @@ class Importer(object):
concat_str=concat_str[idx])
c_row.append(u" ; ".join([v for v in c_values]))
- def get_field(self, cls, attribute, data, m2ms, c_path):
+ def get_field(self, cls, attribute, data, m2ms, c_path, new_created):
field_object, model, direct, m2m = \
cls._meta.get_field_by_name(attribute)
if m2m:
@@ -1224,9 +1229,19 @@ class Importer(object):
for k in v.keys():
if k not in field_names:
continue
- self.get_field(model, k, v, m2m_m2ms, c_c_path)
+ self.get_field(model, k, v, m2m_m2ms, c_c_path,
+ new_created)
if '__force_new' in v:
created = v.pop('__force_new')
+ key = u";".join([u"{}-{}".format(k, v[k])
+ for k in sorted(v.keys())])
+ # only one forced creation
+ if attribute in new_created \
+ and key in new_created[attribute]:
+ continue
+ if attribute not in new_created:
+ new_created[attribute] = []
+ new_created[attribute].append(key)
has_values = bool([1 for k in v if v[k]])
if has_values:
v = model.objects.create(**v)
@@ -1234,8 +1249,24 @@ class Importer(object):
continue
else:
v['defaults'] = v.get('defaults', {})
+ extra_fields = {}
+ # "File" type is a temp object and can be different
+ # for the same filename - it must be treated
+ # separatly
+ for field in model._meta.fields:
+ k = field.name
+ # attr_class est un attribut de FileField
+ if hasattr(field, 'attr_class') and k in v:
+ extra_fields[k] = v.pop(k)
v, created = model.objects.get_or_create(
**v)
+ changed = False
+ for k in extra_fields.keys():
+ if extra_fields[k]:
+ changed = True
+ setattr(v, k, extra_fields[k])
+ if changed:
+ v.save()
for att, objs in m2m_m2ms:
if type(objs) not in (list, tuple):
objs = [objs]
@@ -1268,6 +1299,7 @@ class Importer(object):
c_path = path[:]
# get all related fields
+ new_created = {}
for attribute in list(data.keys()):
c_c_path = c_path[:]
if not attribute:
@@ -1276,7 +1308,8 @@ class Importer(object):
if not data[attribute]:
continue
if attribute != '__force_new':
- self.get_field(cls, attribute, data, m2ms, c_c_path)
+ self.get_field(cls, attribute, data, m2ms, c_c_path,
+ new_created)
# filter uncessary default values
create_dict = copy.deepcopy(data)