diff options
-rw-r--r-- | archaeological_files/tests.py | 10 | ||||
-rw-r--r-- | ishtar_common/models.py | 24 |
2 files changed, 24 insertions, 10 deletions
diff --git a/archaeological_files/tests.py b/archaeological_files/tests.py index 5a433b381..8cd53d946 100644 --- a/archaeological_files/tests.py +++ b/archaeological_files/tests.py @@ -1,6 +1,6 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -# Copyright (C) 2010-2011 Étienne Loks <etienne.loks_AT_peacefrogsDOTnet> +# Copyright (C) 2010-2013 Étienne Loks <etienne.loks_AT_peacefrogsDOTnet> # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as @@ -22,17 +22,18 @@ Unit tests """ import json +from django.conf import settings from django.test import TestCase import models class FileTest(TestCase): - fixtures = ['user.json', 'person_type-fr.json', 'organization_type-fr.json', - 'treatment_type-fr.json'] + fixtures = [settings.ROOT_PATH + 'ishtar_common/fixtures/test-users.json'] model = models.File def setUp(self): self.extra_models, self.model_list = {}, [] + user = models.IshtarUser.objects.get(pk=1) person_type = models.PersonType(label=u'Test person type', txt_idx='test_person', available=True) @@ -78,7 +79,8 @@ class FileTest(TestCase): def testIntelligentHistorisation(self): """ - Test that to identical version are not recorded twice in the history + Test that two identical version are not recorded twice in the history + and that multiple saving in a short time are not considered """ nb_hist = self.item.history.count() self.item.internal_reference = u"Unité_Test" diff --git a/ishtar_common/models.py b/ishtar_common/models.py index e1ca73a3b..6eca7ddd7 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -57,21 +57,33 @@ def post_save_user(sender, **kwargs): txt_idx='administrator')) post_save.connect(post_save_user, sender=User) -# HistoricalRecords enhancement: don't save identical versions class HistoricalRecords(BaseHistoricalRecords): def create_historical_record(self, instance, type): + history_user = getattr(instance, '_history_user', None) 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) + q_history = instance.history.order_by('-history_date', + '-history_id') + if not q_history.count(): + manager.create(history_type=type, history_user=history_user, + **attrs) return - old_instance = history[0] + old_instance = q_history.all()[0] + # multiple saving by the same user in a very short time are generaly + # caused by post_save signals it is not relevant to keep them + history_date = datetime.datetime.now() + if old_instance.history_date and \ + history_date - old_instance.history_date \ + < datetime.timedelta(seconds=20): + return + + # record a new version only if data have been changed for field in instance._meta.fields: if getattr(old_instance, field.attname) != attrs[field.attname]: - manager.create(history_type=type, **attrs) + manager.create(history_type=type, history_user=history_user, + **attrs) return # valid ID validator for models |