Django

Code

Changeset 3599

Show
Ignore:
Timestamp:
08/17/06 13:28:41 (2 years ago)
Author:
utrebec
Message:

[full-history]
* Added CHANGE_TYPES choices
* Added "change_type" field
* Changed "db_table" to "django_history_log"
* Changed doc entry for _import_models()
* Fixed non-working saving of "Update" ChangeLog?
* Other minor stuff

Files:

Legend:

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

    r3596 r3599  
    1010#from django.utils.text import capfirst 
    1111 
     12CHANGE_TYPES = ( 
     13    ('A', 'Addition'), 
     14    ('U', 'Update'), 
     15    ('D', 'Deletion'), 
     16) 
     17 
    1218class ChangeLog(models.Model): 
    1319    change_time = models.DateTimeField (_('time of change'), auto_now=True) 
     
    1622    object_id = models.IntegerField(_('object ID')) 
    1723    user = models.ForeignKey(User, default="1") 
     24    change_type = models.CharField(maxlength=1, choices=CHANGE_TYPES) 
    1825    object = models.TextField() 
    1926    comment = models.CharField(maxlength=250, blank=True) 
     
    2532        verbose_name = _('changelog entry') 
    2633        verbose_name_plural = _('changelog entries') 
    27         db_table = _('history_changelog') 
     34        db_table = _('django_history_log') 
    2835         
    2936    class Admin: 
     37        date_hierarchy = 'change_time' 
     38        list_filter = ['change_time',  'change_type', 'content_type'] 
    3039        fields = ( 
    3140            ('Meta info', {'fields': ('change_time', 'content_type', 'object_id', 'user', 'comment'),}), 
     
    3342        ) 
    3443 
    35         list_display = ('__str__', 'user', 'comment', 'content_type', 'change_time', ) 
     44        list_display = ('__str__', 'user', 'change_type','comment', 'content_type', 'change_time', ) 
    3645         
    3746    def __str__(self): 
     
    8594 
    8695def _import_models(instance): 
    87     """ Returns a list of History-enabled models. """ 
     96    """  
     97    Checks for models that are history-enabled and imports the one of 
     98    which "instance" is an instance of. 
     99 
     100    Returns "True" if import went fine. 
     101    """ 
    88102    model_list = [] 
    89103    m = None 
     
    105119            except: 
    106120                print "Model import error." 
     121                return False 
    107122 
    108123    return m 
     
    119134    #instance_name = instance.__class__.__name__ 
    120135    #print instance_name 
    121     m = None  
     136    m = _import_models(instance) 
    122137    old = None 
    123138    log = None 
    124139     
    125     if _import_models(instance)
     140    if m
    126141        try: 
     142            print "Try" 
    127143            if kwargs['signal_name'] is 'pre_delete': 
     144                print "Instance was last revision." 
    128145                old = instance 
    129                 log = ChangeLog(parent=instance, comment="Object deleted. Last revision.") 
    130                 print "Log created."             
    131             elif kwargs['signal_name'] is 'pre_save' and instance.id: 
    132                 old = getattr(m, model['name']).objects.filter(pk=instance.id)[0] 
    133                 log = ChangeLog(parent=instance, comment="Update") 
     146                log = ChangeLog(parent=instance, change_type='D', comment="Object deleted. Last revision.") 
     147            elif ((kwargs['signal_name'] is 'pre_save') and instance.id): 
    134148                print "Instance has an ID." 
     149                old = getattr(m, instance.__class__.__name__).objects.filter(pk=instance.id)[0] 
     150                log = ChangeLog(parent=instance, change_type='U', comment="Update") 
    135151            else: 
    136                 print "Enter except." 
     152                print "Instance without an ID." 
    137153                old = instance 
    138154                instance.id = 0 # FIX: ID cannot be None 
    139                 log = ChangeLog(parent=instance, comment="New") 
    140                 print "Instance without an ID." 
     155                log = ChangeLog(parent=instance, change_type='A', comment="New") 
    141156        except: 
    142157            return 1 
     
    150165    print "Log: ",log.change_time 
    151166 
    152     log.object = Pickle.dumps(old, protocol=0) 
    153     log.save() 
     167    try:  
     168        log.object = Pickle.dumps(old, protocol=0) 
     169        log.save() 
     170        print "New change saved." 
     171    except: 
     172        print "ChangeLog faild to save changes." 
    154173 
    155     print "New change saved." 
    156174 
    157175dispatcher.connect( save_new_revision, signal=signals.pre_save )