blob: 8c5bddc6876df9dd55fa9db645e309f6d734b6cb [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
Tony Mack5b061472014-02-04 07:57:10 -050041 def can_update(self, user):
42 if user.is_readonly:
43 return False
44 if user.is_admin:
45 return True
46 return False
47
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040048 def delete(self, *args, **kwds):
Scott Baker6ecd4262014-01-21 23:15:21 -080049 # so we have something to give the observer
50 pk = self.pk
51 model_dict = model_to_dict(self)
52 for (k,v) in model_dict.items():
53 # things like datetime are not JSON serializable
54 model_dict[k] = str(v)
55
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040056 super(PlCoreBase, self).delete(*args, **kwds)
Sapan Bhatiadbaf1932013-09-03 11:28:52 -040057
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040058 # This is a no-op if observer_disabled is set
Scott Baker6ecd4262014-01-21 23:15:21 -080059 notify_observer(model=self, delete=True, pk=pk, model_dict=model_dict)
Sapan Bhatiadbaf1932013-09-03 11:28:52 -040060
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040061 def save(self, *args, **kwargs):
62 super(PlCoreBase, self).save(*args, **kwargs)
63
64 # This is a no-op if observer_disabled is set
65 notify_observer()
Sapan Bhatia66f4e612013-07-02 12:12:38 -040066
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040067 self.__initial = self._dict
Scott Baker13acdd62013-05-08 17:42:56 -070068
Tony Mack5b061472014-02-04 07:57:10 -050069 def save_by_user(self, user, *args, **kwds):
70 if self.can_update(user):
71 self.save(*args, **kwds)
72
Tony Mack332ee1d2014-02-04 15:33:45 -050073 def delete_by_user(self, user, *args, **kwds):
74 if self.can_update(user):
75 self.delete(*args, **kwds)
76
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040077 @property
78 def _dict(self):
79 return model_to_dict(self, fields=[field.name for field in
80 self._meta.fields])
Scott Baker13acdd62013-05-08 17:42:56 -070081
Siobhan Tully4bc09f22013-04-10 21:15:21 -040082
83