blob: 51240addec73c544dfaf02d7f09f6670a321a10a [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 Bhatia3089d832014-04-29 14:36:51 -040021# This manager will be inherited by all subclasses because
22# the core model is abstract.
Sapan Bhatia4eb663a2014-04-29 14:26:10 -040023class PlCoreBaseManager(models.Manager):
24 def get_query_set(self):
25 return super(PlCoreBaseManager, self).get_query_set().filter(deleted=False)
Siobhan Tully4bc09f22013-04-10 21:15:21 -040026
Scott Bakerfd44dfc2014-05-23 13:20:53 -070027 # default values for created and updated are only there to keep evolution
28 # from failing.
Sapan Bhatia4eb663a2014-04-29 14:26:10 -040029class PlCoreBase(models.Model):
30 objects = PlCoreBaseManager()
Scott Bakerfd44dfc2014-05-23 13:20:53 -070031 created = models.DateTimeField(auto_now_add=True, default=datetime.datetime.now())
32 updated = models.DateTimeField(auto_now=True, default=datetime.datetime.now())
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040033 enacted = models.DateTimeField(null=True, default=None)
Sapan Bhatia34aee752014-04-28 21:06:39 -040034 backend_status = models.CharField(max_length=140,
Sapan Bhatiad507f432014-04-29 00:41:39 -040035 default="Provisioning in progress")
Sapan Bhatiabcc18992014-04-29 10:32:14 -040036 deleted = models.BooleanField(default=False)
Siobhan Tully4bc09f22013-04-10 21:15:21 -040037
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040038 class Meta:
Sapan Bhatia3089d832014-04-29 14:36:51 -040039 # Changing abstract to False would require the managers of subclasses of
40 # PlCoreBase to be customized individually.
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040041 abstract = True
42 app_label = "core"
Siobhan Tully4bc09f22013-04-10 21:15:21 -040043
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040044 def __init__(self, *args, **kwargs):
45 super(PlCoreBase, self).__init__(*args, **kwargs)
46 self.__initial = self._dict
Scott Baker13acdd62013-05-08 17:42:56 -070047
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040048 @property
49 def diff(self):
50 d1 = self.__initial
51 d2 = self._dict
52 diffs = [(k, (v, d2[k])) for k, v in d1.items() if v != d2[k]]
53 return dict(diffs)
Siobhan Tully73291342013-05-10 10:50:08 -040054
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040055 @property
56 def has_changed(self):
57 return bool(self.diff)
Siobhan Tully73291342013-05-10 10:50:08 -040058
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040059 @property
60 def changed_fields(self):
61 return self.diff.keys()
Siobhan Tully73291342013-05-10 10:50:08 -040062
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040063 def get_field_diff(self, field_name):
64 return self.diff.get(field_name, None)
Siobhan Tully73291342013-05-10 10:50:08 -040065
Tony Mack5b061472014-02-04 07:57:10 -050066 def can_update(self, user):
67 if user.is_readonly:
68 return False
69 if user.is_admin:
70 return True
71 return False
72
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040073 def delete(self, *args, **kwds):
Scott Baker6ecd4262014-01-21 23:15:21 -080074 # so we have something to give the observer
Sapan Bhatia13027812014-04-30 00:51:02 -040075 purge = kwds.get('purge',True)
76 if (observer_disabled or purge):
Sapan Bhatiac8602432014-04-29 20:33:51 -040077 super(PlCoreBase, self).delete(*args, **kwargs)
78 else:
79 self.deleted = True
80 self.enacted=None
81 self.save(update_fields=['enacted','deleted'])
Scott Baker6ecd4262014-01-21 23:15:21 -080082
Sapan Bhatiadbaf1932013-09-03 11:28:52 -040083
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040084 def save(self, *args, **kwargs):
85 super(PlCoreBase, self).save(*args, **kwargs)
Sapan Bhatiac8602432014-04-29 20:33:51 -040086
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040087 # This is a no-op if observer_disabled is set
88 notify_observer()
Sapan Bhatia66f4e612013-07-02 12:12:38 -040089
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040090 self.__initial = self._dict
Scott Baker13acdd62013-05-08 17:42:56 -070091
Tony Mack5b061472014-02-04 07:57:10 -050092 def save_by_user(self, user, *args, **kwds):
93 if self.can_update(user):
94 self.save(*args, **kwds)
95
Tony Mack332ee1d2014-02-04 15:33:45 -050096 def delete_by_user(self, user, *args, **kwds):
97 if self.can_update(user):
98 self.delete(*args, **kwds)
99
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -0400100 @property
101 def _dict(self):
102 return model_to_dict(self, fields=[field.name for field in
103 self._meta.fields])
Scott Baker13acdd62013-05-08 17:42:56 -0700104
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400105
106