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 ++++++++++++---------- archaeological_finds/models_finds.py | 16 ++------ .../templates/ishtar/sheet_find.html | 10 ++--- archaeological_finds/tests.py | 39 ++++++++++---------- ishtar_common/models.py | 38 +++++++++++++++++-- ishtar_common/templatetags/window_field.py | 5 +++ ishtar_common/utils.py | 14 ++++++- 7 files changed, 102 insertions(+), 63 deletions(-) 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): diff --git a/archaeological_finds/models_finds.py b/archaeological_finds/models_finds.py index f8cc34e61..9e377d1f6 100644 --- a/archaeological_finds/models_finds.py +++ b/archaeological_finds/models_finds.py @@ -32,7 +32,7 @@ from django.utils.translation import ugettext_lazy as _, pgettext_lazy, \ from ishtar_common.data_importer import post_importer_action, ImporterError from ishtar_common.utils import cached_label_changed, post_save_point, \ - m2m_historization_changed, HISTORY_M2M_SPLIT + m2m_historization_changed from ishtar_common.alternative_configs import ALTERNATE_CONFIGS @@ -41,7 +41,7 @@ from ishtar_common.models import Document, GeneralType, \ HistoricalRecords, OwnPerms, Person, Basket, post_save_cache, \ ValueGetter, get_current_profile, IshtarSiteProfile, PRIVATE_FIELDS, \ SpatialReferenceSystem, BulkUpdatedItem, ExternalIdManager, QuickAction, \ - MainItem, document_attached_changed + MainItem, document_attached_changed, HistoryModel from archaeological_operations.models import AdministrativeAct, Operation from archaeological_context_records.models import ContextRecord, Dating @@ -1199,7 +1199,7 @@ class Find(BulkUpdatedItem, ValueGetter, BaseHistorizedItem, OwnPerms, help_text=_(u"Related treatments when no new find is created")) cached_label = models.TextField(_(u"Cached name"), null=True, blank=True, db_index=True) - history = HistoricalRecords() + history = HistoricalRecords(bases=[HistoryModel]) BASKET_MODEL = FindBasket class Meta: @@ -1232,16 +1232,6 @@ class Find(BulkUpdatedItem, ValueGetter, BaseHistorizedItem, OwnPerms, def dating(self): return u" ; ".join([unicode(dating) for dating in self.datings.all()]) - @property - def dating_list(self): - if self.historical_datings: - return [ - Dating.history_decompress(v) - for v in self.historical_datings.split(HISTORY_M2M_SPLIT) - ] - else: - return self.datings.all() - @property def excavation_ids(self): return u" - ".join( diff --git a/archaeological_finds/templates/ishtar/sheet_find.html b/archaeological_finds/templates/ishtar/sheet_find.html index 2d234b2d9..ac2df607d 100644 --- a/archaeological_finds/templates/ishtar/sheet_find.html +++ b/archaeological_finds/templates/ishtar/sheet_find.html @@ -19,7 +19,7 @@ {% with permission_view_document=permission_view_document %} {% with permission_view_own_document=permission_view_own_document %} -{% with display_identification=item.integrities.count|or_:item.remarkabilities.count|or_:item.conservatory_state|or_:item.conservatory_comment|or_:item.alterations.count|or_:item.alteration_causes.count|or_:item.preservation_to_considers.count|or_:item.appraisal_date|or_:item.treatment_emergency|or_:item.insurance_value|or_:item.estimated_value|or_:item.datings.count|or_:item.dating_comment %} +{% with display_datings=item.integrities.count|or_:item.remarkabilities.count|or_:item.conservatory_state|or_:item.conservatory_comment|or_:item.alterations.count|or_:item.alteration_causes.count|or_:item.preservation_to_considers.count|or_:item.appraisal_date|or_:item.treatment_emergency|or_:item.insurance_value|or_:item.estimated_value|or_:item.historical_datings|or_:item.datings.count|or_:item.dating_comment %} {% with display_warehouse_treatments=item.container|or_:item.container_ref|or_:item.upstream_treatment|or_:item.downstream_treatment|or_:item.treatments.count %} {% with can_view_documents=permission_view_own_document|or_:permission_view_document %} {% with display_documents=can_view_documents|and_:item.documents.count %} @@ -39,7 +39,7 @@ {% trans "Identification / Description / Dimensions" %} - {% if display_identification %} + {% if display_datings %}