Scott Baker | 55b786a | 2015-02-02 14:28:35 -0800 | [diff] [blame] | 1 | import datetime |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 2 | import os |
Scott Baker | 65d5a9a | 2014-05-26 15:58:09 -0700 | [diff] [blame] | 3 | import sys |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 4 | from django.db import models |
Scott Baker | 13acdd6 | 2013-05-08 17:42:56 -0700 | [diff] [blame] | 5 | from django.forms.models import model_to_dict |
Scott Baker | c1c45f8 | 2014-01-21 16:23:51 -0800 | [diff] [blame] | 6 | from django.core.urlresolvers import reverse |
Scott Baker | 6ecd426 | 2014-01-21 23:15:21 -0800 | [diff] [blame] | 7 | from django.forms.models import model_to_dict |
Scott Baker | 02b5952 | 2014-09-17 22:18:46 -0700 | [diff] [blame] | 8 | from django.utils import timezone |
Scott Baker | e5f41b0 | 2014-10-02 22:50:18 -0700 | [diff] [blame] | 9 | from django.core.exceptions import PermissionDenied |
Sapan Bhatia | 3909719 | 2014-09-04 00:39:19 -0400 | [diff] [blame] | 10 | import model_policy |
Sapan Bhatia | f6613e3 | 2014-11-12 10:38:23 -0500 | [diff] [blame] | 11 | from model_autodeletion import ephemeral_models |
Scott Baker | e5f9d7d | 2015-02-10 18:24:20 -0800 | [diff] [blame] | 12 | from cgi import escape as html_escape |
Scott Baker | 9e99074 | 2014-03-19 22:14:58 -0700 | [diff] [blame] | 13 | |
| 14 | try: |
| 15 | # This is a no-op if observer_disabled is set to 1 in the config file |
| 16 | from observer import * |
| 17 | except: |
Scott Baker | 65d5a9a | 2014-05-26 15:58:09 -0700 | [diff] [blame] | 18 | print >> sys.stderr, "import of observer failed! printing traceback and disabling observer:" |
Scott Baker | 9e99074 | 2014-03-19 22:14:58 -0700 | [diff] [blame] | 19 | import traceback |
| 20 | traceback.print_exc() |
| 21 | |
| 22 | # guard against something failing |
Scott Baker | fd44dfc | 2014-05-23 13:20:53 -0700 | [diff] [blame] | 23 | def notify_observer(*args, **kwargs): |
Scott Baker | 9e99074 | 2014-03-19 22:14:58 -0700 | [diff] [blame] | 24 | pass |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 25 | |
Sapan Bhatia | 3089d83 | 2014-04-29 14:36:51 -0400 | [diff] [blame] | 26 | # This manager will be inherited by all subclasses because |
| 27 | # the core model is abstract. |
Sapan Bhatia | 15bf5ac | 2014-07-21 20:06:59 -0400 | [diff] [blame] | 28 | class PlCoreBaseDeletionManager(models.Manager): |
Scott Baker | 92f1422 | 2014-09-12 12:57:27 -0700 | [diff] [blame] | 29 | def get_queryset(self): |
Scott Baker | 02b5952 | 2014-09-17 22:18:46 -0700 | [diff] [blame] | 30 | parent=super(PlCoreBaseDeletionManager, self) |
| 31 | if hasattr(parent, "get_queryset"): |
| 32 | return parent.get_queryset().filter(deleted=True) |
| 33 | else: |
| 34 | return parent.get_query_set().filter(deleted=True) |
Sapan Bhatia | 15bf5ac | 2014-07-21 20:06:59 -0400 | [diff] [blame] | 35 | |
Scott Baker | 02b5952 | 2014-09-17 22:18:46 -0700 | [diff] [blame] | 36 | # deprecated in django 1.7 in favor of get_queryset(). |
Scott Baker | 92f1422 | 2014-09-12 12:57:27 -0700 | [diff] [blame] | 37 | def get_query_set(self): |
| 38 | return self.get_queryset() |
| 39 | |
Sapan Bhatia | 15bf5ac | 2014-07-21 20:06:59 -0400 | [diff] [blame] | 40 | # This manager will be inherited by all subclasses because |
| 41 | # the core model is abstract. |
Sapan Bhatia | 4eb663a | 2014-04-29 14:26:10 -0400 | [diff] [blame] | 42 | class PlCoreBaseManager(models.Manager): |
Scott Baker | 92f1422 | 2014-09-12 12:57:27 -0700 | [diff] [blame] | 43 | def get_queryset(self): |
Scott Baker | 02b5952 | 2014-09-17 22:18:46 -0700 | [diff] [blame] | 44 | parent=super(PlCoreBaseManager, self) |
| 45 | if hasattr(parent, "get_queryset"): |
| 46 | return parent.get_queryset().filter(deleted=False) |
| 47 | else: |
| 48 | return parent.get_query_set().filter(deleted=False) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 49 | |
Scott Baker | 02b5952 | 2014-09-17 22:18:46 -0700 | [diff] [blame] | 50 | # deprecated in django 1.7 in favor of get_queryset(). |
Scott Baker | 92f1422 | 2014-09-12 12:57:27 -0700 | [diff] [blame] | 51 | def get_query_set(self): |
| 52 | return self.get_queryset() |
| 53 | |
Scott Baker | f27edfe | 2015-02-10 15:44:30 -0800 | [diff] [blame] | 54 | class PlModelMixIn(object): |
Scott Baker | 1a6a390 | 2014-10-03 00:32:37 -0700 | [diff] [blame] | 55 | # Provides useful methods for computing which objects in a model have |
| 56 | # changed. Make sure to do self._initial = self._dict in the __init__ |
| 57 | # method. |
| 58 | |
Scott Baker | f27edfe | 2015-02-10 15:44:30 -0800 | [diff] [blame] | 59 | # Also includes useful utility, like getValidators |
| 60 | |
Scott Baker | 1a6a390 | 2014-10-03 00:32:37 -0700 | [diff] [blame] | 61 | # This is broken out of PlCoreBase into a Mixin so the User model can |
| 62 | # also make use of it. |
| 63 | |
| 64 | @property |
| 65 | def _dict(self): |
| 66 | return model_to_dict(self, fields=[field.name for field in |
| 67 | self._meta.fields]) |
| 68 | |
Scott Baker | 55b786a | 2015-02-02 14:28:35 -0800 | [diff] [blame] | 69 | def fields_differ(self,f1,f2): |
| 70 | if isinstance(f1,datetime.datetime) and isinstance(f2,datetime.datetime) and (timezone.is_aware(f1) != timezone.is_aware(f2)):
|
| 71 | return True
|
| 72 | else:
|
| 73 | return (f1 != f2)
|
| 74 | |
Scott Baker | 1a6a390 | 2014-10-03 00:32:37 -0700 | [diff] [blame] | 75 | @property |
| 76 | def diff(self): |
| 77 | d1 = self._initial |
| 78 | d2 = self._dict |
Scott Baker | 55b786a | 2015-02-02 14:28:35 -0800 | [diff] [blame] | 79 | diffs = [(k, (v, d2[k])) for k, v in d1.items() if self.fields_differ(v,d2[k])] |
Scott Baker | 1a6a390 | 2014-10-03 00:32:37 -0700 | [diff] [blame] | 80 | return dict(diffs) |
| 81 | |
| 82 | @property |
| 83 | def has_changed(self): |
| 84 | return bool(self.diff) |
| 85 | |
| 86 | @property |
| 87 | def changed_fields(self): |
| 88 | return self.diff.keys() |
| 89 | |
Scott Baker | 1a6a390 | 2014-10-03 00:32:37 -0700 | [diff] [blame] | 90 | def has_field_changed(self, field_name): |
| 91 | return field_name in self.diff.keys() |
| 92 | |
| 93 | def get_field_diff(self, field_name): |
| 94 | return self.diff.get(field_name, None) |
| 95 | |
Scott Baker | d48371e | 2014-11-25 11:35:19 -0800 | [diff] [blame] | 96 | #classmethod |
| 97 | def getValidators(cls): |
| 98 | """ primarily for REST API, return a dictionary of field names mapped |
| 99 | to lists of the type of validations that need to be applied to |
| 100 | those fields. |
| 101 | """ |
| 102 | validators = {} |
| 103 | for field in cls._meta.fields: |
| 104 | l = [] |
| 105 | if field.blank==False: |
| 106 | l.append("notBlank") |
Scott Baker | 0d4ecd9 | 2014-11-26 00:53:19 -0800 | [diff] [blame] | 107 | if field.__class__.__name__=="URLField": |
| 108 | l.append("url") |
Scott Baker | d48371e | 2014-11-25 11:35:19 -0800 | [diff] [blame] | 109 | validators[field.name] = l |
| 110 | return validators |
| 111 | |
Scott Baker | e5f9d7d | 2015-02-10 18:24:20 -0800 | [diff] [blame] | 112 | def get_backend_icon(self): |
| 113 | # returns (icon_name, tooltip) |
| 114 | if (self.enacted is not None) and self.enacted >= self.updated or self.backend_status.startswith("1 -"): |
| 115 | return ("success", "successfully enacted") |
| 116 | else: |
| 117 | if ((self.backend_status is not None) and self.backend_status.startswith("0 -")) or self.backend_status == "Provisioning in progress" or self.backend_status=="": |
| 118 | return ("clock", html_escape(self.backend_status, quote=True)) |
| 119 | else: |
| 120 | return ("error", html_escape(self.backend_status, quote=True)) |
| 121 | |
Scott Baker | f27edfe | 2015-02-10 15:44:30 -0800 | [diff] [blame] | 122 | class PlCoreBase(models.Model, PlModelMixIn): |
Sapan Bhatia | 4eb663a | 2014-04-29 14:26:10 -0400 | [diff] [blame] | 123 | objects = PlCoreBaseManager() |
Sapan Bhatia | 15bf5ac | 2014-07-21 20:06:59 -0400 | [diff] [blame] | 124 | deleted_objects = PlCoreBaseDeletionManager() |
| 125 | |
| 126 | # default values for created and updated are only there to keep evolution |
| 127 | # from failing. |
Scott Baker | 02b5952 | 2014-09-17 22:18:46 -0700 | [diff] [blame] | 128 | created = models.DateTimeField(auto_now_add=True, default=timezone.now) |
| 129 | updated = models.DateTimeField(auto_now=True, default=timezone.now) |
Scott Baker | 36286b2 | 2014-12-01 21:42:59 -0800 | [diff] [blame] | 130 | enacted = models.DateTimeField(null=True, blank=True, default=None) |
Sapan Bhatia | ab66b7d | 2015-01-23 15:59:55 +0000 | [diff] [blame] | 131 | policed = models.DateTimeField(null=True, blank=True, default=None) |
Sapan Bhatia | 3117619 | 2015-01-29 20:36:45 +0000 | [diff] [blame] | 132 | |
| 133 | # This is a scratchpad used by the Observer |
| 134 | backend_register = models.CharField(max_length=140, |
| 135 | default="{}", null=True) |
| 136 | |
Scott Baker | 19c7796 | 2015-02-06 00:11:10 -0800 | [diff] [blame] | 137 | backend_status = models.CharField(max_length=1024, |
Sapan Bhatia | 030cede | 2015-01-23 16:07:24 +0000 | [diff] [blame] | 138 | default="0 - Provisioning in progress") |
Sapan Bhatia | bcc1899 | 2014-04-29 10:32:14 -0400 | [diff] [blame] | 139 | deleted = models.BooleanField(default=False) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 140 | |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 141 | class Meta: |
Sapan Bhatia | 3089d83 | 2014-04-29 14:36:51 -0400 | [diff] [blame] | 142 | # Changing abstract to False would require the managers of subclasses of |
| 143 | # PlCoreBase to be customized individually. |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 144 | abstract = True |
| 145 | app_label = "core" |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 146 | |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 147 | def __init__(self, *args, **kwargs): |
| 148 | super(PlCoreBase, self).__init__(*args, **kwargs) |
Scott Baker | f27edfe | 2015-02-10 15:44:30 -0800 | [diff] [blame] | 149 | self._initial = self._dict # for PlModelMixIn |
Scott Baker | f3f895c | 2014-09-23 22:41:17 -0700 | [diff] [blame] | 150 | self.silent = False |
Scott Baker | 13acdd6 | 2013-05-08 17:42:56 -0700 | [diff] [blame] | 151 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 152 | def can_update(self, user): |
Tony Mack | 3428e6e | 2015-02-08 21:38:41 -0500 | [diff] [blame] | 153 | return user.can_update_root() |
Scott Baker | 1a6a390 | 2014-10-03 00:32:37 -0700 | [diff] [blame] | 154 | |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 155 | def delete(self, *args, **kwds): |
Scott Baker | 6ecd426 | 2014-01-21 23:15:21 -0800 | [diff] [blame] | 156 | # so we have something to give the observer |
Sapan Bhatia | 77d1d89 | 2014-07-21 20:07:23 -0400 | [diff] [blame] | 157 | purge = kwds.get('purge',False) |
Scott Baker | 3d2d3e4 | 2014-10-09 16:22:00 -0700 | [diff] [blame] | 158 | if purge: |
| 159 | del kwds['purge'] |
Scott Baker | 6594bea | 2014-09-23 16:04:36 -0700 | [diff] [blame] | 160 | silent = kwds.get('silent',False) |
Scott Baker | 3d2d3e4 | 2014-10-09 16:22:00 -0700 | [diff] [blame] | 161 | if silent: |
| 162 | del kwds['silent'] |
Sapan Bhatia | 77d1d89 | 2014-07-21 20:07:23 -0400 | [diff] [blame] | 163 | try: |
| 164 | purge = purge or observer_disabled |
| 165 | except NameError: |
| 166 | pass |
Scott Baker | 6594bea | 2014-09-23 16:04:36 -0700 | [diff] [blame] | 167 | |
Sapan Bhatia | 77d1d89 | 2014-07-21 20:07:23 -0400 | [diff] [blame] | 168 | if (purge): |
| 169 | super(PlCoreBase, self).delete(*args, **kwds) |
Sapan Bhatia | c860243 | 2014-04-29 20:33:51 -0400 | [diff] [blame] | 170 | else: |
| 171 | self.deleted = True |
| 172 | self.enacted=None |
Scott Baker | 6594bea | 2014-09-23 16:04:36 -0700 | [diff] [blame] | 173 | self.save(update_fields=['enacted','deleted'], silent=silent) |
Sapan Bhatia | dbaf193 | 2013-09-03 11:28:52 -0400 | [diff] [blame] | 174 | |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 175 | def save(self, *args, **kwargs): |
Scott Baker | f3f895c | 2014-09-23 22:41:17 -0700 | [diff] [blame] | 176 | # let the user specify silence as either a kwarg or an instance varible |
| 177 | silent = self.silent |
Scott Baker | 6594bea | 2014-09-23 16:04:36 -0700 | [diff] [blame] | 178 | if "silent" in kwargs: |
Scott Baker | f3f895c | 2014-09-23 22:41:17 -0700 | [diff] [blame] | 179 | silent=silent or kwargs.pop("silent") |
Scott Baker | 6594bea | 2014-09-23 16:04:36 -0700 | [diff] [blame] | 180 | |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 181 | super(PlCoreBase, self).save(*args, **kwargs) |
Sapan Bhatia | c860243 | 2014-04-29 20:33:51 -0400 | [diff] [blame] | 182 | |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 183 | # This is a no-op if observer_disabled is set |
Sapan Bhatia | 03388fa | 2015-02-10 11:46:51 -0500 | [diff] [blame] | 184 | # if not silent: |
| 185 | # notify_observer() |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 186 | |
Scott Baker | 5e5f455 | 2014-10-03 14:48:06 -0700 | [diff] [blame] | 187 | self._initial = self._dict |
Scott Baker | 13acdd6 | 2013-05-08 17:42:56 -0700 | [diff] [blame] | 188 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 189 | def save_by_user(self, user, *args, **kwds): |
Scott Baker | e5f41b0 | 2014-10-02 22:50:18 -0700 | [diff] [blame] | 190 | if not self.can_update(user): |
Scott Baker | 1bffe94 | 2014-10-06 22:58:48 -0700 | [diff] [blame] | 191 | if getattr(self, "_cant_update_fieldName", None) is not None: |
| 192 | raise PermissionDenied("You do not have permission to update field %s on object %s" % (self._cant_update_fieldName, self.__class__.__name__)) |
| 193 | else: |
| 194 | raise PermissionDenied("You do not have permission to update %s objects" % self.__class__.__name__) |
Scott Baker | 1a6a390 | 2014-10-03 00:32:37 -0700 | [diff] [blame] | 195 | |
Scott Baker | e5f41b0 | 2014-10-02 22:50:18 -0700 | [diff] [blame] | 196 | self.save(*args, **kwds) |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 197 | |
Tony Mack | 332ee1d | 2014-02-04 15:33:45 -0500 | [diff] [blame] | 198 | def delete_by_user(self, user, *args, **kwds): |
Scott Baker | e5f41b0 | 2014-10-02 22:50:18 -0700 | [diff] [blame] | 199 | if not self.can_update(user): |
| 200 | raise PermissionDenied("You do not have permission to delete %s objects" % self.__class__.__name__) |
| 201 | self.delete(*args, **kwds) |
Tony Mack | 332ee1d | 2014-02-04 15:33:45 -0500 | [diff] [blame] | 202 | |
Scott Baker | 5e5f455 | 2014-10-03 14:48:06 -0700 | [diff] [blame] | 203 | @classmethod |
| 204 | def select_by_user(cls, user): |
| 205 | # This should be overridden by descendant classes that want to perform |
| 206 | # filtering of visible objects by user. |
| 207 | return cls.objects.all() |
| 208 | |
Sapan Bhatia | e33b9dc | 2014-11-12 10:06:23 -0500 | [diff] [blame] | 209 | @classmethod |
| 210 | def is_ephemeral(cls): |
Sapan Bhatia | f6613e3 | 2014-11-12 10:38:23 -0500 | [diff] [blame] | 211 | return cls in ephemeral_models |
Sapan Bhatia | e33b9dc | 2014-11-12 10:06:23 -0500 | [diff] [blame] | 212 | |
Scott Baker | 17dbb02 | 2014-11-25 00:49:17 -0800 | [diff] [blame] | 213 | |
| 214 | |
Scott Baker | 13acdd6 | 2013-05-08 17:42:56 -0700 | [diff] [blame] | 215 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 216 | |
| 217 | |