fix the problems with DiffModelMixin, and eliminate redundant code
diff --git a/planetstack/core/models/plcorebase.py b/planetstack/core/models/plcorebase.py
index 5369512..31d98a1 100644
--- a/planetstack/core/models/plcorebase.py
+++ b/planetstack/core/models/plcorebase.py
@@ -50,7 +50,7 @@
def get_query_set(self):
return self.get_queryset()
-class DiffModelMixIn:
+class DiffModelMixIn(object):
# Provides useful methods for computing which objects in a model have
# changed. Make sure to do self._initial = self._dict in the __init__
# method.
@@ -106,66 +106,10 @@
validators[field.name] = l
return validators
-class PlCoreBase(models.Model): # , DiffModelMixIn):
+class PlCoreBase(models.Model, DiffModelMixIn):
objects = PlCoreBaseManager()
deleted_objects = PlCoreBaseDeletionManager()
- # ---- copy stuff from DiffModelMixin ----
-
- # XXX Django fails miserably when trying to create initial migrations when
- # DiffModelMixin is used. So, until we figure out what's wrong,
- # just copied the guts of DiffModelMixIn here.
-
- @property
- def _dict(self):
- return model_to_dict(self, fields=[field.name for field in
- self._meta.fields])
-
- def fields_differ(self,f1,f2):
- if isinstance(f1,datetime.datetime) and isinstance(f2,datetime.datetime) and (timezone.is_aware(f1) != timezone.is_aware(f2)):
- return True
- else:
- return (f1 != f2)
-
- @property
- def diff(self):
- d1 = self._initial
- d2 = self._dict
- diffs = [(k, (v, d2[k])) for k, v in d1.items() if self.fields_differ(v,d2[k])]
- return dict(diffs)
-
- @property
- def has_changed(self):
- return bool(self.diff)
-
- @property
- def changed_fields(self):
- return self.diff.keys()
-
- def has_field_changed(self, field_name):
- return field_name in self.diff.keys()
-
- def get_field_diff(self, field_name):
- return self.diff.get(field_name, None)
-
- #classmethod
- def getValidators(cls):
- """ primarily for REST API, return a dictionary of field names mapped
- to lists of the type of validations that need to be applied to
- those fields.
- """
- validators = {}
- for field in cls._meta.fields:
- l = []
- if field.blank==False:
- l.append("notBlank")
- if field.__class__.__name__=="URLField":
- l.append("url")
- validators[field.name] = l
- return validators
-
- # ---- end copy stuff from DiffModelMixin ----
-
# default values for created and updated are only there to keep evolution
# from failing.
created = models.DateTimeField(auto_now_add=True, default=timezone.now)
diff --git a/planetstack/core/models/user.py b/planetstack/core/models/user.py
index 924f602..2f75db7 100644
--- a/planetstack/core/models/user.py
+++ b/planetstack/core/models/user.py
@@ -84,59 +84,7 @@
def get_query_set(self):
return self.get_queryset()
-class User(AbstractBaseUser): #, DiffModelMixIn):
-
- # ---- copy stuff from DiffModelMixin ----
-
- @property
- def _dict(self):
- return model_to_dict(self, fields=[field.name for field in
- self._meta.fields])
-
- def fields_differ(self,f1,f2):
- if isinstance(f1,datetime.datetime) and isinstance(f2,datetime.datetime) and (timezone.is_aware(f1) != timezone.is_aware(f2)):
- return True
- else:
- return (f1 != f2)
-
- @property
- def diff(self):
- d1 = self._initial
- d2 = self._dict
- diffs = [(k, (v, d2[k])) for k, v in d1.items() if self.fields_differ(v,d2[k])]
- return dict(diffs)
-
- @property
- def has_changed(self):
- return bool(self.diff)
-
- @property
- def changed_fields(self):
- return self.diff.keys()
-
- def has_field_changed(self, field_name):
- return field_name in self.diff.keys()
-
- def get_field_diff(self, field_name):
- return self.diff.get(field_name, None)
-
- #classmethod
- def getValidators(cls):
- """ primarily for REST API, return a dictionary of field names mapped
- to lists of the type of validations that need to be applied to
- those fields.
- """
- validators = {}
- for field in cls._meta.fields:
- l = []
- if field.blank==False:
- l.append("notBlank")
- if field.__class__.__name__=="URLField":
- l.append("url")
- validators[field.name] = l
- return validators
- # ---- end copy stuff from DiffModelMixin ----
-
+class User(AbstractBaseUser, DiffModelMixIn):
@property
def remote_password(self):
return hashlib.md5(self.password).hexdigest()[:12]