diff options
| -rw-r--r-- | ishtar/furnitures/forms.py | 2 | ||||
| -rw-r--r-- | ishtar/furnitures/models.py | 21 | ||||
| -rw-r--r-- | ishtar/furnitures/tests.py | 12 | ||||
| -rw-r--r-- | ishtar/furnitures/views.py | 4 | ||||
| -rw-r--r-- | ishtar/templates/sheet_file.html | 5 | ||||
| -rw-r--r-- | static/js/ishtar.js | 4 | 
6 files changed, 42 insertions, 6 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() diff --git a/ishtar/templates/sheet_file.html b/ishtar/templates/sheet_file.html index b161a8e22..bf74490d5 100644 --- a/ishtar/templates/sheet_file.html +++ b/ishtar/templates/sheet_file.html @@ -6,8 +6,9 @@  {%if previous%}  <a href="#" onclick='load_window("{% url show-historized-file item.pk previous %}");$("#{{window_id}}").hide();return false;'>{%trans "Previous version"%} ({{previous}})</a>  {% endif %} +{% if previous and next %} - {% endif %}  {%if next%} - - <a href="#" onclick='load_window("{% url show-historized-file item.pk next %}");$("#{{window_id}}").hide();return false;'>{%trans "Next version"%} ({{next}})</a> +<a href="#" onclick='load_window("{% url show-historized-file item.pk next %}");$("#{{window_id}}").hide();return false;'>{%trans "Next version"%} ({{next}})</a>  {% endif %}  </div>  {% endif %} @@ -18,7 +19,7 @@  <p><label>{%trans "File's name:"%}</label> <span class='value'>{{ item.internal_reference }}</span></p> -<p><label>{%trans "Edition date:"%}</label> <span class='value'>{{ item.history.all.0.history_date }}</span></p> <!-- date = now --> +<p><label>{%trans "Edition date:"%}</label> <span class='value'>{{ item.history_date }}</span></p> <!-- date = now -->  {% if item.reception_date %}<p><label>{%trans "Reception date:"%}</label> <span class='value'>{{ item.reception_date }}</span></p>{% endif %}  <p><label>{%trans "Creation date:"%}</label> <span class='value'>{{ item.creation_date }}</span></p>  {% comment %} diff --git a/static/js/ishtar.js b/static/js/ishtar.js index e52ba69db..f345cf030 100644 --- a/static/js/ishtar.js +++ b/static/js/ishtar.js @@ -47,13 +47,13 @@ $("#main_menu ul li").live('click', function(){  var last_window; -function load_window(url){ +function load_window(url, speed){      $.ajax({          url: url,          cache: false,          success:function(html){              $("#window").append(html); -            $("#"+last_window).show('slow'); +            $("#"+last_window).show();          },          error:function(XMLHttpRequest, textStatus, errorThrows){          }  | 
