summaryrefslogtreecommitdiff
path: root/ishtar/furnitures
diff options
context:
space:
mode:
Diffstat (limited to 'ishtar/furnitures')
-rw-r--r--ishtar/furnitures/forms.py2
-rw-r--r--ishtar/furnitures/models.py21
-rw-r--r--ishtar/furnitures/tests.py12
-rw-r--r--ishtar/furnitures/views.py4
4 files changed, 37 insertions, 2 deletions
diff --git a/ishtar/furnitures/forms.py b/ishtar/furnitures/forms.py
index 43fe83c58..bf01fb501 100644
--- a/ishtar/furnitures/forms.py
+++ b/ishtar/furnitures/forms.py
@@ -381,6 +381,8 @@ class Wizard(NamedUrlSessionFormWizard):
m2m_items[key] = getattr(obj, key+'s').all()
if value not in m2m_items[key]:
if type(value) == dict:
+ if issubclass(obj.__class__, models.BaseHistorizedItem):
+ value['history_modifier'] = request.user
value = getattr(obj, key+'s').model.objects.create(**value)
value.save()
getattr(obj, key+'s').add(value)
diff --git a/ishtar/furnitures/models.py b/ishtar/furnitures/models.py
index de519076f..9d1873770 100644
--- a/ishtar/furnitures/models.py
+++ b/ishtar/furnitures/models.py
@@ -33,10 +33,27 @@ from django.contrib.auth.models import User
from django.contrib.gis.db import models
from django.contrib import admin
-from simple_history.models import HistoricalRecords
+from simple_history.models import HistoricalRecords as BaseHistoricalRecords
from ishtar import settings
+# HistoricalRecords enhancement: don't save identical versions
+class HistoricalRecords(BaseHistoricalRecords):
+ def create_historical_record(self, instance, type):
+ manager = getattr(instance, self.manager_name)
+ attrs = {}
+ for field in instance._meta.fields:
+ attrs[field.attname] = getattr(instance, field.attname)
+ history = instance.history.all()
+ if not history:
+ manager.create(history_type=type, **attrs)
+ return
+ old_instance = history[0]
+ for field in instance._meta.fields:
+ if getattr(old_instance, field.attname) != attrs[field.attname]:
+ manager.create(history_type=type, **attrs)
+ return
+
# valid ID validator for models
def valid_id(cls):
def func(value):
@@ -200,6 +217,7 @@ class BaseHistorizedItem(models.Model):
item._next = historized[step - 1].history_date
else:
item._next = None
+ item.history_date = historized[step].history_date
model = self.__class__
for k in model._meta.get_all_field_names():
field = model._meta.get_field_by_name(k)[0]
@@ -219,6 +237,7 @@ class BaseHistorizedItem(models.Model):
raise HistoryError(u"The class %s has no pk %d" % (
unicode(field.rel.to), val))
setattr(item, k, None)
+ item.pk = self.pk
return item
def rollback(self, date):
diff --git a/ishtar/furnitures/tests.py b/ishtar/furnitures/tests.py
index 5ffca400a..5a433b381 100644
--- a/ishtar/furnitures/tests.py
+++ b/ishtar/furnitures/tests.py
@@ -76,6 +76,18 @@ class FileTest(TestCase):
self.failUnlessEqual(self.item.history.all()[1].internal_reference,
base_label)
+ def testIntelligentHistorisation(self):
+ """
+ Test that to identical version are not recorded twice in the history
+ """
+ nb_hist = self.item.history.count()
+ self.item.internal_reference = u"Unité_Test"
+ self.item.save()
+ self.failUnlessEqual(self.item.history.count(), nb_hist+1)
+ nb_hist = self.item.history.count()
+ self.item.save()
+ self.failUnlessEqual(self.item.history.count(), nb_hist)
+
def testRollbackFile(self):
nb_hist = self.item.history.count()
initial_values = self.item.values()
diff --git a/ishtar/furnitures/views.py b/ishtar/furnitures/views.py
index 43e663267..8dea8cb2f 100644
--- a/ishtar/furnitures/views.py
+++ b/ishtar/furnitures/views.py
@@ -285,7 +285,6 @@ def show_item(model, name):
return HttpResponse(None)
doc_type = 'type' in dct and dct.pop('type')
date = 'date' in dct and dct.pop('date')
- dct['item'], dct['item_name'] = item, name
dct['window_id'] = "%s-%d-%s" % (name, item.pk,
datetime.datetime.now().strftime('%M%s'))
if date:
@@ -299,8 +298,11 @@ def show_item(model, name):
dct['next'] = item._next
else:
historized = item.history.all()
+ if historized:
+ item.history_date = historized[0].history_date
if len(historized) > 1:
dct['previous'] = historized[1].history_date
+ dct['item'], dct['item_name'] = item, name
context_instance = RequestContext(request)
context_instance.update(dct)
n = datetime.datetime.now()