Django

Code

Changeset 3579

Show
Ignore:
Timestamp:
08/13/06 19:10:10 (2 years ago)
Author:
utrebec
Message:

[full-history]
* Added str + changed Admin list_display to use it
* Added extra checking for History-enabled
* Added extra checking of instance "type" (new/update) - Fixed saving
* Using signals.pre_save again (if instance is "new" then it uses ID=0)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/full-history/django/contrib/history/models.py

    r3514 r3579  
    1212class ChangeLog(models.Model): 
    1313    change_time = models.DateTimeField (_('time of change'), auto_now=True) 
    14  
    1514    content_type = models.ForeignKey(ContentType) 
    1615    parent = models.GenericForeignKey() 
    1716    object_id = models.IntegerField(_('object ID')) 
    18  
    1917    user = models.ForeignKey(User, default="1") 
    2018    object = models.TextField() 
    21     comment = models.CharField(maxlength=250, default="Bla"
     19    comment = models.CharField(maxlength=250, blank=True
    2220 
    2321    #object_type = models.CharField(maxlength=50) 
     
    3533        ) 
    3634 
    37         list_display = ('object_id', 'user', 'comment', 'content_type', 'change_time', ) 
     35        list_display = ('__str__', 'user', 'comment', 'content_type', 'change_time', ) 
     36         
     37    def __str__(self): 
     38        return str(self.get_object()) 
    3839 
    3940    def get_object(self): 
     
    99100    print "Sender: ",sender 
    100101 
    101     if not hasattr(instance, 'History'):  
     102    if instance.__class__.__name__ is 'ChangeLog' or not hasattr(instance, 'History'):  
    102103        print "Not history-enabled class." 
    103104        return 0 
     
    114115                m = __import__(model['module'], '', '', [model['name']]) 
    115116                #print model['module'],": ",model['name'],"- ",m 
     117                print "Model import done: ",m 
    116118            except: 
    117119                print "Model import error." 
     
    119121    if m: 
    120122        try: 
    121             old = getattr(m, model['name']).objects.filter(pk=instance.id) 
    122             log = ChangeLog(parent=instance, comment="Update") 
     123            if instance.id: 
     124                old = getattr(m, model['name']).objects.filter(pk=instance.id)[0] 
     125                log = ChangeLog(parent=instance, comment="Update") 
     126                print "Instance has an ID." 
     127            else: 
     128                print "Enter except." 
     129                old = instance 
     130                instance.id = 0 # FIX: ID cannot be None 
     131                log = ChangeLog(parent=instance, comment="New") 
     132                print "Instance without an ID." 
    123133        except: 
    124             old = instance 
    125             log = ChangeLog(parent=instance, comment="New") 
     134            return 1 
    126135    else: 
    127         return 0 
    128      
     136        return 0  # exit wo/ an action 
     137 
     138 
    129139    print "Old: ",old 
    130140    print "Instance: ",instance.id 
     141    #print "Test: ",getattr(instance, 'Admin').date_hierarchy 
    131142    print "Log: ",log 
    132     log.object = Pickle.dumps(old[0], protocol=0) 
     143    log.object = Pickle.dumps(old, protocol=0) 
    133144    log.save() 
    134145    print "New change saved." 
    135146     
    136147 
    137 dispatcher.connect( save_new_revision, signal=signals.post_save ) 
     148dispatcher.connect( save_new_revision, signal=signals.pre_save )