from __future__ import absolute_import, unicode_literals

from django.core import serializers
from django.db import connection
from django.db.models.signals import pre_save, post_save

from .helpers import precise_timestamp

from .settings import diffs_settings


def on_pre_save(sender, instance, **kwargs):
    instance.__dirty_fields = list(instance.get_dirty_fields().keys())


def on_post_save(sender, instance, created, **kwargs):
    if instance.__dirty_fields:
        # get the data
        if hasattr(instance, 'serialize_diff'):
            data = instance.serialize_diff(instance.__dirty_fields)
        else:
            data = serialize_object(instance, instance.__dirty_fields)

        model = instance
        # check if should be related to another "parent" model
        if hasattr(instance, 'get_diff_parent'):
            parent = instance.get_diff_parent()
            if parent:
                model = parent

        create_kwargs = {
            'data': data,
            'created': created,
            'pk': model.id,
            'model_cls': model.__class__,
            'timestamp': precise_timestamp()
        }
        # Respect the transaction if we can and should.
        if hasattr(connection, 'on_commit') and diffs_settings['use_transactions']:
            connection.on_commit(lambda: sender.diffs.create(**create_kwargs))
        else:
            sender.diffs.create(**create_kwargs)

        del instance.__dirty_fields


def serialize_object(instance, dirty_fields):
    """Serializes a django model using the default serialization."""
    return serializers.serialize('json', [instance], fields=dirty_fields)


def connect(cls):
    pre_save.connect(on_pre_save, cls)
    post_save.connect(on_post_save, cls)

@@ -49,16 +49,31 @@
 literals
+%0Aimport logging
 %0A%0Afrom d
@@ -271,16 +271,53 @@
 ttings%0A%0A
+logger = logging.getLogger(%22diffs%22)%0A%0A
 %0Adef on_
@@ -735,24 +735,45 @@
 ty_fields)%0A%0A
+        if data:%0A    
         mode
@@ -785,16 +785,20 @@
 nstance%0A
+    
         
@@ -852,32 +852,36 @@
 %22 model%0A        
+    
 if hasattr(insta
@@ -901,24 +901,28 @@
 f_parent'):%0A
+    
             
@@ -969,16 +969,20 @@
         
+    
 if paren
@@ -984,16 +984,20 @@
 parent:%0A
+    
         
@@ -1028,16 +1028,20 @@
         
+ 
+   
 create_k
@@ -1062,16 +1062,20 @@
         
+    
 'data': 
@@ -1092,16 +1092,20 @@
         
+ 
+   
 'created
@@ -1128,16 +1128,20 @@
         
+    
 'pk': mo
@@ -1160,16 +1160,20 @@
         
+ 
+   
 'model_c
@@ -1194,16 +1194,20 @@
 lass__,%0A
+    
         
@@ -1255,10 +1255,18 @@
     
+ 
-%7D%0A
+   %7D%0A    
     
@@ -1317,32 +1317,36 @@
 should.%0A        
+  
+  
 if hasattr(conne
@@ -1417,16 +1417,20 @@
         
+    
 connecti
@@ -1488,38 +1488,46 @@
 wargs))%0A        
+  
+  
 else:%0A
+    
             send
@@ -1563,44 +1563,217 @@
 gs)%0A
-%0A        del instance.__dirty_fields
+        else:%0A            logger.debug(%22Skipped diff because it was emtpy.%22)%0A        # clean up%0A        del instance.__dirty_fields%0A    else:%0A        logger.debug(%22Skipped diff because no fields had changed.%22)
 %0A%0A%0Ad

Add logic to skip diff when appropriate