diff options
| author | Étienne Loks <etienne.loks@peacefrogs.net> | 2013-09-08 13:06:21 +0200 | 
|---|---|---|
| committer | Étienne Loks <etienne.loks@peacefrogs.net> | 2013-09-08 13:06:21 +0200 | 
| commit | 4060c688056caee0b64dd6513f47e20d285db463 (patch) | |
| tree | cce2bc0020e9cb1dad6258f58290b4d067e1b9c6 /ishtar_common/models.py | |
| parent | 3674db3c0db88d5d4d60a6f8ff9a06d7f5523bd0 (diff) | |
| download | Ishtar-4060c688056caee0b64dd6513f47e20d285db463.tar.bz2 Ishtar-4060c688056caee0b64dd6513f47e20d285db463.zip | |
Prevent multiple hostorization when there are not necessary (refs #1323)
Diffstat (limited to 'ishtar_common/models.py')
| -rw-r--r-- | ishtar_common/models.py | 24 | 
1 files changed, 18 insertions, 6 deletions
| 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 | 
