blob: 590e24009a585fa3210aa74870a680e107cb1d55 [file] [log] [blame]
Siobhan Tully4bc09f22013-04-10 21:15:21 -04001import os
2from django.db import models
Scott Baker13acdd62013-05-08 17:42:56 -07003from django.forms.models import model_to_dict
Scott Bakerc1c45f82014-01-21 16:23:51 -08004from django.core.urlresolvers import reverse
Scott Baker6ecd4262014-01-21 23:15:21 -08005from django.forms.models import model_to_dict
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -04006# This is a no-op if observer_disabled is set to 1 in the config file
7from observer import *
Siobhan Tully4bc09f22013-04-10 21:15:21 -04008
9class PlCoreBase(models.Model):
10
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040011 created = models.DateTimeField(auto_now_add=True)
12 updated = models.DateTimeField(auto_now=True)
13 enacted = models.DateTimeField(null=True, default=None)
Siobhan Tully4bc09f22013-04-10 21:15:21 -040014
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040015 class Meta:
16 abstract = True
17 app_label = "core"
Siobhan Tully4bc09f22013-04-10 21:15:21 -040018
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040019 def __init__(self, *args, **kwargs):
20 super(PlCoreBase, self).__init__(*args, **kwargs)
21 self.__initial = self._dict
Scott Baker13acdd62013-05-08 17:42:56 -070022
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040023 @property
24 def diff(self):
25 d1 = self.__initial
26 d2 = self._dict
27 diffs = [(k, (v, d2[k])) for k, v in d1.items() if v != d2[k]]
28 return dict(diffs)
Siobhan Tully73291342013-05-10 10:50:08 -040029
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040030 @property
31 def has_changed(self):
32 return bool(self.diff)
Siobhan Tully73291342013-05-10 10:50:08 -040033
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040034 @property
35 def changed_fields(self):
36 return self.diff.keys()
Siobhan Tully73291342013-05-10 10:50:08 -040037
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040038 def get_field_diff(self, field_name):
39 return self.diff.get(field_name, None)
Siobhan Tully73291342013-05-10 10:50:08 -040040
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040041 def delete(self, *args, **kwds):
Scott Baker6ecd4262014-01-21 23:15:21 -080042 # so we have something to give the observer
43 pk = self.pk
44 model_dict = model_to_dict(self)
45 for (k,v) in model_dict.items():
46 # things like datetime are not JSON serializable
47 model_dict[k] = str(v)
48
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040049 super(PlCoreBase, self).delete(*args, **kwds)
Sapan Bhatiadbaf1932013-09-03 11:28:52 -040050
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040051 # This is a no-op if observer_disabled is set
Scott Baker6ecd4262014-01-21 23:15:21 -080052 notify_observer(model=self, delete=True, pk=pk, model_dict=model_dict)
Sapan Bhatiadbaf1932013-09-03 11:28:52 -040053
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040054 def save(self, *args, **kwargs):
55 super(PlCoreBase, self).save(*args, **kwargs)
56
57 # This is a no-op if observer_disabled is set
58 notify_observer()
Sapan Bhatia66f4e612013-07-02 12:12:38 -040059
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040060 self.__initial = self._dict
Scott Baker13acdd62013-05-08 17:42:56 -070061
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040062 @property
63 def _dict(self):
64 return model_to_dict(self, fields=[field.name for field in
65 self._meta.fields])
Scott Baker13acdd62013-05-08 17:42:56 -070066
Siobhan Tully4bc09f22013-04-10 21:15:21 -040067
68