blob: 9ce409d51a2ad7ba4ee9eeac21c2c3cba1eb2a1c [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
Sapan Bhatia4eb663a2014-04-29 14:26:10 -040021class PlCoreBaseManager(models.Manager):
22 def get_query_set(self):
23 return super(PlCoreBaseManager, self).get_query_set().filter(deleted=False)
Siobhan Tully4bc09f22013-04-10 21:15:21 -040024
Scott Bakerfd44dfc2014-05-23 13:20:53 -070025 # default values for created and updated are only there to keep evolution
26 # from failing.
Sapan Bhatia4eb663a2014-04-29 14:26:10 -040027class PlCoreBase(models.Model):
28 objects = PlCoreBaseManager()
Scott Bakerfd44dfc2014-05-23 13:20:53 -070029 created = models.DateTimeField(auto_now_add=True, default=datetime.datetime.now())
30 updated = models.DateTimeField(auto_now=True, default=datetime.datetime.now())
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040031 enacted = models.DateTimeField(null=True, default=None)
Sapan Bhatia34aee752014-04-28 21:06:39 -040032 backend_status = models.CharField(max_length=140,
Sapan Bhatiad507f432014-04-29 00:41:39 -040033 default="Provisioning in progress")
Sapan Bhatiabcc18992014-04-29 10:32:14 -040034 deleted = models.BooleanField(default=False)
Siobhan Tully4bc09f22013-04-10 21:15:21 -040035
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040036 class Meta:
37 abstract = True
38 app_label = "core"
Siobhan Tully4bc09f22013-04-10 21:15:21 -040039
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040040 def __init__(self, *args, **kwargs):
41 super(PlCoreBase, self).__init__(*args, **kwargs)
42 self.__initial = self._dict
Scott Baker13acdd62013-05-08 17:42:56 -070043
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040044 @property
45 def diff(self):
46 d1 = self.__initial
47 d2 = self._dict
48 diffs = [(k, (v, d2[k])) for k, v in d1.items() if v != d2[k]]
49 return dict(diffs)
Siobhan Tully73291342013-05-10 10:50:08 -040050
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040051 @property
52 def has_changed(self):
53 return bool(self.diff)
Siobhan Tully73291342013-05-10 10:50:08 -040054
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040055 @property
56 def changed_fields(self):
57 return self.diff.keys()
Siobhan Tully73291342013-05-10 10:50:08 -040058
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040059 def get_field_diff(self, field_name):
60 return self.diff.get(field_name, None)
Siobhan Tully73291342013-05-10 10:50:08 -040061
Tony Mack5b061472014-02-04 07:57:10 -050062 def can_update(self, user):
63 if user.is_readonly:
64 return False
65 if user.is_admin:
66 return True
67 return False
68
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040069 def delete(self, *args, **kwds):
Scott Baker6ecd4262014-01-21 23:15:21 -080070 # so we have something to give the observer
71 pk = self.pk
72 model_dict = model_to_dict(self)
73 for (k,v) in model_dict.items():
74 # things like datetime are not JSON serializable
75 model_dict[k] = str(v)
76
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040077 super(PlCoreBase, self).delete(*args, **kwds)
Sapan Bhatiadbaf1932013-09-03 11:28:52 -040078
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040079 # This is a no-op if observer_disabled is set
Scott Baker6ecd4262014-01-21 23:15:21 -080080 notify_observer(model=self, delete=True, pk=pk, model_dict=model_dict)
Sapan Bhatiadbaf1932013-09-03 11:28:52 -040081
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040082 def save(self, *args, **kwargs):
83 super(PlCoreBase, self).save(*args, **kwargs)
84
85 # This is a no-op if observer_disabled is set
86 notify_observer()
Sapan Bhatia66f4e612013-07-02 12:12:38 -040087
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040088 self.__initial = self._dict
Scott Baker13acdd62013-05-08 17:42:56 -070089
Tony Mack5b061472014-02-04 07:57:10 -050090 def save_by_user(self, user, *args, **kwds):
91 if self.can_update(user):
92 self.save(*args, **kwds)
93
Tony Mack332ee1d2014-02-04 15:33:45 -050094 def delete_by_user(self, user, *args, **kwds):
95 if self.can_update(user):
96 self.delete(*args, **kwds)
97
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040098 @property
99 def _dict(self):
100 return model_to_dict(self, fields=[field.name for field in
101 self._meta.fields])
Scott Baker13acdd62013-05-08 17:42:56 -0700102
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400103
104