Changeset 3599
- Timestamp:
- 08/17/06 13:28:41 (2 years ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/full-history/django/contrib/history/models.py
r3596 r3599 10 10 #from django.utils.text import capfirst 11 11 12 CHANGE_TYPES = ( 13 ('A', 'Addition'), 14 ('U', 'Update'), 15 ('D', 'Deletion'), 16 ) 17 12 18 class ChangeLog(models.Model): 13 19 change_time = models.DateTimeField (_('time of change'), auto_now=True) … … 16 22 object_id = models.IntegerField(_('object ID')) 17 23 user = models.ForeignKey(User, default="1") 24 change_type = models.CharField(maxlength=1, choices=CHANGE_TYPES) 18 25 object = models.TextField() 19 26 comment = models.CharField(maxlength=250, blank=True) … … 25 32 verbose_name = _('changelog entry') 26 33 verbose_name_plural = _('changelog entries') 27 db_table = _(' history_changelog')34 db_table = _('django_history_log') 28 35 29 36 class Admin: 37 date_hierarchy = 'change_time' 38 list_filter = ['change_time', 'change_type', 'content_type'] 30 39 fields = ( 31 40 ('Meta info', {'fields': ('change_time', 'content_type', 'object_id', 'user', 'comment'),}), … … 33 42 ) 34 43 35 list_display = ('__str__', 'user', 'c omment', 'content_type', 'change_time', )44 list_display = ('__str__', 'user', 'change_type','comment', 'content_type', 'change_time', ) 36 45 37 46 def __str__(self): … … 85 94 86 95 def _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 """ 88 102 model_list = [] 89 103 m = None … … 105 119 except: 106 120 print "Model import error." 121 return False 107 122 108 123 return m … … 119 134 #instance_name = instance.__class__.__name__ 120 135 #print instance_name 121 m = None136 m = _import_models(instance) 122 137 old = None 123 138 log = None 124 139 125 if _import_models(instance):140 if m: 126 141 try: 142 print "Try" 127 143 if kwargs['signal_name'] is 'pre_delete': 144 print "Instance was last revision." 128 145 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): 134 148 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") 135 151 else: 136 print " Enter except."152 print "Instance without an ID." 137 153 old = instance 138 154 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") 141 156 except: 142 157 return 1 … … 150 165 print "Log: ",log.change_time 151 166 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." 154 173 155 print "New change saved."156 174 157 175 dispatcher.connect( save_new_revision, signal=signals.pre_save )
