blob: 7251a819a59db967541ac8e26d3f32725f78d721 [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
Scott Baker9e990742014-03-19 22:14:58 -07006
7try:
8 # This is a no-op if observer_disabled is set to 1 in the config file
9 from observer import *
10except:
11 print "import of observer failed! printing traceback and disabling observer:"
12 import traceback
13 traceback.print_exc()
14
15 # guard against something failing
16 def notify_observer():
17 pass
Siobhan Tully4bc09f22013-04-10 21:15:21 -040018
19class PlCoreBase(models.Model):
20
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040021 created = models.DateTimeField(auto_now_add=True)
22 updated = models.DateTimeField(auto_now=True)
23 enacted = models.DateTimeField(null=True, default=None)
Siobhan Tully4bc09f22013-04-10 21:15:21 -040024
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040025 class Meta:
26 abstract = True
27 app_label = "core"
Siobhan Tully4bc09f22013-04-10 21:15:21 -040028
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040029 def __init__(self, *args, **kwargs):
30 super(PlCoreBase, self).__init__(*args, **kwargs)
31 self.__initial = self._dict
Scott Baker13acdd62013-05-08 17:42:56 -070032
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040033 @property
34 def diff(self):
35 d1 = self.__initial
36 d2 = self._dict
37 diffs = [(k, (v, d2[k])) for k, v in d1.items() if v != d2[k]]
38 return dict(diffs)
Siobhan Tully73291342013-05-10 10:50:08 -040039
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040040 @property
41 def has_changed(self):
42 return bool(self.diff)
Siobhan Tully73291342013-05-10 10:50:08 -040043
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040044 @property
45 def changed_fields(self):
46 return self.diff.keys()
Siobhan Tully73291342013-05-10 10:50:08 -040047
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040048 def get_field_diff(self, field_name):
49 return self.diff.get(field_name, None)
Siobhan Tully73291342013-05-10 10:50:08 -040050
Tony Mack5b061472014-02-04 07:57:10 -050051 def can_update(self, user):
52 if user.is_readonly:
53 return False
54 if user.is_admin:
55 return True
56 return False
57
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040058 def delete(self, *args, **kwds):
Scott Baker6ecd4262014-01-21 23:15:21 -080059 # so we have something to give the observer
60 pk = self.pk
61 model_dict = model_to_dict(self)
62 for (k,v) in model_dict.items():
63 # things like datetime are not JSON serializable
64 model_dict[k] = str(v)
65
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040066 super(PlCoreBase, self).delete(*args, **kwds)
Sapan Bhatiadbaf1932013-09-03 11:28:52 -040067
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040068 # This is a no-op if observer_disabled is set
Scott Baker6ecd4262014-01-21 23:15:21 -080069 notify_observer(model=self, delete=True, pk=pk, model_dict=model_dict)
Sapan Bhatiadbaf1932013-09-03 11:28:52 -040070
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040071 def save(self, *args, **kwargs):
72 super(PlCoreBase, self).save(*args, **kwargs)
73
74 # This is a no-op if observer_disabled is set
75 notify_observer()
Sapan Bhatia66f4e612013-07-02 12:12:38 -040076
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040077 self.__initial = self._dict
Scott Baker13acdd62013-05-08 17:42:56 -070078
Tony Mack5b061472014-02-04 07:57:10 -050079 def save_by_user(self, user, *args, **kwds):
80 if self.can_update(user):
81 self.save(*args, **kwds)
82
Tony Mack332ee1d2014-02-04 15:33:45 -050083 def delete_by_user(self, user, *args, **kwds):
84 if self.can_update(user):
85 self.delete(*args, **kwds)
86
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040087 @property
88 def _dict(self):
89 return model_to_dict(self, fields=[field.name for field in
90 self._meta.fields])
Scott Baker13acdd62013-05-08 17:42:56 -070091
Siobhan Tully4bc09f22013-04-10 21:15:21 -040092
93