blob: 76aa7a22394887c3acf18ee88d4673346ecc4f58 [file] [log] [blame]
Siobhan Tully4bc09f22013-04-10 21:15:21 -04001import os
Scott Baker65d5a9a2014-05-26 15:58:09 -07002import sys
Siobhan Tully4bc09f22013-04-10 21:15:21 -04003from django.db import models
Scott Baker13acdd62013-05-08 17:42:56 -07004from django.forms.models import model_to_dict
Scott Bakerc1c45f82014-01-21 16:23:51 -08005from django.core.urlresolvers import reverse
Scott Baker6ecd4262014-01-21 23:15:21 -08006from django.forms.models import model_to_dict
Scott Bakerb24f2c32014-09-17 22:18:46 -07007from django.utils import timezone
Scott Baker0bec56f2014-10-02 22:50:18 -07008from django.core.exceptions import PermissionDenied
Sapan Bhatiabad67742014-09-04 00:39:19 -04009import model_policy
Sapan Bhatia40bbfd92014-11-12 10:38:23 -050010from model_autodeletion import ephemeral_models
Scott Baker9e990742014-03-19 22:14:58 -070011
12try:
13 # This is a no-op if observer_disabled is set to 1 in the config file
14 from observer import *
15except:
Scott Baker65d5a9a2014-05-26 15:58:09 -070016 print >> sys.stderr, "import of observer failed! printing traceback and disabling observer:"
Scott Baker9e990742014-03-19 22:14:58 -070017 import traceback
18 traceback.print_exc()
19
20 # guard against something failing
Scott Bakerfd44dfc2014-05-23 13:20:53 -070021 def notify_observer(*args, **kwargs):
Scott Baker9e990742014-03-19 22:14:58 -070022 pass
Siobhan Tully4bc09f22013-04-10 21:15:21 -040023
Sapan Bhatia3089d832014-04-29 14:36:51 -040024# This manager will be inherited by all subclasses because
25# the core model is abstract.
Sapan Bhatia15bf5ac2014-07-21 20:06:59 -040026class PlCoreBaseDeletionManager(models.Manager):
Scott Bakerb08d6562014-09-12 12:57:27 -070027 def get_queryset(self):
Scott Bakerb24f2c32014-09-17 22:18:46 -070028 parent=super(PlCoreBaseDeletionManager, self)
29 if hasattr(parent, "get_queryset"):
30 return parent.get_queryset().filter(deleted=True)
31 else:
32 return parent.get_query_set().filter(deleted=True)
Sapan Bhatia15bf5ac2014-07-21 20:06:59 -040033
Scott Bakerb24f2c32014-09-17 22:18:46 -070034 # deprecated in django 1.7 in favor of get_queryset().
Scott Bakerb08d6562014-09-12 12:57:27 -070035 def get_query_set(self):
36 return self.get_queryset()
37
Sapan Bhatia15bf5ac2014-07-21 20:06:59 -040038# This manager will be inherited by all subclasses because
39# the core model is abstract.
Sapan Bhatia4eb663a2014-04-29 14:26:10 -040040class PlCoreBaseManager(models.Manager):
Scott Bakerb08d6562014-09-12 12:57:27 -070041 def get_queryset(self):
Scott Bakerb24f2c32014-09-17 22:18:46 -070042 parent=super(PlCoreBaseManager, self)
43 if hasattr(parent, "get_queryset"):
44 return parent.get_queryset().filter(deleted=False)
45 else:
46 return parent.get_query_set().filter(deleted=False)
Siobhan Tully4bc09f22013-04-10 21:15:21 -040047
Scott Bakerb24f2c32014-09-17 22:18:46 -070048 # deprecated in django 1.7 in favor of get_queryset().
Scott Bakerb08d6562014-09-12 12:57:27 -070049 def get_query_set(self):
50 return self.get_queryset()
51
Scott Bakercbfb6002014-10-03 00:32:37 -070052class DiffModelMixIn:
53 # Provides useful methods for computing which objects in a model have
54 # changed. Make sure to do self._initial = self._dict in the __init__
55 # method.
56
57 # This is broken out of PlCoreBase into a Mixin so the User model can
58 # also make use of it.
59
60 @property
61 def _dict(self):
62 return model_to_dict(self, fields=[field.name for field in
63 self._meta.fields])
64
65 @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)
71
72 @property
73 def has_changed(self):
74 return bool(self.diff)
75
76 @property
77 def changed_fields(self):
78 return self.diff.keys()
79
Scott Bakercbfb6002014-10-03 00:32:37 -070080 def has_field_changed(self, field_name):
81 return field_name in self.diff.keys()
82
83 def get_field_diff(self, field_name):
84 return self.diff.get(field_name, None)
85
Scott Baker56f799b2014-11-25 11:35:19 -080086 #classmethod
87 def getValidators(cls):
88 """ primarily for REST API, return a dictionary of field names mapped
89 to lists of the type of validations that need to be applied to
90 those fields.
91 """
92 validators = {}
93 for field in cls._meta.fields:
94 l = []
95 if field.blank==False:
96 l.append("notBlank")
Scott Baker0d108672014-11-26 00:53:19 -080097 if field.__class__.__name__=="URLField":
98 l.append("url")
Scott Baker56f799b2014-11-25 11:35:19 -080099 validators[field.name] = l
100 return validators
101
Scott Baker46e50af2014-11-06 17:33:27 -0800102class PlCoreBase(models.Model): # , DiffModelMixIn):
Sapan Bhatia4eb663a2014-04-29 14:26:10 -0400103 objects = PlCoreBaseManager()
Sapan Bhatia15bf5ac2014-07-21 20:06:59 -0400104 deleted_objects = PlCoreBaseDeletionManager()
105
Scott Baker46e50af2014-11-06 17:33:27 -0800106 # ---- copy stuff from DiffModelMixin ----
107
108 # XXX Django fails miserably when trying to create initial migrations when
109 # DiffModelMixin is used. So, until we figure out what's wrong,
110 # just copied the guts of DiffModelMixIn here.
111
112 @property
113 def _dict(self):
114 return model_to_dict(self, fields=[field.name for field in
115 self._meta.fields])
116
117 @property
118 def diff(self):
119 d1 = self._initial
120 d2 = self._dict
121 diffs = [(k, (v, d2[k])) for k, v in d1.items() if v != d2[k]]
122 return dict(diffs)
123
124 @property
125 def has_changed(self):
126 return bool(self.diff)
127
128 @property
129 def changed_fields(self):
130 return self.diff.keys()
131
132 def has_field_changed(self, field_name):
133 return field_name in self.diff.keys()
134
135 def get_field_diff(self, field_name):
136 return self.diff.get(field_name, None)
Scott Baker56f799b2014-11-25 11:35:19 -0800137
138 #classmethod
139 def getValidators(cls):
140 """ primarily for REST API, return a dictionary of field names mapped
141 to lists of the type of validations that need to be applied to
142 those fields.
143 """
144 validators = {}
145 for field in cls._meta.fields:
146 l = []
147 if field.blank==False:
148 l.append("notBlank")
Scott Baker0d108672014-11-26 00:53:19 -0800149 if field.__class__.__name__=="URLField":
150 l.append("url")
Scott Baker56f799b2014-11-25 11:35:19 -0800151 validators[field.name] = l
152 return validators
153
Scott Baker46e50af2014-11-06 17:33:27 -0800154 # ---- end copy stuff from DiffModelMixin ----
155
Sapan Bhatia15bf5ac2014-07-21 20:06:59 -0400156 # default values for created and updated are only there to keep evolution
157 # from failing.
Scott Bakerb24f2c32014-09-17 22:18:46 -0700158 created = models.DateTimeField(auto_now_add=True, default=timezone.now)
159 updated = models.DateTimeField(auto_now=True, default=timezone.now)
Scott Baker2a72eaf2014-12-01 21:42:59 -0800160 enacted = models.DateTimeField(null=True, blank=True, default=None)
Sapan Bhatia67400ad2015-01-23 15:59:55 +0000161 policed = models.DateTimeField(null=True, blank=True, default=None)
Sapan Bhatia34aee752014-04-28 21:06:39 -0400162 backend_status = models.CharField(max_length=140,
Sapan Bhatiad507f432014-04-29 00:41:39 -0400163 default="Provisioning in progress")
Sapan Bhatiabcc18992014-04-29 10:32:14 -0400164 deleted = models.BooleanField(default=False)
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400165
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -0400166 class Meta:
Sapan Bhatia3089d832014-04-29 14:36:51 -0400167 # Changing abstract to False would require the managers of subclasses of
168 # PlCoreBase to be customized individually.
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -0400169 abstract = True
170 app_label = "core"
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400171
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -0400172 def __init__(self, *args, **kwargs):
173 super(PlCoreBase, self).__init__(*args, **kwargs)
Scott Bakercbfb6002014-10-03 00:32:37 -0700174 self._initial = self._dict # for DiffModelMixIn
Scott Baker5dc87a62014-09-23 22:41:17 -0700175 self.silent = False
Scott Baker13acdd62013-05-08 17:42:56 -0700176
Tony Mack5b061472014-02-04 07:57:10 -0500177 def can_update(self, user):
178 if user.is_readonly:
179 return False
180 if user.is_admin:
181 return True
Tony Mack5b061472014-02-04 07:57:10 -0500182
Scott Baker0119c152014-10-06 22:58:48 -0700183 return False
Scott Bakercbfb6002014-10-03 00:32:37 -0700184
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -0400185 def delete(self, *args, **kwds):
Scott Baker6ecd4262014-01-21 23:15:21 -0800186 # so we have something to give the observer
Sapan Bhatia77d1d892014-07-21 20:07:23 -0400187 purge = kwds.get('purge',False)
Scott Bakerc5b50602014-10-09 16:22:00 -0700188 if purge:
189 del kwds['purge']
Scott Baker0491f6f2014-09-23 16:04:36 -0700190 silent = kwds.get('silent',False)
Scott Bakerc5b50602014-10-09 16:22:00 -0700191 if silent:
192 del kwds['silent']
Sapan Bhatia77d1d892014-07-21 20:07:23 -0400193 try:
194 purge = purge or observer_disabled
195 except NameError:
196 pass
Scott Baker0491f6f2014-09-23 16:04:36 -0700197
Sapan Bhatia77d1d892014-07-21 20:07:23 -0400198 if (purge):
199 super(PlCoreBase, self).delete(*args, **kwds)
Sapan Bhatiac8602432014-04-29 20:33:51 -0400200 else:
201 self.deleted = True
202 self.enacted=None
Scott Baker0491f6f2014-09-23 16:04:36 -0700203 self.save(update_fields=['enacted','deleted'], silent=silent)
Sapan Bhatiadbaf1932013-09-03 11:28:52 -0400204
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -0400205 def save(self, *args, **kwargs):
Scott Baker5dc87a62014-09-23 22:41:17 -0700206 # let the user specify silence as either a kwarg or an instance varible
207 silent = self.silent
Scott Baker0491f6f2014-09-23 16:04:36 -0700208 if "silent" in kwargs:
Scott Baker5dc87a62014-09-23 22:41:17 -0700209 silent=silent or kwargs.pop("silent")
Scott Baker0491f6f2014-09-23 16:04:36 -0700210
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -0400211 super(PlCoreBase, self).save(*args, **kwargs)
Sapan Bhatiac8602432014-04-29 20:33:51 -0400212
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -0400213 # This is a no-op if observer_disabled is set
Scott Baker0491f6f2014-09-23 16:04:36 -0700214 if not silent:
215 notify_observer()
Sapan Bhatia66f4e612013-07-02 12:12:38 -0400216
Scott Baker165f70c2014-10-03 14:48:06 -0700217 self._initial = self._dict
Scott Baker13acdd62013-05-08 17:42:56 -0700218
Tony Mack5b061472014-02-04 07:57:10 -0500219 def save_by_user(self, user, *args, **kwds):
Scott Baker0bec56f2014-10-02 22:50:18 -0700220 if not self.can_update(user):
Scott Baker0119c152014-10-06 22:58:48 -0700221 if getattr(self, "_cant_update_fieldName", None) is not None:
222 raise PermissionDenied("You do not have permission to update field %s on object %s" % (self._cant_update_fieldName, self.__class__.__name__))
223 else:
224 raise PermissionDenied("You do not have permission to update %s objects" % self.__class__.__name__)
Scott Bakercbfb6002014-10-03 00:32:37 -0700225
Scott Baker0bec56f2014-10-02 22:50:18 -0700226 self.save(*args, **kwds)
Tony Mack5b061472014-02-04 07:57:10 -0500227
Tony Mack332ee1d2014-02-04 15:33:45 -0500228 def delete_by_user(self, user, *args, **kwds):
Scott Baker0bec56f2014-10-02 22:50:18 -0700229 if not self.can_update(user):
230 raise PermissionDenied("You do not have permission to delete %s objects" % self.__class__.__name__)
231 self.delete(*args, **kwds)
Tony Mack332ee1d2014-02-04 15:33:45 -0500232
Scott Baker165f70c2014-10-03 14:48:06 -0700233 @classmethod
234 def select_by_user(cls, user):
235 # This should be overridden by descendant classes that want to perform
236 # filtering of visible objects by user.
237 return cls.objects.all()
238
Sapan Bhatia998b32e2014-11-12 10:06:23 -0500239 @classmethod
240 def is_ephemeral(cls):
Sapan Bhatia40bbfd92014-11-12 10:38:23 -0500241 return cls in ephemeral_models
Sapan Bhatia998b32e2014-11-12 10:06:23 -0500242
Scott Bakere40fbd42014-11-25 00:49:17 -0800243
244
Scott Baker13acdd62013-05-08 17:42:56 -0700245
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400246
247