diff options
author | Étienne Loks <etienne.loks@proxience.com> | 2015-01-27 15:26:09 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@proxience.com> | 2015-01-27 15:26:09 +0100 |
commit | c730975bd9dab4deaf79880075fb919ed9f96427 (patch) | |
tree | 70d2aa4499630be5305124b5f3f940c9b9c7b45f /ishtar_common | |
parent | f960a671e29949be75d50d1ec9e92e90cd62a9f7 (diff) | |
download | Ishtar-c730975bd9dab4deaf79880075fb919ed9f96427.tar.bz2 Ishtar-c730975bd9dab4deaf79880075fb919ed9f96427.zip |
Bibracte import for context records.
Diffstat (limited to 'ishtar_common')
-rw-r--r-- | ishtar_common/data_importer.py | 42 |
1 files changed, 31 insertions, 11 deletions
diff --git a/ishtar_common/data_importer.py b/ishtar_common/data_importer.py index 87b3a40b0..d77f28e18 100644 --- a/ishtar_common/data_importer.py +++ b/ishtar_common/data_importer.py @@ -201,11 +201,10 @@ class StrChoiceFormater(Formater): def prepare(self, value): return unicode(value).strip() - def check(self, values, output=None): - if not output or output == 'silent': - return + def _get_choices(self): msgstr = unicode(_(u"Choice for \"%s\" is not available. "\ u"Which one is relevant?\n")) + idx = -1 for idx, choice in enumerate(self.choices): msgstr += u"%d. %s\n" % (idx+1, choice[1]) idx += 2 @@ -214,6 +213,11 @@ class StrChoiceFormater(Formater): + u"\n" idx += 1 msgstr += unicode(_(u"%d. None of the above - skip")) % idx + u"\n" + return msgstr, idx + + def check(self, values, output=None): + if not output or output == 'silent': + return if self.many_split: new_values = [] r = re.compile(self.many_split) @@ -228,6 +232,7 @@ class StrChoiceFormater(Formater): if output != 'cli': self.missings.add(value) continue + msgstr, idx = self._get_choices() res = None while res not in range(1, idx+1): sys.stdout.write(msgstr % value) @@ -245,6 +250,8 @@ class StrChoiceFormater(Formater): self.add_key(v, value) elif self.create and res == len(self.choices): self.equiv_dict[value] = self.new(base_value) + self.choices.append((self.equiv_dict[value].pk, + unicode(self.equiv_dict[value]))) else: self.equiv_dict[value] = None @@ -282,11 +289,16 @@ class TypeFormater(StrChoiceFormater): obj.add_key(slugify(value), force=True) def new(self, value): - values = copy(self.defaults) + values = copy.copy(self.defaults) values['label'] = value values['txt_idx'] = slugify(value) - self.model.objects.create(**values) - return + if 'order' in self.model._meta.get_all_field_names(): + order = 1 + q = self.model.objects.values('order').order_by('-order') + if q.count(): + order = q.all()[0]['order'] or 1 + values['order'] = order + return self.model.objects.create(**values) class DateFormater(Formater): def __init__(self, date_format="%d/%m/%Y"): @@ -386,7 +398,8 @@ class Importer(object): self.test = test self.errors = [] # list of (line, col, message) self.messages = [] # list of (line, col, message) - self.number_imported = 0 + self.number_updated = 0 + self.number_created = 0 self.check_col_num = check_col_num self.check_validity = check_validity self.line_format = copy.copy(self.LINE_FORMAT) @@ -555,7 +568,6 @@ class Importer(object): if self.test: return # manage unicity of items (mainly for updates) - self.number_imported += 1 if self.UNICITY_KEYS: data['defaults'] = {} for k in data.keys(): @@ -563,9 +575,16 @@ class Importer(object): and k != 'defaults': data['defaults'][k] = data.pop(k) - data['history_modifier'] = self.history_modifier + if 'history_modifier' in \ + self.OBJECT_CLS._meta.get_all_field_names(): + data['history_modifier'] = self.history_modifier obj, created = self.get_object(self.OBJECT_CLS, data) + if created: + self.number_created += 1 + else: + self.number_updated += 1 + if not created and 'defaults' in data: for k in data['defaults']: setattr(obj, k, data['defaults'][k]) @@ -719,7 +738,9 @@ class Importer(object): type(data[attribute]) == dict: c_path.append(attribute) # put history_modifier for every created item - data[attribute]['history_modifier'] = \ + if 'history_modifier' in \ + field_object.rel.to._meta.get_all_field_names(): + data[attribute]['history_modifier'] = \ self.history_modifier data[attribute], created = self.get_object( field_object.rel.to, data[attribute], c_path) @@ -739,7 +760,6 @@ class Importer(object): defaults = {} if 'history_modifier' in create_dict: defaults = {'history_modifier':create_dict.pop('history_modifier')} - try: try: dct = create_dict.copy() |