From 0801fd75242e769ec856f4e7e94ee57b1dc945d2 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Fri, 11 Jan 2019 14:01:02 +0100 Subject: Fix history M2M save --- archaeological_context_records/models.py | 43 ++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 19 deletions(-) (limited to 'archaeological_context_records') diff --git a/archaeological_context_records/models.py b/archaeological_context_records/models.py index 5ba631dba..030f817d9 100644 --- a/archaeological_context_records/models.py +++ b/archaeological_context_records/models.py @@ -29,7 +29,7 @@ from django.utils.translation import ugettext_lazy as _, pgettext, \ activate, pgettext_lazy, deactivate from django.utils.text import slugify -from ishtar_common.utils import cached_label_changed +from ishtar_common.utils import cached_label_changed, HISTORY_M2M_SPLIT from ishtar_common.models import Document, GeneralType, \ BaseHistorizedItem, HistoricalRecords, OwnPerms, ShortMenuItem, \ @@ -104,25 +104,30 @@ class Dating(models.Model): return self.SEP.join(values) @classmethod - def history_decompress(cls, value): - if not value: - value = "" - res = {} - for idx, val in enumerate(value.split(cls.SEP)): - key = cls.HISTORY_ATTR[idx] - if val == '': - val = None - elif key in ("period", "dating_type", "quality"): - field = cls._meta.get_field(key) - q = field.related_model.objects.filter(txt_idx=val) - if q.count(): - val = q.all()[0] - else: # do not exist anymore in db + def history_decompress(cls, full_value, create=False): + if not full_value: + return [] + full_res = [] + for value in full_value.split(HISTORY_M2M_SPLIT): + res = {} + for idx, val in enumerate(value.split(cls.SEP)): + key = cls.HISTORY_ATTR[idx] + if val == '': val = None - elif key in ("start_date", "end_date"): - val = int(val) - res[key] = val - return res + elif key in ("period", "dating_type", "quality"): + field = cls._meta.get_field(key) + q = field.related_model.objects.filter(txt_idx=val) + if q.count(): + val = q.all()[0] + else: # do not exist anymore in db + val = None + elif key in ("start_date", "end_date"): + val = int(val) + res[key] = val + if create: + res = cls.objects.create(**res) + full_res.append(res) + return full_res @classmethod def is_identical(cls, dating_1, dating_2): -- cgit v1.2.3