diff options
Diffstat (limited to 'ishtar/furnitures/models.py')
| -rw-r--r-- | ishtar/furnitures/models.py | 35 | 
1 files changed, 34 insertions, 1 deletions
| diff --git a/ishtar/furnitures/models.py b/ishtar/furnitures/models.py index 3571256cf..2fe65b598 100644 --- a/ishtar/furnitures/models.py +++ b/ishtar/furnitures/models.py @@ -174,7 +174,40 @@ class BaseHistorizedItem(models.Model):          super(BaseHistorizedItem, self).save(*args, **kwargs)          return True +    def get_previous(self, step, strict=True): +        """ +        Get a "step" previous state of the item +        """ +        historized = self.history.all() +        assert len(historized) > step +        assert not hasattr(historized, '_step') +        item = historized[step] +        item._step = step +        model = self.__class__ +        for k in model._meta.get_all_field_names(): +            field = model._meta.get_field_by_name(k)[0] +            if hasattr(field, 'rel') and field.rel: +                if not hasattr(item, k+'_id'): +                    setattr(item, k, getattr(self, k)) +                    continue +                val = getattr(item, k+'_id') +                if not val: +                    setattr(item, k, None) +                    continue +                try: +                    val = field.rel.to.objects.get(pk=val) +                    setattr(item, k, val) +                except ObjectDoesNotExist: +                    if strict: +                        raise HistoryError(u"The class %s has no pk %d" % ( +                                           unicode(field.rel.to), val)) +                    setattr(item, k, None) +        return item +      def rollback(self, date): +        """ +        Rollback to a previous state +        """          to_del, new_item = [], None          for item in self.history.all():              to_del.append(item) @@ -193,7 +226,7 @@ class BaseHistorizedItem(models.Model):              self.save()          except:              raise HistoryError(u"The rollback has failed.") -        # clean the non necessary steps +        # clean the obsolete history          for historized_item in to_del:              historized_item.delete() | 
