blob: 59cac52c3767d1e48a5ddc58fb9564c3ec8493d8 [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
Sapan Bhatia39097192014-09-04 00:39:19 -04008import model_policy
Scott Baker9e990742014-03-19 22:14:58 -07009
10try:
11 # This is a no-op if observer_disabled is set to 1 in the config file
12 from observer import *
13except:
Scott Baker65d5a9a2014-05-26 15:58:09 -070014 print >> sys.stderr, "import of observer failed! printing traceback and disabling observer:"
Scott Baker9e990742014-03-19 22:14:58 -070015 import traceback
16 traceback.print_exc()
17
18 # guard against something failing
Scott Bakerfd44dfc2014-05-23 13:20:53 -070019 def notify_observer(*args, **kwargs):
Scott Baker9e990742014-03-19 22:14:58 -070020 pass
Siobhan Tully4bc09f22013-04-10 21:15:21 -040021
Sapan Bhatia3089d832014-04-29 14:36:51 -040022# This manager will be inherited by all subclasses because
23# the core model is abstract.
Sapan Bhatia15bf5ac2014-07-21 20:06:59 -040024class PlCoreBaseDeletionManager(models.Manager):
25 def get_query_set(self):
Sapan Bhatiabbcc1902014-07-22 01:11:36 -040026 return super(PlCoreBaseDeletionManager, self).get_query_set().filter(deleted=True)
Sapan Bhatia15bf5ac2014-07-21 20:06:59 -040027
28# This manager will be inherited by all subclasses because
29# the core model is abstract.
Sapan Bhatia4eb663a2014-04-29 14:26:10 -040030class PlCoreBaseManager(models.Manager):
31 def get_query_set(self):
32 return super(PlCoreBaseManager, self).get_query_set().filter(deleted=False)
Siobhan Tully4bc09f22013-04-10 21:15:21 -040033
Sapan Bhatia4eb663a2014-04-29 14:26:10 -040034class PlCoreBase(models.Model):
35 objects = PlCoreBaseManager()
Sapan Bhatia15bf5ac2014-07-21 20:06:59 -040036 deleted_objects = PlCoreBaseDeletionManager()
37
38 # default values for created and updated are only there to keep evolution
39 # from failing.
Scott Baker09dd0c02014-09-12 12:05:51 -070040 created = models.DateTimeField(auto_now_add=True, default=datetime.datetime.now)
41 updated = models.DateTimeField(auto_now=True, default=datetime.datetime.now)
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040042 enacted = models.DateTimeField(null=True, default=None)
Sapan Bhatia34aee752014-04-28 21:06:39 -040043 backend_status = models.CharField(max_length=140,
Sapan Bhatiad507f432014-04-29 00:41:39 -040044 default="Provisioning in progress")
Sapan Bhatiabcc18992014-04-29 10:32:14 -040045 deleted = models.BooleanField(default=False)
Siobhan Tully4bc09f22013-04-10 21:15:21 -040046
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040047 class Meta:
Sapan Bhatia3089d832014-04-29 14:36:51 -040048 # Changing abstract to False would require the managers of subclasses of
49 # PlCoreBase to be customized individually.
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040050 abstract = True
51 app_label = "core"
Siobhan Tully4bc09f22013-04-10 21:15:21 -040052
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040053 def __init__(self, *args, **kwargs):
54 super(PlCoreBase, self).__init__(*args, **kwargs)
55 self.__initial = self._dict
Scott Baker13acdd62013-05-08 17:42:56 -070056
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040057 @property
58 def diff(self):
59 d1 = self.__initial
60 d2 = self._dict
61 diffs = [(k, (v, d2[k])) for k, v in d1.items() if v != d2[k]]
62 return dict(diffs)
Siobhan Tully73291342013-05-10 10:50:08 -040063
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040064 @property
65 def has_changed(self):
66 return bool(self.diff)
Siobhan Tully73291342013-05-10 10:50:08 -040067
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040068 @property
69 def changed_fields(self):
70 return self.diff.keys()
Siobhan Tully73291342013-05-10 10:50:08 -040071
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040072 def get_field_diff(self, field_name):
73 return self.diff.get(field_name, None)
Siobhan Tully73291342013-05-10 10:50:08 -040074
Tony Mack5b061472014-02-04 07:57:10 -050075 def can_update(self, user):
76 if user.is_readonly:
77 return False
78 if user.is_admin:
79 return True
80 return False
81
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040082 def delete(self, *args, **kwds):
Scott Baker6ecd4262014-01-21 23:15:21 -080083 # so we have something to give the observer
Sapan Bhatia77d1d892014-07-21 20:07:23 -040084 purge = kwds.get('purge',False)
85 try:
86 purge = purge or observer_disabled
87 except NameError:
88 pass
89
90 if (purge):
Sapan Bhatiaad2c4a12014-07-22 01:21:05 -040091 del kwds['purge']
Sapan Bhatia77d1d892014-07-21 20:07:23 -040092 super(PlCoreBase, self).delete(*args, **kwds)
Sapan Bhatiac8602432014-04-29 20:33:51 -040093 else:
94 self.deleted = True
95 self.enacted=None
96 self.save(update_fields=['enacted','deleted'])
Scott Baker6ecd4262014-01-21 23:15:21 -080097
Sapan Bhatiadbaf1932013-09-03 11:28:52 -040098
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040099 def save(self, *args, **kwargs):
100 super(PlCoreBase, self).save(*args, **kwargs)
Sapan Bhatiac8602432014-04-29 20:33:51 -0400101
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -0400102 # This is a no-op if observer_disabled is set
103 notify_observer()
Sapan Bhatia66f4e612013-07-02 12:12:38 -0400104
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -0400105 self.__initial = self._dict
Scott Baker13acdd62013-05-08 17:42:56 -0700106
Tony Mack5b061472014-02-04 07:57:10 -0500107 def save_by_user(self, user, *args, **kwds):
108 if self.can_update(user):
109 self.save(*args, **kwds)
110
Tony Mack332ee1d2014-02-04 15:33:45 -0500111 def delete_by_user(self, user, *args, **kwds):
112 if self.can_update(user):
113 self.delete(*args, **kwds)
114
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -0400115 @property
116 def _dict(self):
117 return model_to_dict(self, fields=[field.name for field in
118 self._meta.fields])
Scott Baker13acdd62013-05-08 17:42:56 -0700119
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400120
121