blob: ec79419d982b7c707f475499619dc81290a60c4d [file] [log] [blame]
Scott Bakerfd44dfc2014-05-23 13:20:53 -07001import datetime
Siobhan Tully4bc09f22013-04-10 21:15:21 -04002import os
Scott Baker65d5a9a2014-05-26 15:58:09 -07003import sys
Siobhan Tully4bc09f22013-04-10 21:15:21 -04004from django.db import models
Scott Baker13acdd62013-05-08 17:42:56 -07005from django.forms.models import model_to_dict
Scott Bakerc1c45f82014-01-21 16:23:51 -08006from django.core.urlresolvers import reverse
Scott Baker6ecd4262014-01-21 23:15:21 -08007from django.forms.models import model_to_dict
Scott Baker9e990742014-03-19 22:14:58 -07008
9try:
10 # This is a no-op if observer_disabled is set to 1 in the config file
11 from observer import *
12except:
Scott Baker65d5a9a2014-05-26 15:58:09 -070013 print >> sys.stderr, "import of observer failed! printing traceback and disabling observer:"
Scott Baker9e990742014-03-19 22:14:58 -070014 import traceback
15 traceback.print_exc()
16
17 # guard against something failing
Scott Bakerfd44dfc2014-05-23 13:20:53 -070018 def notify_observer(*args, **kwargs):
Scott Baker9e990742014-03-19 22:14:58 -070019 pass
Siobhan Tully4bc09f22013-04-10 21:15:21 -040020
21class PlCoreBase(models.Model):
22
Scott Bakerfd44dfc2014-05-23 13:20:53 -070023 # default values for created and updated are only there to keep evolution
24 # from failing.
25
26 created = models.DateTimeField(auto_now_add=True, default=datetime.datetime.now())
27 updated = models.DateTimeField(auto_now=True, default=datetime.datetime.now())
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040028 enacted = models.DateTimeField(null=True, default=None)
Siobhan Tully4bc09f22013-04-10 21:15:21 -040029
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040030 class Meta:
31 abstract = True
32 app_label = "core"
Siobhan Tully4bc09f22013-04-10 21:15:21 -040033
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040034 def __init__(self, *args, **kwargs):
35 super(PlCoreBase, self).__init__(*args, **kwargs)
36 self.__initial = self._dict
Scott Baker13acdd62013-05-08 17:42:56 -070037
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040038 @property
39 def diff(self):
40 d1 = self.__initial
41 d2 = self._dict
42 diffs = [(k, (v, d2[k])) for k, v in d1.items() if v != d2[k]]
43 return dict(diffs)
Siobhan Tully73291342013-05-10 10:50:08 -040044
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040045 @property
46 def has_changed(self):
47 return bool(self.diff)
Siobhan Tully73291342013-05-10 10:50:08 -040048
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040049 @property
50 def changed_fields(self):
51 return self.diff.keys()
Siobhan Tully73291342013-05-10 10:50:08 -040052
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040053 def get_field_diff(self, field_name):
54 return self.diff.get(field_name, None)
Siobhan Tully73291342013-05-10 10:50:08 -040055
Tony Mack5b061472014-02-04 07:57:10 -050056 def can_update(self, user):
57 if user.is_readonly:
58 return False
59 if user.is_admin:
60 return True
61 return False
62
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040063 def delete(self, *args, **kwds):
Scott Baker6ecd4262014-01-21 23:15:21 -080064 # so we have something to give the observer
65 pk = self.pk
66 model_dict = model_to_dict(self)
67 for (k,v) in model_dict.items():
68 # things like datetime are not JSON serializable
69 model_dict[k] = str(v)
70
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040071 super(PlCoreBase, self).delete(*args, **kwds)
Sapan Bhatiadbaf1932013-09-03 11:28:52 -040072
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040073 # This is a no-op if observer_disabled is set
Scott Baker6ecd4262014-01-21 23:15:21 -080074 notify_observer(model=self, delete=True, pk=pk, model_dict=model_dict)
Sapan Bhatiadbaf1932013-09-03 11:28:52 -040075
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040076 def save(self, *args, **kwargs):
77 super(PlCoreBase, self).save(*args, **kwargs)
78
79 # This is a no-op if observer_disabled is set
80 notify_observer()
Sapan Bhatia66f4e612013-07-02 12:12:38 -040081
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040082 self.__initial = self._dict
Scott Baker13acdd62013-05-08 17:42:56 -070083
Tony Mack5b061472014-02-04 07:57:10 -050084 def save_by_user(self, user, *args, **kwds):
85 if self.can_update(user):
86 self.save(*args, **kwds)
87
Tony Mack332ee1d2014-02-04 15:33:45 -050088 def delete_by_user(self, user, *args, **kwds):
89 if self.can_update(user):
90 self.delete(*args, **kwds)
91
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040092 @property
93 def _dict(self):
94 return model_to_dict(self, fields=[field.name for field in
95 self._meta.fields])
Scott Baker13acdd62013-05-08 17:42:56 -070096
Siobhan Tully4bc09f22013-04-10 21:15:21 -040097
98