- def mark_deleted(self, user):
- if not self.deleted:
- self.deleted = True
- self.deleted_at = datetime.datetime.now()
- self.deleted_by = user
- self.save()
- marked_deleted.send(sender=self.__class__, instance=self, deleted_by=user)
- return True
- else:
- return False
+ def __init__(self, *args, **kwargs):
+ super(BaseModel, self).__init__(*args, **kwargs)
+ self.reset_original_state(kwargs.keys())
+
+ def reset_original_state(self, reset_fields=None):
+ self._original_state = self._as_dict()
+
+ if reset_fields:
+ self._original_state.update(dict([(f, None) for f in reset_fields]))
+
+ def get_dirty_fields(self):
+ return [f.name for f in self._meta.fields if self._original_state[f.attname] != self.__dict__[f.attname]]
+
+ def _as_dict(self):
+ return dict([(name, getattr(self, name)) for name in
+ ([f.attname for f in self._meta.fields] + [k for k in self.__dict__.keys() if k.endswith('_dencache_')])
+ ])
+
+ def _get_update_kwargs(self):
+ return dict([
+ (f.name, getattr(self, f.name)) for f in self._meta.fields if self._original_state[f.attname] != self.__dict__[f.attname]
+ ])
+
+ def save(self, full_save=False, *args, **kwargs):
+ put_back = [k for k, v in self.__dict__.items() if isinstance(v, models.expressions.ExpressionNode)]