Author: utrebec
Date: 2006-08-14 18:28:23 -0500 (Mon, 14 Aug 2006)
New Revision: 3588

Modified:
   django/branches/full-history/django/contrib/history/models.py
Log:
[full-history]
* Added save_last_revision() for when the record is deleted.
* Renamed _get_enabled_models() to _import_models()
* Moved the "import" stuff to _import_models() so it can be used everywhere
* Added "pre-delete signal" connect
* BUG: save_new_revision() stopped working (EOFError on line 42 - get_object() 
function).



Modified: django/branches/full-history/django/contrib/history/models.py
===================================================================
--- django/branches/full-history/django/contrib/history/models.py       
2006-08-14 23:21:10 UTC (rev 3587)
+++ django/branches/full-history/django/contrib/history/models.py       
2006-08-14 23:28:23 UTC (rev 3588)
@@ -83,9 +83,11 @@
 # Pre-save signal catch #
 #########################
 
-def _get_enabled_models():
+def _import_models(instance):
     """ Returns a list of History-enabled models. """
     model_list = []
+    m = None
+
     for model in models.get_models():
        try:
            if model.History:
@@ -93,8 +95,18 @@
                                   'name': model.__name__})
        except:
            pass
-    return model_list
 
+    for model in model_list:
+       if model['name'] is instance.__class__.__name__:
+           try:
+               m = __import__(model['module'], '', '', [model['name']])
+               #print model['module'],": ",model['name'],"- ",m
+               print "Model import done: ",m
+           except:
+               print "Model import error."
+
+    return m
+
 def save_new_revision(sender, instance, signal, *args, **kwargs):
     """ Saves a old copy of the record into the History table."""
     print "Sender: ",sender
@@ -108,17 +120,8 @@
     m = None 
     old = None
     log = None
-
-    for model in _get_enabled_models():
-       if model['name'] is instance.__class__.__name__:
-           try:
-               m = __import__(model['module'], '', '', [model['name']])
-               #print model['module'],": ",model['name'],"- ",m
-               print "Model import done: ",m
-           except:
-               print "Model import error."
     
-    if m:
+    if _import_models(instance):
        try:
            if instance.id:
                old = getattr(m, 
model['name']).objects.filter(pk=instance.id)[0]
@@ -135,14 +138,50 @@
     else:
        return 0  # exit wo/ an action
 
-
+    # DEBUG
     print "Old: ",old
     print "Instance: ",instance.id
     #print "Test: ",getattr(instance, 'Admin').date_hierarchy
     print "Log: ",log
+
     log.object = Pickle.dumps(old, protocol=0)
     log.save()
+
     print "New change saved."
+
+dispatcher.connect( save_new_revision, signal=signals.pre_save )
     
+###########################
+# Pre-delete signal catch #
+###########################
 
-dispatcher.connect( save_new_revision, signal=signals.pre_save )
+def save_last_revision(sender, instance, signal, *args, **kwargs):
+    """ Saves the last copy of the record when the record is deleted."""
+    print "Sender: ",sender
+
+    if instance.__class__.__name__ is 'ChangeLog' or not hasattr(instance, 
'History'): 
+       print "Not history-enabled class."
+       return 0
+
+    #instance_name = instance.__class__.__name__
+    #print instance_name
+    m = None 
+    old = None
+    log = None
+    
+    if _import_models(instance):
+       try:
+           old = instance
+           log = ChangeLog(parent=instance, comment="Object deleted. Last 
revision.")
+           print "Log created."
+       except:
+           return 1
+
+    try:
+       log.object = Pickle.dumps(old, protocol=0)
+       log.save()
+       print "Last change saved."
+    except:
+       print "Failed!"
+
+dispatcher.connect( save_last_revision, signal=signals.pre_delete )


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates
-~----------~----~----~----~------~----~------~--~---

Reply via email to