Commits

Trey Hunner  committed 314c711 Draft

Fix coding style and remove unused variables

  • Participants
  • Parent commits ee78138

Comments (0)

Files changed (4)

File simple_history/__init__.py

-from django.utils import importlib
-
 import models
 
 

File simple_history/admin.py

 from django import template
-from django.db import models
+from django.core.exceptions import PermissionDenied
 from django.conf.urls.defaults import patterns, url
-from django.conf import settings
 from django.contrib import admin
 from django.contrib.admin import helpers
 from django.contrib.contenttypes.models import ContentType
         history = getattr(self.model, self.model._meta.simple_history_manager_attribute)
         model = history.model
         opts = model._meta
-        app_label = opts.app_label
         pk_name = original_opts.pk.attname
         record = get_object_or_404(model, **{pk_name: object_id, 'history_id': version_id})
         obj = record.instance
         obj._state.adding = False
-        original = get_object_or_404(original_model, pk=object_id)
 
         if not self.has_change_permission(request, obj):
             raise PermissionDenied
 
-        if request.method == 'POST' and request.POST.has_key("_saveasnew"):
+        if request.method == 'POST' and '_saveas_new' in request.POST:
             return self.add_view(request, form_url='../add/')
 
         formsets = []
 
         else:
             form = ModelForm(instance=obj)
-            prefixes = {}
 
         adminForm = helpers.AdminForm(form, self.get_fieldsets(request, obj),
             self.prepopulated_fields, self.get_readonly_fields(request, obj),
             model_admin=self)
         media = self.media + adminForm.media
 
-
         url_triplet = (self.admin_site.name, original_opts.app_label,
                             original_opts.module_name)
         context = {

File simple_history/manager.py

 from django.db import models
 
+
 class HistoryDescriptor(object):
     def __init__(self, model):
         self.model = model
             return HistoryManager(self.model)
         return HistoryManager(self.model, instance)
 
+
 class HistoryManager(models.Manager):
     def __init__(self, model, instance=None):
         super(HistoryManager, self).__init__()
             return super(HistoryManager, self).get_query_set()
 
         if isinstance(self.instance._meta.pk, models.OneToOneField):
-            filter = {self.instance._meta.pk.name+"_id":self.instance.pk}
+            filter = {self.instance._meta.pk.name + "_id": self.instance.pk}
         else:
             filter = {self.instance._meta.pk.name: self.instance.pk}
         return super(HistoryManager, self).get_query_set().filter(**filter)
         Returns the most recent copy of the instance available in the history.
         """
         if not self.instance:
-            raise TypeError("Can't use most_recent() without a %s instance." % \
+            raise TypeError("Can't use most_recent() without a %s instance." %
                             self.instance._meta.object_name)
         tmp = []
         for field in self.instance._meta.fields:
             if isinstance(field, models.ForeignKey):
-                tmp.append(field.name+"_id")
+                tmp.append(field.name + "_id")
             else:
                 tmp.append(field.name)
         fields = tuple(tmp)
         try:
             values = self.values_list(*fields)[0]
         except IndexError:
-            raise self.instance.DoesNotExist("%s has no historical record." % \
+            raise self.instance.DoesNotExist("%s has no historical record." %
                                              self.instance._meta.object_name)
         return self.instance.__class__(*values)
 
         according to what was present on the object on the date provided.
         """
         if not self.instance:
-            raise TypeError("Can't use as_of() without a %s instance." % \
+            raise TypeError("Can't use as_of() without a %s instance." %
                             self.instance._meta.object_name)
-        tmp=[]
+        tmp = []
         for field in self.instance._meta.fields:
             if isinstance(field, models.ForeignKey):
-                tmp.append(field.name+"_id")
+                tmp.append(field.name + "_id")
             else:
                 tmp.append(field.name)
         fields = tuple(tmp)
         try:
             values = qs.values_list('history_type', *fields)[0]
         except IndexError:
-            raise self.instance.DoesNotExist("%s had not yet been created." % \
+            raise self.instance.DoesNotExist("%s had not yet been created." %
                                              self.instance._meta.object_name)
         if values[0] == '-':
-            raise self.instance.DoesNotExist("%s had already been deleted." % \
+            raise self.instance.DoesNotExist("%s had already been deleted." %
                                              self.instance._meta.object_name)
-        return self.instance.__class__(*values[1:])
+        return self.instance.__class__(*values[1:])

File simple_history/models.py

 import copy
-import datetime
 from django.db import models
 from django.contrib import admin
 from django.contrib.auth.models import User
 from django.utils import importlib
 from manager import HistoryDescriptor
 
+
 class HistoricalRecords(object):
     def contribute_to_class(self, cls, name):
         self.manager_name = name
         def get_instance(self):
             return model(**dict([(k, getattr(self, k)) for k in fields]))
 
-        rel_nm = '_%s_history' % model._meta.object_name.lower()
         return {
             'history_id': models.AutoField(primary_key=True),
-            'history_date': models.DateTimeField(default=datetime.datetime.now),
+            'history_date': models.DateTimeField(auto_now_add=True),
             'history_type': models.CharField(max_length=1, choices=(
                 ('+', 'Created'),
                 ('~', 'Changed'),
             attrs[field.attname] = getattr(instance, field.attname)
         manager.create(history_type=type, changed_by=changed_by, **attrs)
 
+
 class HistoricalObjectDescriptor(object):
     def __init__(self, model):
         self.model = model