Changes between Version 6 and Version 7 of AuditTrail
- Timestamp:
- Sep 5, 2007, 10:09:08 AM (17 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
AuditTrail
v6 v7 131 131 from django.db import models 132 132 from django.core.exceptions import ImproperlyConfigured 133 import copy 133 134 import re 134 135 import types … … 266 267 if field.name in attrs: 267 268 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) 270 274 271 275 for track_field in _track_fields(kwargs['track_fields']): 272 276 if track_field['name'] in attrs: 273 277 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']) 275 279 276 280 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 AutoField282 return models.IntegerField(db_index=True, editable=False)283 284 copied_field = None285 cls = field.__class__286 287 # Use the field's attributes to start with288 kwargs = field.__dict__.copy()289 290 # Swap primary keys for ordinary indexes291 if field.primary_key:292 kwargs['db_index'] = True293 del kwargs['primary_key']294 295 # Some hackery to copy the field296 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 arguments306 # So try to remove that argument so the field can try again307 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 wrong311 raise e312 313 return copied_field314 281 315 282 def _build_track_field(track_item):