blob: c86d6755afa2f3ff8667e70dfef1e48185cde96c [file] [log] [blame]
Scott Bakerdaca8162015-02-02 14:28:35 -08001import 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 Bakerb24f2c32014-09-17 22:18:46 -07008from django.utils import timezone
Scott Baker0bec56f2014-10-02 22:50:18 -07009from django.core.exceptions import PermissionDenied
Sapan Bhatiabad67742014-09-04 00:39:19 -040010import model_policy
Sapan Bhatia40bbfd92014-11-12 10:38:23 -050011from model_autodeletion import ephemeral_models
Scott Baker9e990742014-03-19 22:14:58 -070012
13try:
14 # This is a no-op if observer_disabled is set to 1 in the config file
15 from observer import *
16except:
Scott Baker65d5a9a2014-05-26 15:58:09 -070017 print >> sys.stderr, "import of observer failed! printing traceback and disabling observer:"
Scott Baker9e990742014-03-19 22:14:58 -070018 import traceback
19 traceback.print_exc()
20
21 # guard against something failing
Scott Bakerfd44dfc2014-05-23 13:20:53 -070022 def notify_observer(*args, **kwargs):
Scott Baker9e990742014-03-19 22:14:58 -070023 pass
Siobhan Tully4bc09f22013-04-10 21:15:21 -040024
Sapan Bhatia3089d832014-04-29 14:36:51 -040025# This manager will be inherited by all subclasses because
26# the core model is abstract.
Sapan Bhatia15bf5ac2014-07-21 20:06:59 -040027class PlCoreBaseDeletionManager(models.Manager):
Scott Bakerb08d6562014-09-12 12:57:27 -070028 def get_queryset(self):
Scott Bakerb24f2c32014-09-17 22:18:46 -070029 parent=super(PlCoreBaseDeletionManager, self)
30 if hasattr(parent, "get_queryset"):
31 return parent.get_queryset().filter(deleted=True)
32 else:
33 return parent.get_query_set().filter(deleted=True)
Sapan Bhatia15bf5ac2014-07-21 20:06:59 -040034
Scott Bakerb24f2c32014-09-17 22:18:46 -070035 # deprecated in django 1.7 in favor of get_queryset().
Scott Bakerb08d6562014-09-12 12:57:27 -070036 def get_query_set(self):
37 return self.get_queryset()
38
Sapan Bhatia15bf5ac2014-07-21 20:06:59 -040039# This manager will be inherited by all subclasses because
40# the core model is abstract.
Sapan Bhatia4eb663a2014-04-29 14:26:10 -040041class PlCoreBaseManager(models.Manager):
Scott Bakerb08d6562014-09-12 12:57:27 -070042 def get_queryset(self):
Scott Bakerb24f2c32014-09-17 22:18:46 -070043 parent=super(PlCoreBaseManager, self)
44 if hasattr(parent, "get_queryset"):
45 return parent.get_queryset().filter(deleted=False)
46 else:
47 return parent.get_query_set().filter(deleted=False)
Siobhan Tully4bc09f22013-04-10 21:15:21 -040048
Scott Bakerb24f2c32014-09-17 22:18:46 -070049 # deprecated in django 1.7 in favor of get_queryset().
Scott Bakerb08d6562014-09-12 12:57:27 -070050 def get_query_set(self):
51 return self.get_queryset()
52
Scott Bakercbfb6002014-10-03 00:32:37 -070053class DiffModelMixIn:
54 # Provides useful methods for computing which objects in a model have
55 # changed. Make sure to do self._initial = self._dict in the __init__
56 # method.
57
58 # This is broken out of PlCoreBase into a Mixin so the User model can
59 # also make use of it.
60
61 @property
62 def _dict(self):
63 return model_to_dict(self, fields=[field.name for field in
64 self._meta.fields])
65
Scott Bakerdaca8162015-02-02 14:28:35 -080066 def fields_differ(self,f1,f2):
67 if isinstance(f1,datetime.datetime) and isinstance(f2,datetime.datetime) and (timezone.is_aware(f1) != timezone.is_aware(f2)):
68 return True
69 else:
70 return (f1 != f2)
71
Scott Bakercbfb6002014-10-03 00:32:37 -070072 @property
73 def diff(self):
74 d1 = self._initial
75 d2 = self._dict
Scott Bakerdaca8162015-02-02 14:28:35 -080076 diffs = [(k, (v, d2[k])) for k, v in d1.items() if self.fields_differ(v,d2[k])]
Scott Bakercbfb6002014-10-03 00:32:37 -070077 return dict(diffs)
78
79 @property
80 def has_changed(self):
81 return bool(self.diff)
82
83 @property
84 def changed_fields(self):
85 return self.diff.keys()
86
Scott Bakercbfb6002014-10-03 00:32:37 -070087 def has_field_changed(self, field_name):
88 return field_name in self.diff.keys()
89
90 def get_field_diff(self, field_name):
91 return self.diff.get(field_name, None)
92
Scott Baker56f799b2014-11-25 11:35:19 -080093 #classmethod
94 def getValidators(cls):
95 """ primarily for REST API, return a dictionary of field names mapped
96 to lists of the type of validations that need to be applied to
97 those fields.
98 """
99 validators = {}
100 for field in cls._meta.fields:
101 l = []
102 if field.blank==False:
103 l.append("notBlank")
Scott Baker0d108672014-11-26 00:53:19 -0800104 if field.__class__.__name__=="URLField":
105 l.append("url")
Scott Baker56f799b2014-11-25 11:35:19 -0800106 validators[field.name] = l
107 return validators
108
Scott Baker46e50af2014-11-06 17:33:27 -0800109class PlCoreBase(models.Model): # , DiffModelMixIn):
Sapan Bhatia4eb663a2014-04-29 14:26:10 -0400110 objects = PlCoreBaseManager()
Sapan Bhatia15bf5ac2014-07-21 20:06:59 -0400111 deleted_objects = PlCoreBaseDeletionManager()
112
Scott Baker46e50af2014-11-06 17:33:27 -0800113 # ---- copy stuff from DiffModelMixin ----
114
115 # XXX Django fails miserably when trying to create initial migrations when
116 # DiffModelMixin is used. So, until we figure out what's wrong,
117 # just copied the guts of DiffModelMixIn here.
118
119 @property
120 def _dict(self):
121 return model_to_dict(self, fields=[field.name for field in
122 self._meta.fields])
123
Scott Bakerdaca8162015-02-02 14:28:35 -0800124 def fields_differ(self,f1,f2):
125 if isinstance(f1,datetime.datetime) and isinstance(f2,datetime.datetime) and (timezone.is_aware(f1) != timezone.is_aware(f2)):
126 return True
127 else:
128 return (f1 != f2)
129
Scott Baker46e50af2014-11-06 17:33:27 -0800130 @property
131 def diff(self):
132 d1 = self._initial
133 d2 = self._dict
Scott Bakerdaca8162015-02-02 14:28:35 -0800134 diffs = [(k, (v, d2[k])) for k, v in d1.items() if self.fields_differ(v,d2[k])]
Scott Baker46e50af2014-11-06 17:33:27 -0800135 return dict(diffs)
136
137 @property
138 def has_changed(self):
139 return bool(self.diff)
140
141 @property
142 def changed_fields(self):
143 return self.diff.keys()
144
145 def has_field_changed(self, field_name):
146 return field_name in self.diff.keys()
147
148 def get_field_diff(self, field_name):
149 return self.diff.get(field_name, None)
Scott Baker56f799b2014-11-25 11:35:19 -0800150
151 #classmethod
152 def getValidators(cls):
153 """ primarily for REST API, return a dictionary of field names mapped
154 to lists of the type of validations that need to be applied to
155 those fields.
156 """
157 validators = {}
158 for field in cls._meta.fields:
159 l = []
160 if field.blank==False:
161 l.append("notBlank")
Scott Baker0d108672014-11-26 00:53:19 -0800162 if field.__class__.__name__=="URLField":
163 l.append("url")
Scott Baker56f799b2014-11-25 11:35:19 -0800164 validators[field.name] = l
165 return validators
166
Scott Baker46e50af2014-11-06 17:33:27 -0800167 # ---- end copy stuff from DiffModelMixin ----
168
Sapan Bhatia15bf5ac2014-07-21 20:06:59 -0400169 # default values for created and updated are only there to keep evolution
170 # from failing.
Scott Bakerb24f2c32014-09-17 22:18:46 -0700171 created = models.DateTimeField(auto_now_add=True, default=timezone.now)
172 updated = models.DateTimeField(auto_now=True, default=timezone.now)
Scott Baker2a72eaf2014-12-01 21:42:59 -0800173 enacted = models.DateTimeField(null=True, blank=True, default=None)
Sapan Bhatia67400ad2015-01-23 15:59:55 +0000174 policed = models.DateTimeField(null=True, blank=True, default=None)
Sapan Bhatiac1945a62015-01-29 20:36:45 +0000175
176 # This is a scratchpad used by the Observer
177 backend_register = models.CharField(max_length=140,
178 default="{}", null=True)
179
Scott Baker85b98e72015-02-06 00:11:10 -0800180 backend_status = models.CharField(max_length=1024,
Sapan Bhatiacc9d9602015-01-23 16:07:24 +0000181 default="0 - Provisioning in progress")
Sapan Bhatiabcc18992014-04-29 10:32:14 -0400182 deleted = models.BooleanField(default=False)
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400183
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -0400184 class Meta:
Sapan Bhatia3089d832014-04-29 14:36:51 -0400185 # Changing abstract to False would require the managers of subclasses of
186 # PlCoreBase to be customized individually.
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -0400187 abstract = True
188 app_label = "core"
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400189
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -0400190 def __init__(self, *args, **kwargs):
191 super(PlCoreBase, self).__init__(*args, **kwargs)
Scott Bakercbfb6002014-10-03 00:32:37 -0700192 self._initial = self._dict # for DiffModelMixIn
Scott Baker5dc87a62014-09-23 22:41:17 -0700193 self.silent = False
Scott Baker13acdd62013-05-08 17:42:56 -0700194
Tony Mack5b061472014-02-04 07:57:10 -0500195 def can_update(self, user):
196 if user.is_readonly:
197 return False
198 if user.is_admin:
199 return True
Tony Mack5b061472014-02-04 07:57:10 -0500200
Scott Baker0119c152014-10-06 22:58:48 -0700201 return False
Scott Bakercbfb6002014-10-03 00:32:37 -0700202
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -0400203 def delete(self, *args, **kwds):
Scott Baker6ecd4262014-01-21 23:15:21 -0800204 # so we have something to give the observer
Sapan Bhatia77d1d892014-07-21 20:07:23 -0400205 purge = kwds.get('purge',False)
Scott Bakerc5b50602014-10-09 16:22:00 -0700206 if purge:
207 del kwds['purge']
Scott Baker0491f6f2014-09-23 16:04:36 -0700208 silent = kwds.get('silent',False)
Scott Bakerc5b50602014-10-09 16:22:00 -0700209 if silent:
210 del kwds['silent']
Sapan Bhatia77d1d892014-07-21 20:07:23 -0400211 try:
212 purge = purge or observer_disabled
213 except NameError:
214 pass
Scott Baker0491f6f2014-09-23 16:04:36 -0700215
Sapan Bhatia77d1d892014-07-21 20:07:23 -0400216 if (purge):
217 super(PlCoreBase, self).delete(*args, **kwds)
Sapan Bhatiac8602432014-04-29 20:33:51 -0400218 else:
219 self.deleted = True
220 self.enacted=None
Scott Baker0491f6f2014-09-23 16:04:36 -0700221 self.save(update_fields=['enacted','deleted'], silent=silent)
Sapan Bhatiadbaf1932013-09-03 11:28:52 -0400222
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -0400223 def save(self, *args, **kwargs):
Scott Baker5dc87a62014-09-23 22:41:17 -0700224 # let the user specify silence as either a kwarg or an instance varible
225 silent = self.silent
Scott Baker0491f6f2014-09-23 16:04:36 -0700226 if "silent" in kwargs:
Scott Baker5dc87a62014-09-23 22:41:17 -0700227 silent=silent or kwargs.pop("silent")
Scott Baker0491f6f2014-09-23 16:04:36 -0700228
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -0400229 super(PlCoreBase, self).save(*args, **kwargs)
Sapan Bhatiac8602432014-04-29 20:33:51 -0400230
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -0400231 # This is a no-op if observer_disabled is set
Scott Baker0491f6f2014-09-23 16:04:36 -0700232 if not silent:
233 notify_observer()
Sapan Bhatia66f4e612013-07-02 12:12:38 -0400234
Scott Baker165f70c2014-10-03 14:48:06 -0700235 self._initial = self._dict
Scott Baker13acdd62013-05-08 17:42:56 -0700236
Tony Mack5b061472014-02-04 07:57:10 -0500237 def save_by_user(self, user, *args, **kwds):
Scott Baker0bec56f2014-10-02 22:50:18 -0700238 if not self.can_update(user):
Scott Baker0119c152014-10-06 22:58:48 -0700239 if getattr(self, "_cant_update_fieldName", None) is not None:
240 raise PermissionDenied("You do not have permission to update field %s on object %s" % (self._cant_update_fieldName, self.__class__.__name__))
241 else:
242 raise PermissionDenied("You do not have permission to update %s objects" % self.__class__.__name__)
Scott Bakercbfb6002014-10-03 00:32:37 -0700243
Scott Baker0bec56f2014-10-02 22:50:18 -0700244 self.save(*args, **kwds)
Tony Mack5b061472014-02-04 07:57:10 -0500245
Tony Mack332ee1d2014-02-04 15:33:45 -0500246 def delete_by_user(self, user, *args, **kwds):
Scott Baker0bec56f2014-10-02 22:50:18 -0700247 if not self.can_update(user):
248 raise PermissionDenied("You do not have permission to delete %s objects" % self.__class__.__name__)
249 self.delete(*args, **kwds)
Tony Mack332ee1d2014-02-04 15:33:45 -0500250
Scott Baker165f70c2014-10-03 14:48:06 -0700251 @classmethod
252 def select_by_user(cls, user):
253 # This should be overridden by descendant classes that want to perform
254 # filtering of visible objects by user.
255 return cls.objects.all()
256
Sapan Bhatia998b32e2014-11-12 10:06:23 -0500257 @classmethod
258 def is_ephemeral(cls):
Sapan Bhatia40bbfd92014-11-12 10:38:23 -0500259 return cls in ephemeral_models
Sapan Bhatia998b32e2014-11-12 10:06:23 -0500260
Scott Bakere40fbd42014-11-25 00:49:17 -0800261
262
Scott Baker13acdd62013-05-08 17:42:56 -0700263
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400264
265