diff options
Diffstat (limited to 'ishtar_common/models.py')
-rw-r--r-- | ishtar_common/models.py | 38 |
1 files changed, 34 insertions, 4 deletions
diff --git a/ishtar_common/models.py b/ishtar_common/models.py index c7223c898..e5e173654 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -148,6 +148,18 @@ class ValueGetter(object): return values +class HistoryModel(models.Model): + class Meta: + abstract = True + + def m2m_listing(self, key): + hist_value = getattr(self, "historical_" + key, None) + if not hist_value: + return + related_model = getattr(self, key).model + return related_model.history_decompress(hist_value) + + class HistoricalRecords(BaseHistoricalRecords): def create_historical_record(self, instance, type): try: @@ -523,9 +535,9 @@ class GeneralType(Cached, models.Model): return self.txt_idx @classmethod - def history_decompress(cls, value=""): + def history_decompress(cls, value, create=False): if not value: - value = "" + return [] res = [] for txt_idx in value.split(HISTORY_M2M_SPLIT): try: @@ -1571,12 +1583,27 @@ class BaseHistorizedItem(DocumentItem, FullSearch, Imported, JsonData, if not new_item: raise HistoryError(u"The date to rollback to doesn't exist.") try: - for f in self._meta.fields: - k = f.name + field_keys = [f.name for f in self._meta.fields] + for k in field_keys: if k != 'id' and hasattr(self, k): if not hasattr(new_item, k): k = k + "_id" setattr(self, k, getattr(new_item, k)) + + # M2M history + if k.startwith('historical_') \ + and k[len('historical_'):] in field_keys: + value = getattr(new_item, k) + if not value: + continue + base_k = k[len('historical_'):] + base_field = self._meta.get_field(base_k) + base_field.clear() + new_values = base_field.related_model.history_decompress( + value, create=True + ) + for val in new_values: + base_field.add(val) try: self.history_modifier = User.objects.get( pk=new_item.history_modifier_id) @@ -1591,6 +1618,9 @@ class BaseHistorizedItem(DocumentItem, FullSearch, Imported, JsonData, for historized_item in to_del: historized_item.delete() + def m2m_listing(self, key): + return getattr(self, key).all() + def values(self): values = {} for f in self._meta.fields: |