Commits

Ivan Zakrevsky committed 78953fb

Fixed for new objects

Comments (0)

Files changed (2)

versioning/__init__.py

     """
     from django.db import models
     from django.db.models import signals as model_signals
-    from versioning.signals import pre_save
+    from versioning.signals import pre_save, post_save
 
     opts = model._meta
 
     _registry[model] = fields
 
     model_signals.pre_save.connect(pre_save, sender=model)
+    model_signals.post_save.connect(post_save, sender=model)

versioning/signals.py

 
 
 def pre_save(instance, **kwargs):
-    """Pre-save signal handler
+    """
+    Pre-save signal handler
     """
     model = kwargs["sender"]
+    if not hasattr(instance, 'revision_info'):
+        instance.revision_info = {}
+    info = instance.revision_info
+
     try:
         original = model._default_manager.get(pk=instance.pk)
     except model.DoesNotExist:
         original = model()
-    delta = obj_diff(instance, original)
-    info = getattr(instance, 'revision_info', {})
+    info['delta'] = obj_diff(instance, original)
     request = get_request()
     if request:
         if not info.get('editor'):
             info['editor_ip'] = request.META.get("REMOTE_ADDR")
     if not hasattr(info, 'editor') or not getattr(info['editor'], 'pk', None):
         info['editor'] = None
-    rev = Revision(delta=delta, **info)
-    rev.content_object = instance
-    rev.save()
+
+
+def post_save(instance, **kwargs):
+    """
+    Post-save signal handler
+    """
+    info = getattr(instance, 'revision_info', {})
+    if info:
+        rev = Revision(**info)
+        rev.content_object = instance
+        rev.save()