Permalink
Browse files

make python files compatible with pep8

  • Loading branch information...
1 parent 8a703b9 commit 35269b458ffa262c9f9afb1215de587b08362e4a @sergiocharpineljr sergiocharpineljr committed Aug 13, 2012
View
@@ -13,9 +13,10 @@
settings_audit = None
value_error_re = re.compile("^.+'(.+)'$")
+
class AuditTrail(object):
- def __init__(self, show_in_admin=False, save_change_type=True, audit_deletes=True,
- track_fields=None):
+ def __init__(self, show_in_admin=False, save_change_type=True,
+ audit_deletes=True, track_fields=None):
self.opts = {}
self.opts['show_in_admin'] = show_in_admin
self.opts['save_change_type'] = save_change_type
@@ -38,7 +39,8 @@ def _contribute(sender, **kwargs):
# admin.site.register(cls, clsAdmin)
# Otherwise, register class with default ModelAdmin
admin.site.register(model)
- descriptor = AuditTrailDescriptor(model._default_manager, sender._meta.pk.attname)
+ descriptor = AuditTrailDescriptor(model._default_manager,
+ sender._meta.pk.attname)
setattr(sender, name, descriptor)
def _audit_track(instance, field_arr, **kwargs):
@@ -68,9 +70,6 @@ def _audit(sender, instance, created, **kwargs):
for field_arr in model._audit_track:
kwargs[field_arr[0]] = _audit_track(instance, field_arr)
model._default_manager.create(**kwargs)
- ## Uncomment this line for pre r8223 Django builds
- #dispatcher.connect(_audit, signal=models.signals.post_save, sender=cls, weak=False)
- ## Comment this line for pre r8223 Django builds
models.signals.post_save.connect(_audit, sender=cls, weak=False)
if self.opts['audit_deletes']:
@@ -82,17 +81,14 @@ def _audit_delete(sender, instance, **kwargs):
if self.opts['save_change_type']:
kwargs['_audit_change_type'] = 'D'
for field_arr in model._audit_track:
- kwargs[field_arr[0]] = _audit_track(instance, field_arr)
+ kwargs[field_arr[0]] = _audit_track(instance,
+ field_arr)
model._default_manager.create(**kwargs)
- ## Uncomment this line for pre r8223 Django builds
- #dispatcher.connect(_audit_delete, signal=models.signals.pre_delete, sender=cls, weak=False)
- ## Comment this line for pre r8223 Django builds
- models.signals.pre_delete.connect(_audit_delete, sender=cls, weak=False)
-
- ## Uncomment this line for pre r8223 Django builds
- #dispatcher.connect(_contribute, signal=models.signals.class_prepared, sender=cls, weak=False)
- ## Comment this line for pre r8223 Django builds
- models.signals.class_prepared.connect(_contribute, sender=cls, weak=False)
+ models.signals.pre_delete.connect(_audit_delete, sender=cls,
+ weak=False)
+ models.signals.class_prepared.connect(_contribute, sender=cls,
+ weak=False)
+
class AuditTrailDescriptor(object):
def __init__(self, manager, pk_attribute):
@@ -101,13 +97,14 @@ def __init__(self, manager, pk_attribute):
def __get__(self, instance=None, owner=None):
if instance == None:
- #raise AttributeError, "Audit trail is only accessible via %s instances." % type.__name__
return create_audit_manager_class(self.manager)
else:
- return create_audit_manager_with_pk(self.manager, self.pk_attribute, instance._get_pk_val())
+ return create_audit_manager_with_pk(self.manager,
+ self.pk_attribute, instance._get_pk_val())
def __set__(self, instance, value):
- raise AttributeError, "Audit trail may not be edited in this manner."
+ raise AttributeError("Audit trail may not be edited in this manner.")
+
def create_audit_manager_with_pk(manager, pk_attribute, pk):
"""Create an audit trail manager based on the current object"""
@@ -116,16 +113,19 @@ def __init__(self):
self.model = manager.model
def get_query_set(self):
- return super(AuditTrailWithPkManager, self).get_query_set().filter(**{pk_attribute: pk})
+ return super(AuditTrailWithPkManager, self).get_query_set().filter(
+ **{pk_attribute: pk})
return AuditTrailWithPkManager()
+
def create_audit_manager_class(manager):
"""Create an audit trail manager based on the current object"""
class AuditTrailManager(manager.__class__):
def __init__(self):
self.model = manager.model
return AuditTrailManager()
+
def create_audit_model(cls, **kwargs):
"""Create an audit model for the specific class"""
name = cls.__name__ + 'Audit'
@@ -140,10 +140,13 @@ class Meta:
'__module__': cls.__module__,
'Meta': Meta,
'_audit_id': models.AutoField(primary_key=True),
- '_audit_timestamp': models.DateTimeField(auto_now_add=True, db_index=True),
+ '_audit_timestamp': models.DateTimeField(auto_now_add=True,
+ db_index=True),
'_audit__str__': cls.__str__.im_func,
- '__str__': lambda self: '%s as of %s' % (self._audit__str__(), self._audit_timestamp),
- '_audit_track': _track_fields(track_fields=kwargs['track_fields'], unprocessed=True)
+ '__str__': lambda self: '%s as of %s' % (self._audit__str__(),
+ self._audit_timestamp),
+ '_audit_track': _track_fields(track_fields=kwargs['track_fields'],
+ unprocessed=True)
}
if 'save_change_type' in kwargs and kwargs['save_change_type']:
@@ -153,26 +156,31 @@ class Meta:
for field in cls._meta.fields:
#if field.attname in attrs:
if field.name in attrs:
- raise ImproperlyConfigured, "%s cannot use %s as it is needed by AuditTrail." % (cls.__name__, field.attname)
+ raise ImproperlyConfigured("%s cannot use %s as it is needed by "
+ "AuditTrail." % (cls.__name__,
+ field.attname))
if isinstance(field, models.AutoField):
# Audit models have a separate AutoField
- attrs[field.name] = models.IntegerField(db_index=True, editable=False)
+ attrs[field.name] = models.IntegerField(db_index=True,
+ editable=False)
else:
attrs[field.name] = copy.copy(field)
- # If 'unique' is in there, we need to remove it, otherwise the index
- # is created and multiple audit entries for one item fail.
+ # If 'unique' is in there, we need to remove it, otherwise the
+ # index is created and multiple audit entries for one item fail.
attrs[field.name]._unique = False
# If a model has primary_key = True, a second primary key would be
# created in the audit model. Set primary_key to false.
attrs[field.name].primary_key = False
for track_field in _track_fields(kwargs['track_fields']):
if track_field['name'] in attrs:
- raise NameError('Field named "%s" already exists in audit version of %s' % (track_field['name'], cls.__name__))
+ raise NameError('Field named "%s" already exists in audit version '
+ 'of %s' % (track_field['name'], cls.__name__))
attrs[track_field['name']] = copy.copy(track_field['field'])
-
+
return type(name, (models.Model,), attrs)
+
def _build_track_field(track_item):
track = {}
track['name'] = track_item[0]
@@ -181,26 +189,28 @@ def _build_track_field(track_item):
elif issubclass(track_item[1], models.Model):
track['field'] = models.ForeignKey(track_item[1])
else:
- raise TypeError('Track fields only support items that are Fields or Models.')
+ raise TypeError('Track fields only support items that are Fields or '
+ 'Models.')
return track
+
def _track_fields(track_fields=None, unprocessed=False):
# Add in the fields from the Audit class "track" attribute.
tracks_found = []
-
+
if settings_audit:
- global_track_fields = getattr(settings_audit, 'GLOBAL_TRACK_FIELDS', [])
+ global_track_fields = getattr(settings_audit, 'GLOBAL_TRACK_FIELDS',
+ [])
for track_item in global_track_fields:
if unprocessed:
tracks_found.append(track_item)
else:
tracks_found.append(_build_track_field(track_item))
-
+
if track_fields:
for track_item in track_fields:
if unprocessed:
tracks_found.append(track_item)
else:
tracks_found.append(_build_track_field(track_item))
return tracks_found
-
View
@@ -1,14 +1,17 @@
from ispdb.config.models import Config, Domain, DomainRequest
from django.contrib import admin
+
class DomainInline(admin.TabularInline):
model = Domain
extra = 0
+
class DomainRequestInline(admin.TabularInline):
model = DomainRequest
extra = 0
+
class ConfigAdmin(admin.ModelAdmin):
inlines = [
DomainRequestInline,
@@ -21,7 +24,7 @@ class ConfigAdmin(admin.ModelAdmin):
def change_view(self, request, obj_id):
c = self.model.objects.get(pk=obj_id)
if not c.domainrequests.all():
- self.inlines=[DomainInline,]
+ self.inlines = [DomainInline, ]
return super(ConfigAdmin, self).change_view(request, obj_id)
def list_domains(self, obj):
Oops, something went wrong.

0 comments on commit 35269b4

Please sign in to comment.