Changeset 3637
- Timestamp:
- 08/21/06 16:38:15 (2 years ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/full-history/django/contrib/history/models.py
r3599 r3637 16 16 ) 17 17 18 ######################### 19 # Manager - API methods # 20 ######################### 21 22 class ChangeLogManager(models.Manager): 23 24 def get_version(self, object, offset=0): 25 """ 26 Returns 'current-offset' revision of the 'object' 27 """ 28 ct = ContentType.objects.get_for_model(object) 29 return self.get_query_set().filter( 30 content_type=ct.id).filter( 31 object_id=object.id)[offset] 32 33 def list_history(self, object, **kwargs): 34 """ 35 list_history(object): Returns a list of all revisions for that id. 36 list_history(object, offset=X): Returns a list of last X revisions. 37 """ 38 ct = ContentType.objects.get_for_model(object) 39 if kwargs['offset']: 40 return self.get_query_set().filter( 41 content_type=ct.id).filter( 42 object_id=object.id)[:kwargs['offset']] 43 else: 44 return ChangeLog.objects.filter(object_id=object.id) 45 46 def version_by_date(self, object, date): 47 """ 48 Returns a list of revisions made at 'date'. 49 """ 50 ct = ContentType.objects.get_for_model(object) 51 return self.get_query_set().filter( 52 content_type=ct.id).filter( 53 change_time__exact=date) 54 55 ######################## 56 # Generic change model # 57 ######################## 58 18 59 class ChangeLog(models.Model): 19 60 change_time = models.DateTimeField (_('time of change'), auto_now=True) … … 26 67 comment = models.CharField(maxlength=250, blank=True) 27 68 28 #object_type = models.CharField(maxlength=50) 29 #pub_date = models.DateTimeField('date published') 69 objects = ChangeLogManager() 30 70 31 71 class Meta: … … 53 93 def get_rev_num(self): 54 94 """ Returns the ID/revision number of ChangeLog entry. """ 55 return self.id 56 57 ####################### 58 # Other (API) methods # 59 ####################### 60 61 #class ChangeLogManager(models.Manager): 62 63 def get_version(object, offset=0): 64 """ Returns 'current-offset' revision of the 'object' """ 65 try: 66 list = ChangeLog.objects.order_by('-id').filter(object_id=object.id)[offset] 67 print list.get_object() 68 return list 69 except: 70 pass 71 72 def list_history(parent_id, **kwargs): 73 """ 74 list_history(parent_id): Returns a list of all revisions for that id. 75 list_history(parent_id, offset=X): Returns a list of last X revisions. 76 """ 77 if kwargs: 78 list = ChangeLog.objects.filter(object_id=parent_id)[:kwargs['offset']] 79 return list 80 else: 81 return ChangeLog.objects.filter(object_id=parent_id) 82 83 84 def version_by_date(self, date): 85 """ Returns a list of revisions made at 'date'. """ 86 return ChangeLog.objects.filter(object_id=self.id).filter(change_time__exact=date) 87 88 89 95 return self.id 90 96 91 97 ######################### … … 98 104 which "instance" is an instance of. 99 105 100 Returns " True" if import went fine.106 Returns "import object" if import went fine. 101 107 """ 102 108 model_list = [] … … 124 130 125 131 def save_new_revision(sender, instance, signal, *args, **kwargs): 126 """ Saves a old copy of the record into the History table.""" 132 """ 133 Saves a old copy of the record into the History table. 134 135 If the instance does not have an ID then it is a new record and saved as such. 136 If the instance is passed with signal_name='pre_save' it saves a previous 137 version (taken from the database). 138 If the instance is passed with signal_name='pre_delete' it saves the instance 139 as the latest revision. 140 141 """ 127 142 print "Sender: ",sender 128 143 print "Signal: ",kwargs['signal_name'] … … 134 149 #instance_name = instance.__class__.__name__ 135 150 #print instance_name 136 m = _import_models(instance)151 im = _import_models(instance) 137 152 old = None 138 153 log = None 139 154 140 if m:155 if im: 141 156 try: 142 print "Try"143 157 if kwargs['signal_name'] is 'pre_delete': 144 158 print "Instance was last revision." … … 147 161 elif ((kwargs['signal_name'] is 'pre_save') and instance.id): 148 162 print "Instance has an ID." 149 old = getattr( m, instance.__class__.__name__).objects.filter(pk=instance.id)[0]163 old = getattr(im, instance.__class__.__name__).objects.filter(pk=instance.id)[0] 150 164 log = ChangeLog(parent=instance, change_type='U', comment="Update") 151 165 else: … … 160 174 161 175 # DEBUG 162 print "Old: ",old163 print "Instance: ",instance.id176 #print "Old: ",old 177 #print "Instance: ",instance.id 164 178 #print "Test: ",getattr(instance, 'Admin').date_hierarchy 165 print "Log: ",log.change_time179 #print "Log: ",log.change_time 166 180 167 181 try:
