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 | 02b5952 | 2014-09-17 22:18:46 -0700 | [diff] [blame] | 7 | from django.utils import timezone |
Scott Baker | e5f41b0 | 2014-10-02 22:50:18 -0700 | [diff] [blame] | 8 | from django.core.exceptions import PermissionDenied |
Sapan Bhatia | 3909719 | 2014-09-04 00:39:19 -0400 | [diff] [blame] | 9 | import model_policy |
Scott Baker | 9e99074 | 2014-03-19 22:14:58 -0700 | [diff] [blame] | 10 | |
| 11 | try: |
| 12 | # This is a no-op if observer_disabled is set to 1 in the config file |
| 13 | from observer import * |
| 14 | except: |
Scott Baker | 65d5a9a | 2014-05-26 15:58:09 -0700 | [diff] [blame] | 15 | print >> sys.stderr, "import of observer failed! printing traceback and disabling observer:" |
Scott Baker | 9e99074 | 2014-03-19 22:14:58 -0700 | [diff] [blame] | 16 | import traceback |
| 17 | traceback.print_exc() |
| 18 | |
| 19 | # guard against something failing |
Scott Baker | fd44dfc | 2014-05-23 13:20:53 -0700 | [diff] [blame] | 20 | def notify_observer(*args, **kwargs): |
Scott Baker | 9e99074 | 2014-03-19 22:14:58 -0700 | [diff] [blame] | 21 | pass |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 22 | |
Sapan Bhatia | 3089d83 | 2014-04-29 14:36:51 -0400 | [diff] [blame] | 23 | # This manager will be inherited by all subclasses because |
| 24 | # the core model is abstract. |
Sapan Bhatia | 15bf5ac | 2014-07-21 20:06:59 -0400 | [diff] [blame] | 25 | class PlCoreBaseDeletionManager(models.Manager): |
Scott Baker | 92f1422 | 2014-09-12 12:57:27 -0700 | [diff] [blame] | 26 | def get_queryset(self): |
Scott Baker | 02b5952 | 2014-09-17 22:18:46 -0700 | [diff] [blame] | 27 | parent=super(PlCoreBaseDeletionManager, self) |
| 28 | if hasattr(parent, "get_queryset"): |
| 29 | return parent.get_queryset().filter(deleted=True) |
| 30 | else: |
| 31 | return parent.get_query_set().filter(deleted=True) |
Sapan Bhatia | 15bf5ac | 2014-07-21 20:06:59 -0400 | [diff] [blame] | 32 | |
Scott Baker | 02b5952 | 2014-09-17 22:18:46 -0700 | [diff] [blame] | 33 | # deprecated in django 1.7 in favor of get_queryset(). |
Scott Baker | 92f1422 | 2014-09-12 12:57:27 -0700 | [diff] [blame] | 34 | def get_query_set(self): |
| 35 | return self.get_queryset() |
| 36 | |
Sapan Bhatia | 15bf5ac | 2014-07-21 20:06:59 -0400 | [diff] [blame] | 37 | # This manager will be inherited by all subclasses because |
| 38 | # the core model is abstract. |
Sapan Bhatia | 4eb663a | 2014-04-29 14:26:10 -0400 | [diff] [blame] | 39 | class PlCoreBaseManager(models.Manager): |
Scott Baker | 92f1422 | 2014-09-12 12:57:27 -0700 | [diff] [blame] | 40 | def get_queryset(self): |
Scott Baker | 02b5952 | 2014-09-17 22:18:46 -0700 | [diff] [blame] | 41 | parent=super(PlCoreBaseManager, self) |
| 42 | if hasattr(parent, "get_queryset"): |
| 43 | return parent.get_queryset().filter(deleted=False) |
| 44 | else: |
| 45 | return parent.get_query_set().filter(deleted=False) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 46 | |
Scott Baker | 02b5952 | 2014-09-17 22:18:46 -0700 | [diff] [blame] | 47 | # deprecated in django 1.7 in favor of get_queryset(). |
Scott Baker | 92f1422 | 2014-09-12 12:57:27 -0700 | [diff] [blame] | 48 | def get_query_set(self): |
| 49 | return self.get_queryset() |
| 50 | |
Scott Baker | 1a6a390 | 2014-10-03 00:32:37 -0700 | [diff] [blame] | 51 | class DiffModelMixIn: |
| 52 | # Provides useful methods for computing which objects in a model have |
| 53 | # changed. Make sure to do self._initial = self._dict in the __init__ |
| 54 | # method. |
| 55 | |
| 56 | # This is broken out of PlCoreBase into a Mixin so the User model can |
| 57 | # also make use of it. |
| 58 | |
| 59 | @property |
| 60 | def _dict(self): |
| 61 | return model_to_dict(self, fields=[field.name for field in |
| 62 | self._meta.fields]) |
| 63 | |
| 64 | @property |
| 65 | def diff(self): |
| 66 | d1 = self._initial |
| 67 | d2 = self._dict |
| 68 | diffs = [(k, (v, d2[k])) for k, v in d1.items() if v != d2[k]] |
| 69 | return dict(diffs) |
| 70 | |
| 71 | @property |
| 72 | def has_changed(self): |
| 73 | return bool(self.diff) |
| 74 | |
| 75 | @property |
| 76 | def changed_fields(self): |
| 77 | return self.diff.keys() |
| 78 | |
| 79 | @property |
| 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 | |
| 86 | |
| 87 | class PlCoreBase(models.Model, DiffModelMixIn): |
Sapan Bhatia | 4eb663a | 2014-04-29 14:26:10 -0400 | [diff] [blame] | 88 | objects = PlCoreBaseManager() |
Sapan Bhatia | 15bf5ac | 2014-07-21 20:06:59 -0400 | [diff] [blame] | 89 | deleted_objects = PlCoreBaseDeletionManager() |
| 90 | |
| 91 | # default values for created and updated are only there to keep evolution |
| 92 | # from failing. |
Scott Baker | 02b5952 | 2014-09-17 22:18:46 -0700 | [diff] [blame] | 93 | created = models.DateTimeField(auto_now_add=True, default=timezone.now) |
| 94 | updated = models.DateTimeField(auto_now=True, default=timezone.now) |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 95 | enacted = models.DateTimeField(null=True, default=None) |
Sapan Bhatia | 34aee75 | 2014-04-28 21:06:39 -0400 | [diff] [blame] | 96 | backend_status = models.CharField(max_length=140, |
Sapan Bhatia | d507f43 | 2014-04-29 00:41:39 -0400 | [diff] [blame] | 97 | default="Provisioning in progress") |
Sapan Bhatia | bcc1899 | 2014-04-29 10:32:14 -0400 | [diff] [blame] | 98 | deleted = models.BooleanField(default=False) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 99 | |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 100 | class Meta: |
Sapan Bhatia | 3089d83 | 2014-04-29 14:36:51 -0400 | [diff] [blame] | 101 | # Changing abstract to False would require the managers of subclasses of |
| 102 | # PlCoreBase to be customized individually. |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 103 | abstract = True |
| 104 | app_label = "core" |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 105 | |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 106 | def __init__(self, *args, **kwargs): |
| 107 | super(PlCoreBase, self).__init__(*args, **kwargs) |
Scott Baker | 1a6a390 | 2014-10-03 00:32:37 -0700 | [diff] [blame] | 108 | self._initial = self._dict # for DiffModelMixIn |
Scott Baker | f3f895c | 2014-09-23 22:41:17 -0700 | [diff] [blame] | 109 | self.silent = False |
Scott Baker | 13acdd6 | 2013-05-08 17:42:56 -0700 | [diff] [blame] | 110 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 111 | def can_update(self, user): |
| 112 | if user.is_readonly: |
| 113 | return False |
| 114 | if user.is_admin: |
| 115 | return True |
| 116 | return False |
| 117 | |
Scott Baker | 1a6a390 | 2014-10-03 00:32:37 -0700 | [diff] [blame] | 118 | def can_update_field(self, user, fieldName): |
| 119 | # Give us the opportunity to implement fine-grained permission checking. |
| 120 | # Default to True, and let can_update() permit or deny the whole object. |
| 121 | return True |
| 122 | |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 123 | def delete(self, *args, **kwds): |
Scott Baker | 6ecd426 | 2014-01-21 23:15:21 -0800 | [diff] [blame] | 124 | # so we have something to give the observer |
Sapan Bhatia | 77d1d89 | 2014-07-21 20:07:23 -0400 | [diff] [blame] | 125 | purge = kwds.get('purge',False) |
Scott Baker | 6594bea | 2014-09-23 16:04:36 -0700 | [diff] [blame] | 126 | silent = kwds.get('silent',False) |
Sapan Bhatia | 77d1d89 | 2014-07-21 20:07:23 -0400 | [diff] [blame] | 127 | try: |
| 128 | purge = purge or observer_disabled |
| 129 | except NameError: |
| 130 | pass |
Scott Baker | 6594bea | 2014-09-23 16:04:36 -0700 | [diff] [blame] | 131 | |
Sapan Bhatia | 77d1d89 | 2014-07-21 20:07:23 -0400 | [diff] [blame] | 132 | if (purge): |
Sapan Bhatia | ad2c4a1 | 2014-07-22 01:21:05 -0400 | [diff] [blame] | 133 | del kwds['purge'] |
Sapan Bhatia | 77d1d89 | 2014-07-21 20:07:23 -0400 | [diff] [blame] | 134 | super(PlCoreBase, self).delete(*args, **kwds) |
Sapan Bhatia | c860243 | 2014-04-29 20:33:51 -0400 | [diff] [blame] | 135 | else: |
| 136 | self.deleted = True |
| 137 | self.enacted=None |
Scott Baker | 6594bea | 2014-09-23 16:04:36 -0700 | [diff] [blame] | 138 | self.save(update_fields=['enacted','deleted'], silent=silent) |
Sapan Bhatia | dbaf193 | 2013-09-03 11:28:52 -0400 | [diff] [blame] | 139 | |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 140 | def save(self, *args, **kwargs): |
Scott Baker | f3f895c | 2014-09-23 22:41:17 -0700 | [diff] [blame] | 141 | # let the user specify silence as either a kwarg or an instance varible |
| 142 | silent = self.silent |
Scott Baker | 6594bea | 2014-09-23 16:04:36 -0700 | [diff] [blame] | 143 | if "silent" in kwargs: |
Scott Baker | f3f895c | 2014-09-23 22:41:17 -0700 | [diff] [blame] | 144 | silent=silent or kwargs.pop("silent") |
Scott Baker | 6594bea | 2014-09-23 16:04:36 -0700 | [diff] [blame] | 145 | |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 146 | super(PlCoreBase, self).save(*args, **kwargs) |
Sapan Bhatia | c860243 | 2014-04-29 20:33:51 -0400 | [diff] [blame] | 147 | |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 148 | # This is a no-op if observer_disabled is set |
Scott Baker | 6594bea | 2014-09-23 16:04:36 -0700 | [diff] [blame] | 149 | if not silent: |
| 150 | notify_observer() |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 151 | |
Scott Baker | 5e5f455 | 2014-10-03 14:48:06 -0700 | [diff] [blame] | 152 | self._initial = self._dict |
Scott Baker | 13acdd6 | 2013-05-08 17:42:56 -0700 | [diff] [blame] | 153 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 154 | def save_by_user(self, user, *args, **kwds): |
Scott Baker | e5f41b0 | 2014-10-02 22:50:18 -0700 | [diff] [blame] | 155 | if not self.can_update(user): |
| 156 | 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] | 157 | |
| 158 | for fieldName in self.changed_fields: |
| 159 | if not self.can_update_field(user, fieldName): |
| 160 | raise PermissionDenied("You do not have permission to update field %s in object %s" % (fieldName, self.__class__.__name__)) |
| 161 | |
Scott Baker | e5f41b0 | 2014-10-02 22:50:18 -0700 | [diff] [blame] | 162 | self.save(*args, **kwds) |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 163 | |
Tony Mack | 332ee1d | 2014-02-04 15:33:45 -0500 | [diff] [blame] | 164 | def delete_by_user(self, user, *args, **kwds): |
Scott Baker | e5f41b0 | 2014-10-02 22:50:18 -0700 | [diff] [blame] | 165 | if not self.can_update(user): |
| 166 | raise PermissionDenied("You do not have permission to delete %s objects" % self.__class__.__name__) |
| 167 | self.delete(*args, **kwds) |
Tony Mack | 332ee1d | 2014-02-04 15:33:45 -0500 | [diff] [blame] | 168 | |
Scott Baker | 5e5f455 | 2014-10-03 14:48:06 -0700 | [diff] [blame] | 169 | @classmethod |
| 170 | def select_by_user(cls, user): |
| 171 | # This should be overridden by descendant classes that want to perform |
| 172 | # filtering of visible objects by user. |
| 173 | return cls.objects.all() |
| 174 | |
Scott Baker | 13acdd6 | 2013-05-08 17:42:56 -0700 | [diff] [blame] | 175 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 176 | |
| 177 | |