Correction de bug, ajout de la methode diff_with
authorcyril robert <Cyril Robert crobert@inverse.ca>
Tue, 2 Feb 2010 21:05:12 +0000 (16:05 -0500)
committercyril robert <Cyril Robert crobert@inverse.ca>
Tue, 2 Feb 2010 21:05:12 +0000 (16:05 -0500)
django_historique/models.py

index f37c3d1..55a2879 100644 (file)
@@ -22,30 +22,63 @@ class History (models.Model):
 
     def save_log (self, instance):
         "Enregistrement de l'instance courante avant l'ecriture de la nouvelle"
-        iClass = instance.__class__
-        hClass = self.__class__
+        if instance.id:
+            iClass = instance.__class__
+            hClass = self.__class__
 
-        instance = iClass.objects.get(id = instance.id)
-        self.history_objectid = instance.id
-        self.history_comment = "pre_save history item <%s>" \
-                % (repr(instance))
-        self.history_revision = hClass.objects.filter(history_objectid=instance.id).count()+1
+            instance = iClass.objects.get(id = instance.id)
+            self.history_objectid = instance.id
+            self.history_comment = "pre_save history item <%s>" \
+                    % (repr(instance))
+            self.history_revision = hClass.objects.filter(history_objectid=instance.id).count()+1
+    
+            for field in instance._meta.fields:
+                if field.__class__.__name__ != 'AutoField':
+                    if getattr(field, 'unique', False):
+                        field._unique = False
+                    setattr(self, field.name, getattr(instance, field.name))
 
-        for field in instance._meta.fields:
-            if field.__class__.__name__ != 'AutoField':
-                if getattr(field, 'unique', False):
-                    field._unique = False
-                setattr(self, field.name, getattr(instance, field.name))
-
-        #log_instance (instance, "I")
-        #log_instance (self, "S")
+            #log_instance (instance, "I")
+            #log_instance (self, "S")
 
-        #self.save ()
-        super (History, self).save ()
+            #self.save ()
+            super (History, self).save ()
 
     def save (self):
         pass
 
+    def diff_with (self, instance, ignore = None, \
+            choice_value = True, verbose_names = True):
+        ignored = ["history_datetime", "history_objectid", 
+                "history_revision", "history_comment"]
+        if ignore:
+            ignored.extend (ignore)
+        diffs = []
+        for field in instance._meta.fields:
+            if field.__class__.__name__ != 'AutoField' \
+                    and field.name not in ignored:
+                hvalue = getattr (self, field.name, "")
+                if hvalue is None:
+                    hvalue = ""
+                ivalue = getattr (instance, field.name, "")
+                if ivalue is None:
+                    ivalue = ""
+                if ivalue != hvalue:
+                    if choice_value and len (field.choices) > 0:
+                        for pair in field.choices:
+                            if ivalue == pair[0]: ivalue = pair[1]
+                            if hvalue == pair[0]: hvalue = pair[1]
+                    
+                    name = field.name
+                    if verbose_names:
+                        name = field.verbose_name
+
+                    diffs.append({"name": name, \
+                                  "previous": hvalue, \
+                                  "updated": ivalue, \
+                                  "date": getattr(self, 'history_datetime')})
+        return diffs
+
     class Meta:
         abstract = True