Django

Code

Changeset 3637

Show
Ignore:
Timestamp:
08/21/06 16:38:15 (2 years ago)
Author:
utrebec
Message:

[full-history]
* Added ChangeLogManager? + objects = ChangeLogManager?() to ChangeLog? class
* Moved API funtions to ChangeLogManager?
* Fixed the problem with filtering by "content_type" -> now uses object's content_type.id
* Commented out Some debug statements

Files:

Legend:

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

    r3599 r3637  
    1616) 
    1717 
     18######################### 
     19# Manager - API methods # 
     20######################### 
     21 
     22class 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 
    1859class ChangeLog(models.Model): 
    1960    change_time = models.DateTimeField (_('time of change'), auto_now=True) 
     
    2667    comment = models.CharField(maxlength=250, blank=True) 
    2768 
    28     #object_type = models.CharField(maxlength=50) 
    29     #pub_date = models.DateTimeField('date published') 
     69    objects = ChangeLogManager() 
    3070     
    3171    class Meta: 
     
    5393    def get_rev_num(self): 
    5494        """ 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   
    9096 
    9197######################### 
     
    98104    which "instance" is an instance of. 
    99105 
    100     Returns "True" if import went fine. 
     106    Returns "import object" if import went fine. 
    101107    """ 
    102108    model_list = [] 
     
    124130 
    125131def 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    """ 
    127142    print "Sender: ",sender 
    128143    print "Signal: ",kwargs['signal_name'] 
     
    134149    #instance_name = instance.__class__.__name__ 
    135150    #print instance_name 
    136     m = _import_models(instance) 
     151    im = _import_models(instance) 
    137152    old = None 
    138153    log = None 
    139154     
    140     if m: 
     155    if im: 
    141156        try: 
    142             print "Try" 
    143157            if kwargs['signal_name'] is 'pre_delete': 
    144158                print "Instance was last revision." 
     
    147161            elif ((kwargs['signal_name'] is 'pre_save') and instance.id): 
    148162                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] 
    150164                log = ChangeLog(parent=instance, change_type='U', comment="Update") 
    151165            else: 
     
    160174 
    161175    # DEBUG 
    162     print "Old: ",old 
    163     print "Instance: ",instance.id 
     176    #print "Old: ",old 
     177    #print "Instance: ",instance.id 
    164178    #print "Test: ",getattr(instance, 'Admin').date_hierarchy 
    165     print "Log: ",log.change_time 
     179    #print "Log: ",log.change_time 
    166180 
    167181    try: