blob: c6fc5be9f2f2ae8ce17e9170894b296114303a0a [file] [log] [blame]
Scott Bakerfd44dfc2014-05-23 13:20:53 -07001import datetime
Siobhan Tully4bc09f22013-04-10 21:15:21 -04002import os
3from django.db import models
Scott Baker13acdd62013-05-08 17:42:56 -07004from django.forms.models import model_to_dict
Scott Bakerc1c45f82014-01-21 16:23:51 -08005from django.core.urlresolvers import reverse
Scott Baker6ecd4262014-01-21 23:15:21 -08006from django.forms.models import model_to_dict
Scott Baker9e990742014-03-19 22:14:58 -07007
8try:
9 # This is a no-op if observer_disabled is set to 1 in the config file
10 from observer import *
11except:
12 print "import of observer failed! printing traceback and disabling observer:"
13 import traceback
14 traceback.print_exc()
15
16 # guard against something failing
Scott Bakerfd44dfc2014-05-23 13:20:53 -070017 def notify_observer(*args, **kwargs):
Scott Baker9e990742014-03-19 22:14:58 -070018 pass
Siobhan Tully4bc09f22013-04-10 21:15:21 -040019
20class PlCoreBase(models.Model):
21
Scott Bakerfd44dfc2014-05-23 13:20:53 -070022 # default values for created and updated are only there to keep evolution
23 # from failing.
24
25 created = models.DateTimeField(auto_now_add=True, default=datetime.datetime.now())
26 updated = models.DateTimeField(auto_now=True, default=datetime.datetime.now())
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040027 enacted = models.DateTimeField(null=True, default=None)
Siobhan Tully4bc09f22013-04-10 21:15:21 -040028
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040029 class Meta:
30 abstract = True
31 app_label = "core"
Siobhan Tully4bc09f22013-04-10 21:15:21 -040032
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040033 def __init__(self, *args, **kwargs):
34 super(PlCoreBase, self).__init__(*args, **kwargs)
35 self.__initial = self._dict
Scott Baker13acdd62013-05-08 17:42:56 -070036
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040037 @property
38 def diff(self):
39 d1 = self.__initial
40 d2 = self._dict
41 diffs = [(k, (v, d2[k])) for k, v in d1.items() if v != d2[k]]
42 return dict(diffs)
Siobhan Tully73291342013-05-10 10:50:08 -040043
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040044 @property
45 def has_changed(self):
46 return bool(self.diff)
Siobhan Tully73291342013-05-10 10:50:08 -040047
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040048 @property
49 def changed_fields(self):
50 return self.diff.keys()
Siobhan Tully73291342013-05-10 10:50:08 -040051
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040052 def get_field_diff(self, field_name):
53 return self.diff.get(field_name, None)
Siobhan Tully73291342013-05-10 10:50:08 -040054
Tony Mack5b061472014-02-04 07:57:10 -050055 def can_update(self, user):
56 if user.is_readonly:
57 return False
58 if user.is_admin:
59 return True
60 return False
61
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040062 def delete(self, *args, **kwds):
Scott Baker6ecd4262014-01-21 23:15:21 -080063 # so we have something to give the observer
64 pk = self.pk
65 model_dict = model_to_dict(self)
66 for (k,v) in model_dict.items():
67 # things like datetime are not JSON serializable
68 model_dict[k] = str(v)
69
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040070 super(PlCoreBase, self).delete(*args, **kwds)
Sapan Bhatiadbaf1932013-09-03 11:28:52 -040071
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040072 # This is a no-op if observer_disabled is set
Scott Baker6ecd4262014-01-21 23:15:21 -080073 notify_observer(model=self, delete=True, pk=pk, model_dict=model_dict)
Sapan Bhatiadbaf1932013-09-03 11:28:52 -040074
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040075 def save(self, *args, **kwargs):
76 super(PlCoreBase, self).save(*args, **kwargs)
77
78 # This is a no-op if observer_disabled is set
79 notify_observer()
Sapan Bhatia66f4e612013-07-02 12:12:38 -040080
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040081 self.__initial = self._dict
Scott Baker13acdd62013-05-08 17:42:56 -070082
Tony Mack5b061472014-02-04 07:57:10 -050083 def save_by_user(self, user, *args, **kwds):
84 if self.can_update(user):
85 self.save(*args, **kwds)
86
Tony Mack332ee1d2014-02-04 15:33:45 -050087 def delete_by_user(self, user, *args, **kwds):
88 if self.can_update(user):
89 self.delete(*args, **kwds)
90
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040091 @property
92 def _dict(self):
93 return model_to_dict(self, fields=[field.name for field in
94 self._meta.fields])
Scott Baker13acdd62013-05-08 17:42:56 -070095
Siobhan Tully4bc09f22013-04-10 21:15:21 -040096
97