Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 1 | import os |
Scott Baker | 65d5a9a | 2014-05-26 15:58:09 -0700 | [diff] [blame] | 2 | import sys |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 3 | from django.db import models |
Scott Baker | 13acdd6 | 2013-05-08 17:42:56 -0700 | [diff] [blame] | 4 | from django.forms.models import model_to_dict |
Scott Baker | c1c45f8 | 2014-01-21 16:23:51 -0800 | [diff] [blame] | 5 | from django.core.urlresolvers import reverse |
Scott Baker | 6ecd426 | 2014-01-21 23:15:21 -0800 | [diff] [blame] | 6 | from django.forms.models import model_to_dict |
Scott Baker | b24f2c3 | 2014-09-17 22:18:46 -0700 | [diff] [blame] | 7 | from django.utils import timezone |
Scott Baker | 0bec56f | 2014-10-02 22:50:18 -0700 | [diff] [blame] | 8 | from django.core.exceptions import PermissionDenied |
Sapan Bhatia | bad6774 | 2014-09-04 00:39:19 -0400 | [diff] [blame] | 9 | import model_policy |
Sapan Bhatia | 40bbfd9 | 2014-11-12 10:38:23 -0500 | [diff] [blame] | 10 | from model_autodeletion import ephemeral_models |
Scott Baker | 9e99074 | 2014-03-19 22:14:58 -0700 | [diff] [blame] | 11 | |
| 12 | try: |
| 13 | # This is a no-op if observer_disabled is set to 1 in the config file |
| 14 | from observer import * |
| 15 | except: |
Scott Baker | 65d5a9a | 2014-05-26 15:58:09 -0700 | [diff] [blame] | 16 | print >> sys.stderr, "import of observer failed! printing traceback and disabling observer:" |
Scott Baker | 9e99074 | 2014-03-19 22:14:58 -0700 | [diff] [blame] | 17 | import traceback |
| 18 | traceback.print_exc() |
| 19 | |
| 20 | # guard against something failing |
Scott Baker | fd44dfc | 2014-05-23 13:20:53 -0700 | [diff] [blame] | 21 | def notify_observer(*args, **kwargs): |
Scott Baker | 9e99074 | 2014-03-19 22:14:58 -0700 | [diff] [blame] | 22 | pass |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 23 | |
Sapan Bhatia | 3089d83 | 2014-04-29 14:36:51 -0400 | [diff] [blame] | 24 | # This manager will be inherited by all subclasses because |
| 25 | # the core model is abstract. |
Sapan Bhatia | 15bf5ac | 2014-07-21 20:06:59 -0400 | [diff] [blame] | 26 | class PlCoreBaseDeletionManager(models.Manager): |
Scott Baker | b08d656 | 2014-09-12 12:57:27 -0700 | [diff] [blame] | 27 | def get_queryset(self): |
Scott Baker | b24f2c3 | 2014-09-17 22:18:46 -0700 | [diff] [blame] | 28 | 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 Bhatia | 15bf5ac | 2014-07-21 20:06:59 -0400 | [diff] [blame] | 33 | |
Scott Baker | b24f2c3 | 2014-09-17 22:18:46 -0700 | [diff] [blame] | 34 | # deprecated in django 1.7 in favor of get_queryset(). |
Scott Baker | b08d656 | 2014-09-12 12:57:27 -0700 | [diff] [blame] | 35 | def get_query_set(self): |
| 36 | return self.get_queryset() |
| 37 | |
Sapan Bhatia | 15bf5ac | 2014-07-21 20:06:59 -0400 | [diff] [blame] | 38 | # This manager will be inherited by all subclasses because |
| 39 | # the core model is abstract. |
Sapan Bhatia | 4eb663a | 2014-04-29 14:26:10 -0400 | [diff] [blame] | 40 | class PlCoreBaseManager(models.Manager): |
Scott Baker | b08d656 | 2014-09-12 12:57:27 -0700 | [diff] [blame] | 41 | def get_queryset(self): |
Scott Baker | b24f2c3 | 2014-09-17 22:18:46 -0700 | [diff] [blame] | 42 | 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 Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 47 | |
Scott Baker | b24f2c3 | 2014-09-17 22:18:46 -0700 | [diff] [blame] | 48 | # deprecated in django 1.7 in favor of get_queryset(). |
Scott Baker | b08d656 | 2014-09-12 12:57:27 -0700 | [diff] [blame] | 49 | def get_query_set(self): |
| 50 | return self.get_queryset() |
| 51 | |
Scott Baker | cbfb600 | 2014-10-03 00:32:37 -0700 | [diff] [blame] | 52 | class 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 Baker | cbfb600 | 2014-10-03 00:32:37 -0700 | [diff] [blame] | 80 | 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 Baker | 56f799b | 2014-11-25 11:35:19 -0800 | [diff] [blame] | 86 | #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") |
| 97 | validators[field.name] = l |
| 98 | return validators |
| 99 | |
Scott Baker | 46e50af | 2014-11-06 17:33:27 -0800 | [diff] [blame] | 100 | class PlCoreBase(models.Model): # , DiffModelMixIn): |
Sapan Bhatia | 4eb663a | 2014-04-29 14:26:10 -0400 | [diff] [blame] | 101 | objects = PlCoreBaseManager() |
Sapan Bhatia | 15bf5ac | 2014-07-21 20:06:59 -0400 | [diff] [blame] | 102 | deleted_objects = PlCoreBaseDeletionManager() |
| 103 | |
Scott Baker | 46e50af | 2014-11-06 17:33:27 -0800 | [diff] [blame] | 104 | # ---- copy stuff from DiffModelMixin ---- |
| 105 | |
| 106 | # XXX Django fails miserably when trying to create initial migrations when |
| 107 | # DiffModelMixin is used. So, until we figure out what's wrong, |
| 108 | # just copied the guts of DiffModelMixIn here. |
| 109 | |
| 110 | @property |
| 111 | def _dict(self): |
| 112 | return model_to_dict(self, fields=[field.name for field in |
| 113 | self._meta.fields]) |
| 114 | |
| 115 | @property |
| 116 | def diff(self): |
| 117 | d1 = self._initial |
| 118 | d2 = self._dict |
| 119 | diffs = [(k, (v, d2[k])) for k, v in d1.items() if v != d2[k]] |
| 120 | return dict(diffs) |
| 121 | |
| 122 | @property |
| 123 | def has_changed(self): |
| 124 | return bool(self.diff) |
| 125 | |
| 126 | @property |
| 127 | def changed_fields(self): |
| 128 | return self.diff.keys() |
| 129 | |
| 130 | def has_field_changed(self, field_name): |
| 131 | return field_name in self.diff.keys() |
| 132 | |
| 133 | def get_field_diff(self, field_name): |
| 134 | return self.diff.get(field_name, None) |
Scott Baker | 56f799b | 2014-11-25 11:35:19 -0800 | [diff] [blame] | 135 | |
| 136 | #classmethod |
| 137 | def getValidators(cls): |
| 138 | """ primarily for REST API, return a dictionary of field names mapped |
| 139 | to lists of the type of validations that need to be applied to |
| 140 | those fields. |
| 141 | """ |
| 142 | validators = {} |
| 143 | for field in cls._meta.fields: |
| 144 | l = [] |
| 145 | if field.blank==False: |
| 146 | l.append("notBlank") |
| 147 | validators[field.name] = l |
| 148 | return validators |
| 149 | |
Scott Baker | 46e50af | 2014-11-06 17:33:27 -0800 | [diff] [blame] | 150 | # ---- end copy stuff from DiffModelMixin ---- |
| 151 | |
Sapan Bhatia | 15bf5ac | 2014-07-21 20:06:59 -0400 | [diff] [blame] | 152 | # default values for created and updated are only there to keep evolution |
| 153 | # from failing. |
Scott Baker | b24f2c3 | 2014-09-17 22:18:46 -0700 | [diff] [blame] | 154 | created = models.DateTimeField(auto_now_add=True, default=timezone.now) |
| 155 | updated = models.DateTimeField(auto_now=True, default=timezone.now) |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 156 | enacted = models.DateTimeField(null=True, default=None) |
Sapan Bhatia | 34aee75 | 2014-04-28 21:06:39 -0400 | [diff] [blame] | 157 | backend_status = models.CharField(max_length=140, |
Sapan Bhatia | d507f43 | 2014-04-29 00:41:39 -0400 | [diff] [blame] | 158 | default="Provisioning in progress") |
Sapan Bhatia | bcc1899 | 2014-04-29 10:32:14 -0400 | [diff] [blame] | 159 | deleted = models.BooleanField(default=False) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 160 | |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 161 | class Meta: |
Sapan Bhatia | 3089d83 | 2014-04-29 14:36:51 -0400 | [diff] [blame] | 162 | # Changing abstract to False would require the managers of subclasses of |
| 163 | # PlCoreBase to be customized individually. |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 164 | abstract = True |
| 165 | app_label = "core" |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 166 | |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 167 | def __init__(self, *args, **kwargs): |
| 168 | super(PlCoreBase, self).__init__(*args, **kwargs) |
Scott Baker | cbfb600 | 2014-10-03 00:32:37 -0700 | [diff] [blame] | 169 | self._initial = self._dict # for DiffModelMixIn |
Scott Baker | 5dc87a6 | 2014-09-23 22:41:17 -0700 | [diff] [blame] | 170 | self.silent = False |
Scott Baker | 13acdd6 | 2013-05-08 17:42:56 -0700 | [diff] [blame] | 171 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 172 | def can_update(self, user): |
| 173 | if user.is_readonly: |
| 174 | return False |
| 175 | if user.is_admin: |
| 176 | return True |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 177 | |
Scott Baker | 0119c15 | 2014-10-06 22:58:48 -0700 | [diff] [blame] | 178 | return False |
Scott Baker | cbfb600 | 2014-10-03 00:32:37 -0700 | [diff] [blame] | 179 | |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 180 | def delete(self, *args, **kwds): |
Scott Baker | 6ecd426 | 2014-01-21 23:15:21 -0800 | [diff] [blame] | 181 | # so we have something to give the observer |
Sapan Bhatia | 77d1d89 | 2014-07-21 20:07:23 -0400 | [diff] [blame] | 182 | purge = kwds.get('purge',False) |
Scott Baker | c5b5060 | 2014-10-09 16:22:00 -0700 | [diff] [blame] | 183 | if purge: |
| 184 | del kwds['purge'] |
Scott Baker | 0491f6f | 2014-09-23 16:04:36 -0700 | [diff] [blame] | 185 | silent = kwds.get('silent',False) |
Scott Baker | c5b5060 | 2014-10-09 16:22:00 -0700 | [diff] [blame] | 186 | if silent: |
| 187 | del kwds['silent'] |
Sapan Bhatia | 77d1d89 | 2014-07-21 20:07:23 -0400 | [diff] [blame] | 188 | try: |
| 189 | purge = purge or observer_disabled |
| 190 | except NameError: |
| 191 | pass |
Scott Baker | 0491f6f | 2014-09-23 16:04:36 -0700 | [diff] [blame] | 192 | |
Sapan Bhatia | 77d1d89 | 2014-07-21 20:07:23 -0400 | [diff] [blame] | 193 | if (purge): |
| 194 | super(PlCoreBase, self).delete(*args, **kwds) |
Sapan Bhatia | c860243 | 2014-04-29 20:33:51 -0400 | [diff] [blame] | 195 | else: |
| 196 | self.deleted = True |
| 197 | self.enacted=None |
Scott Baker | 0491f6f | 2014-09-23 16:04:36 -0700 | [diff] [blame] | 198 | self.save(update_fields=['enacted','deleted'], silent=silent) |
Sapan Bhatia | dbaf193 | 2013-09-03 11:28:52 -0400 | [diff] [blame] | 199 | |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 200 | def save(self, *args, **kwargs): |
Scott Baker | 5dc87a6 | 2014-09-23 22:41:17 -0700 | [diff] [blame] | 201 | # let the user specify silence as either a kwarg or an instance varible |
| 202 | silent = self.silent |
Scott Baker | 0491f6f | 2014-09-23 16:04:36 -0700 | [diff] [blame] | 203 | if "silent" in kwargs: |
Scott Baker | 5dc87a6 | 2014-09-23 22:41:17 -0700 | [diff] [blame] | 204 | silent=silent or kwargs.pop("silent") |
Scott Baker | 0491f6f | 2014-09-23 16:04:36 -0700 | [diff] [blame] | 205 | |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 206 | super(PlCoreBase, self).save(*args, **kwargs) |
Sapan Bhatia | c860243 | 2014-04-29 20:33:51 -0400 | [diff] [blame] | 207 | |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 208 | # This is a no-op if observer_disabled is set |
Scott Baker | 0491f6f | 2014-09-23 16:04:36 -0700 | [diff] [blame] | 209 | if not silent: |
| 210 | notify_observer() |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 211 | |
Scott Baker | 165f70c | 2014-10-03 14:48:06 -0700 | [diff] [blame] | 212 | self._initial = self._dict |
Scott Baker | 13acdd6 | 2013-05-08 17:42:56 -0700 | [diff] [blame] | 213 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 214 | def save_by_user(self, user, *args, **kwds): |
Scott Baker | 0bec56f | 2014-10-02 22:50:18 -0700 | [diff] [blame] | 215 | if not self.can_update(user): |
Scott Baker | 0119c15 | 2014-10-06 22:58:48 -0700 | [diff] [blame] | 216 | if getattr(self, "_cant_update_fieldName", None) is not None: |
| 217 | raise PermissionDenied("You do not have permission to update field %s on object %s" % (self._cant_update_fieldName, self.__class__.__name__)) |
| 218 | else: |
| 219 | raise PermissionDenied("You do not have permission to update %s objects" % self.__class__.__name__) |
Scott Baker | cbfb600 | 2014-10-03 00:32:37 -0700 | [diff] [blame] | 220 | |
Scott Baker | 0bec56f | 2014-10-02 22:50:18 -0700 | [diff] [blame] | 221 | self.save(*args, **kwds) |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 222 | |
Tony Mack | 332ee1d | 2014-02-04 15:33:45 -0500 | [diff] [blame] | 223 | def delete_by_user(self, user, *args, **kwds): |
Scott Baker | 0bec56f | 2014-10-02 22:50:18 -0700 | [diff] [blame] | 224 | if not self.can_update(user): |
| 225 | raise PermissionDenied("You do not have permission to delete %s objects" % self.__class__.__name__) |
| 226 | self.delete(*args, **kwds) |
Tony Mack | 332ee1d | 2014-02-04 15:33:45 -0500 | [diff] [blame] | 227 | |
Scott Baker | 165f70c | 2014-10-03 14:48:06 -0700 | [diff] [blame] | 228 | @classmethod |
| 229 | def select_by_user(cls, user): |
| 230 | # This should be overridden by descendant classes that want to perform |
| 231 | # filtering of visible objects by user. |
| 232 | return cls.objects.all() |
| 233 | |
Sapan Bhatia | 998b32e | 2014-11-12 10:06:23 -0500 | [diff] [blame] | 234 | @classmethod |
| 235 | def is_ephemeral(cls): |
Sapan Bhatia | 40bbfd9 | 2014-11-12 10:38:23 -0500 | [diff] [blame] | 236 | return cls in ephemeral_models |
Sapan Bhatia | 998b32e | 2014-11-12 10:06:23 -0500 | [diff] [blame] | 237 | |
Scott Baker | e40fbd4 | 2014-11-25 00:49:17 -0800 | [diff] [blame] | 238 | |
| 239 | |
Scott Baker | 13acdd6 | 2013-05-08 17:42:56 -0700 | [diff] [blame] | 240 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 241 | |
| 242 | |