blob: d1784cd30371be492332874bca360f8dc0fd99eb [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 Bhatiabad67742014-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):
Scott Bakerb08d6562014-09-12 12:57:27 -070025 def get_queryset(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
Scott Bakerb08d6562014-09-12 12:57:27 -070028 # deprecated in django 1.7 in favor of get_queryset()
29 def get_query_set(self):
30 return self.get_queryset()
31
Sapan Bhatia15bf5ac2014-07-21 20:06:59 -040032# This manager will be inherited by all subclasses because
33# the core model is abstract.
Sapan Bhatia4eb663a2014-04-29 14:26:10 -040034class PlCoreBaseManager(models.Manager):
Scott Bakerb08d6562014-09-12 12:57:27 -070035 def get_queryset(self):
Sapan Bhatia4eb663a2014-04-29 14:26:10 -040036 return super(PlCoreBaseManager, self).get_query_set().filter(deleted=False)
Siobhan Tully4bc09f22013-04-10 21:15:21 -040037
Scott Bakerb08d6562014-09-12 12:57:27 -070038 # deprecated in django 1.7 in favor of get_queryset()
39 def get_query_set(self):
40 return self.get_queryset()
41
Sapan Bhatia4eb663a2014-04-29 14:26:10 -040042class PlCoreBase(models.Model):
43 objects = PlCoreBaseManager()
Sapan Bhatia15bf5ac2014-07-21 20:06:59 -040044 deleted_objects = PlCoreBaseDeletionManager()
45
46 # default values for created and updated are only there to keep evolution
47 # from failing.
Scott Baker70b70052014-09-12 12:05:51 -070048 created = models.DateTimeField(auto_now_add=True, default=datetime.datetime.now)
49 updated = models.DateTimeField(auto_now=True, default=datetime.datetime.now)
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040050 enacted = models.DateTimeField(null=True, default=None)
Sapan Bhatia34aee752014-04-28 21:06:39 -040051 backend_status = models.CharField(max_length=140,
Sapan Bhatiad507f432014-04-29 00:41:39 -040052 default="Provisioning in progress")
Sapan Bhatiabcc18992014-04-29 10:32:14 -040053 deleted = models.BooleanField(default=False)
Siobhan Tully4bc09f22013-04-10 21:15:21 -040054
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040055 class Meta:
Sapan Bhatia3089d832014-04-29 14:36:51 -040056 # Changing abstract to False would require the managers of subclasses of
57 # PlCoreBase to be customized individually.
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040058 abstract = True
59 app_label = "core"
Siobhan Tully4bc09f22013-04-10 21:15:21 -040060
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040061 def __init__(self, *args, **kwargs):
62 super(PlCoreBase, self).__init__(*args, **kwargs)
63 self.__initial = self._dict
Scott Baker13acdd62013-05-08 17:42:56 -070064
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040065 @property
66 def diff(self):
67 d1 = self.__initial
68 d2 = self._dict
69 diffs = [(k, (v, d2[k])) for k, v in d1.items() if v != d2[k]]
70 return dict(diffs)
Siobhan Tully73291342013-05-10 10:50:08 -040071
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040072 @property
73 def has_changed(self):
74 return bool(self.diff)
Siobhan Tully73291342013-05-10 10:50:08 -040075
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040076 @property
77 def changed_fields(self):
78 return self.diff.keys()
Siobhan Tully73291342013-05-10 10:50:08 -040079
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040080 def get_field_diff(self, field_name):
81 return self.diff.get(field_name, None)
Siobhan Tully73291342013-05-10 10:50:08 -040082
Tony Mack5b061472014-02-04 07:57:10 -050083 def can_update(self, user):
84 if user.is_readonly:
85 return False
86 if user.is_admin:
87 return True
88 return False
89
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040090 def delete(self, *args, **kwds):
Scott Baker6ecd4262014-01-21 23:15:21 -080091 # so we have something to give the observer
Sapan Bhatia77d1d892014-07-21 20:07:23 -040092 purge = kwds.get('purge',False)
93 try:
94 purge = purge or observer_disabled
95 except NameError:
96 pass
97
98 if (purge):
Sapan Bhatiaad2c4a12014-07-22 01:21:05 -040099 del kwds['purge']
Sapan Bhatia77d1d892014-07-21 20:07:23 -0400100 super(PlCoreBase, self).delete(*args, **kwds)
Sapan Bhatiac8602432014-04-29 20:33:51 -0400101 else:
102 self.deleted = True
103 self.enacted=None
104 self.save(update_fields=['enacted','deleted'])
Scott Baker6ecd4262014-01-21 23:15:21 -0800105
Sapan Bhatiadbaf1932013-09-03 11:28:52 -0400106
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -0400107 def save(self, *args, **kwargs):
108 super(PlCoreBase, self).save(*args, **kwargs)
Sapan Bhatiac8602432014-04-29 20:33:51 -0400109
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -0400110 # This is a no-op if observer_disabled is set
111 notify_observer()
Sapan Bhatia66f4e612013-07-02 12:12:38 -0400112
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -0400113 self.__initial = self._dict
Scott Baker13acdd62013-05-08 17:42:56 -0700114
Tony Mack5b061472014-02-04 07:57:10 -0500115 def save_by_user(self, user, *args, **kwds):
116 if self.can_update(user):
117 self.save(*args, **kwds)
118
Tony Mack332ee1d2014-02-04 15:33:45 -0500119 def delete_by_user(self, user, *args, **kwds):
120 if self.can_update(user):
121 self.delete(*args, **kwds)
122
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -0400123 @property
124 def _dict(self):
125 return model_to_dict(self, fields=[field.name for field in
126 self._meta.fields])
Scott Baker13acdd62013-05-08 17:42:56 -0700127
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400128
129