summaryrefslogtreecommitdiff
path: root/ishtar_common
diff options
context:
space:
mode:
Diffstat (limited to 'ishtar_common')
-rw-r--r--ishtar_common/models.py38
-rw-r--r--ishtar_common/templatetags/window_field.py5
-rw-r--r--ishtar_common/utils.py14
3 files changed, 51 insertions, 6 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:
diff --git a/ishtar_common/templatetags/window_field.py b/ishtar_common/templatetags/window_field.py
index 3af9ed634..df72690c4 100644
--- a/ishtar_common/templatetags/window_field.py
+++ b/ishtar_common/templatetags/window_field.py
@@ -166,3 +166,8 @@ def field_flex_detail(context, caption, item, small=False):
if small:
size = 2
return field_detail(context, caption, item, size=size)
+
+
+@register.filter
+def m2m_listing(item, key):
+ return item.m2m_listing(key)
diff --git a/ishtar_common/utils.py b/ishtar_common/utils.py
index 94e71c655..56c6f669f 100644
--- a/ishtar_common/utils.py
+++ b/ishtar_common/utils.py
@@ -938,12 +938,22 @@ def m2m_historization_changed(sender, **kwargs):
obj = kwargs.get('instance', None)
if not obj:
return
+ hist_values = {}
for attr in obj.HISTORICAL_M2M:
values = []
for value in getattr(obj, attr).all():
if not hasattr(value, "history_compress"):
continue
values.append(value.history_compress())
- setattr(obj, 'historical_' + attr, HISTORY_M2M_SPLIT.join(values))
- obj.skip_history_when_saving = True
+ hist_values['historical_' + attr] = HISTORY_M2M_SPLIT.join(values)
+ for key in hist_values:
+ setattr(obj, key, hist_values[key])
+ # force resave of last history record
+ if hasattr(obj, 'skip_history_when_saving'):
+ delattr(obj, 'skip_history_when_saving')
+ obj._force_history = True
+ q = obj.history.order_by("-history_date")
+ if q.count():
+ last = q.all()[0]
+ last.delete()
obj.save()