Code

Changes between Version 6 and Version 7 of AuditTrail


Ignore:
Timestamp:
09/05/07 08:09:08 (7 years ago)
Author:
Marty Alchin <gulopine@…>
Comment:

Updated to use Python's standard copy module.

Legend:

Unmodified
Added
Removed
Modified
  • AuditTrail

    v6 v7  
    131131from django.db import models 
    132132from django.core.exceptions import ImproperlyConfigured 
     133import copy 
    133134import re 
    134135import types 
     
    266267        if field.name in attrs: 
    267268            raise ImproperlyConfigured, "%s cannot use %s as it is needed by AuditTrail." % (cls.__name__, field.attname) 
    268         #attrs[field.attname] = copy_field(field) 
    269         attrs[field.name] = copy_field(field) 
     269        if isinstance(field, models.AutoField): 
     270            # Audit models have a separate AutoField 
     271            attrs[field.name] = models.IntegerField(db_index=True, editable=False) 
     272        else: 
     273            attrs[field.name] = copy.copy(field) 
    270274 
    271275    for track_field in _track_fields(kwargs['track_fields']): 
    272276        if track_field['name'] in attrs: 
    273277            raise NameError('Field named "%s" already exists in audit version of %s' % (track_field['name'], cls.__name__)) 
    274         attrs[track_field['name']] = copy_field(track_field['field']) 
     278        attrs[track_field['name']] = copy.copy(track_field['field']) 
    275279     
    276280    return type(name, (models.Model,), attrs) 
    277  
    278 def copy_field(field): 
    279     """Copy an instantiated field to a new instantiated field""" 
    280     if isinstance(field, models.AutoField): 
    281         # Audit models have a separate AutoField 
    282         return models.IntegerField(db_index=True, editable=False) 
    283  
    284     copied_field = None 
    285     cls = field.__class__ 
    286  
    287     # Use the field's attributes to start with 
    288     kwargs = field.__dict__.copy() 
    289  
    290     # Swap primary keys for ordinary indexes 
    291     if field.primary_key: 
    292         kwargs['db_index'] = True 
    293         del kwargs['primary_key'] 
    294  
    295     # Some hackery to copy the field 
    296     while copied_field is None: 
    297         try: 
    298             if isinstance(field, models.ForeignKey): 
    299                 copied_field = cls(field.rel.to, **kwargs) 
    300             elif isinstance(field, models.OneToOneField): 
    301                 copied_field = models.ForeignKey(field.rel.to, **kwargs) 
    302             else: 
    303                 copied_field = cls(**kwargs) 
    304         except (TypeError, ValueError), e: 
    305             # Some attributes, like creation_counter, aren't valid arguments 
    306             # So try to remove that argument so the field can try again 
    307             try: 
    308                 del kwargs[value_error_re.match(str(e)).group(1)] 
    309             except: 
    310                 # The attribute was already removed, and something's still going wrong 
    311                 raise e 
    312  
    313     return copied_field 
    314281 
    315282def _build_track_field(track_item):