Scott Baker | daca816 | 2015-02-02 14:28:35 -0800 | [diff] [blame] | 1 | import datetime |
Sapan Bhatia | ca88d9a | 2015-12-22 18:38:56 +0100 | [diff] [blame] | 2 | import json |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 3 | import os |
Scott Baker | 65d5a9a | 2014-05-26 15:58:09 -0700 | [diff] [blame] | 4 | import sys |
Sapan Bhatia | f5c361b | 2016-04-27 19:19:07 +0200 | [diff] [blame] | 5 | import threading |
Tony Mack | b0c951c | 2015-03-09 16:47:46 -0400 | [diff] [blame] | 6 | from django import db |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 7 | from django.db import models |
Scott Baker | b2254a5 | 2016-12-11 17:51:33 -0800 | [diff] [blame] | 8 | from django.db import transaction |
Scott Baker | 13acdd6 | 2013-05-08 17:42:56 -0700 | [diff] [blame] | 9 | from django.forms.models import model_to_dict |
Scott Baker | c1c45f8 | 2014-01-21 16:23:51 -0800 | [diff] [blame] | 10 | from django.core.urlresolvers import reverse |
Scott Baker | 6ecd426 | 2014-01-21 23:15:21 -0800 | [diff] [blame] | 11 | from django.forms.models import model_to_dict |
Scott Baker | b24f2c3 | 2014-09-17 22:18:46 -0700 | [diff] [blame] | 12 | from django.utils import timezone |
Scott Baker | 0bec56f | 2014-10-02 22:50:18 -0700 | [diff] [blame] | 13 | from django.core.exceptions import PermissionDenied |
Sapan Bhatia | 40bbfd9 | 2014-11-12 10:38:23 -0500 | [diff] [blame] | 14 | from model_autodeletion import ephemeral_models |
Scott Baker | b6b474d | 2015-02-10 18:24:20 -0800 | [diff] [blame] | 15 | from cgi import escape as html_escape |
Scott Baker | b2254a5 | 2016-12-11 17:51:33 -0800 | [diff] [blame] | 16 | from journal import journal_object |
Scott Baker | 9e99074 | 2014-03-19 22:14:58 -0700 | [diff] [blame] | 17 | |
Sapan Bhatia | e40f3d5 | 2016-09-16 17:05:58 +0200 | [diff] [blame] | 18 | import redis |
| 19 | from redis import ConnectionError |
| 20 | |
Scott Baker | 9e99074 | 2014-03-19 22:14:58 -0700 | [diff] [blame] | 21 | try: |
| 22 | # This is a no-op if observer_disabled is set to 1 in the config file |
Sapan Bhatia | 003e84c | 2016-01-15 11:05:52 -0500 | [diff] [blame] | 23 | from synchronizers.base import * |
Scott Baker | 9e99074 | 2014-03-19 22:14:58 -0700 | [diff] [blame] | 24 | except: |
Scott Baker | 65d5a9a | 2014-05-26 15:58:09 -0700 | [diff] [blame] | 25 | print >> sys.stderr, "import of observer failed! printing traceback and disabling observer:" |
Scott Baker | 9e99074 | 2014-03-19 22:14:58 -0700 | [diff] [blame] | 26 | import traceback |
| 27 | traceback.print_exc() |
| 28 | |
| 29 | # guard against something failing |
Scott Baker | fd44dfc | 2014-05-23 13:20:53 -0700 | [diff] [blame] | 30 | def notify_observer(*args, **kwargs): |
Scott Baker | 9e99074 | 2014-03-19 22:14:58 -0700 | [diff] [blame] | 31 | pass |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 32 | |
Tony Mack | 653c983 | 2015-03-04 12:41:36 -0500 | [diff] [blame] | 33 | class StrippedCharField(models.CharField): |
| 34 | """ CharField that strips trailing and leading spaces.""" |
| 35 | def clean(self, value, *args, **kwds): |
| 36 | if value is not None: |
| 37 | value = value.strip() |
| 38 | return super(StrippedCharField, self).clean(value, *args, **kwds) |
Sapan Bhatia | ce36dac | 2015-04-07 17:38:24 -0400 | [diff] [blame] | 39 | |
Tony Mack | 653c983 | 2015-03-04 12:41:36 -0500 | [diff] [blame] | 40 | |
Sapan Bhatia | 3089d83 | 2014-04-29 14:36:51 -0400 | [diff] [blame] | 41 | # This manager will be inherited by all subclasses because |
| 42 | # the core model is abstract. |
Sapan Bhatia | 15bf5ac | 2014-07-21 20:06:59 -0400 | [diff] [blame] | 43 | class PlCoreBaseDeletionManager(models.Manager): |
Scott Baker | b08d656 | 2014-09-12 12:57:27 -0700 | [diff] [blame] | 44 | def get_queryset(self): |
Scott Baker | b24f2c3 | 2014-09-17 22:18:46 -0700 | [diff] [blame] | 45 | parent=super(PlCoreBaseDeletionManager, self) |
| 46 | if hasattr(parent, "get_queryset"): |
| 47 | return parent.get_queryset().filter(deleted=True) |
| 48 | else: |
| 49 | return parent.get_query_set().filter(deleted=True) |
Sapan Bhatia | 15bf5ac | 2014-07-21 20:06:59 -0400 | [diff] [blame] | 50 | |
Scott Baker | b24f2c3 | 2014-09-17 22:18:46 -0700 | [diff] [blame] | 51 | # deprecated in django 1.7 in favor of get_queryset(). |
Scott Baker | b08d656 | 2014-09-12 12:57:27 -0700 | [diff] [blame] | 52 | def get_query_set(self): |
| 53 | return self.get_queryset() |
| 54 | |
Sapan Bhatia | 15bf5ac | 2014-07-21 20:06:59 -0400 | [diff] [blame] | 55 | # This manager will be inherited by all subclasses because |
| 56 | # the core model is abstract. |
Sapan Bhatia | 4eb663a | 2014-04-29 14:26:10 -0400 | [diff] [blame] | 57 | class PlCoreBaseManager(models.Manager): |
Scott Baker | b08d656 | 2014-09-12 12:57:27 -0700 | [diff] [blame] | 58 | def get_queryset(self): |
Scott Baker | b24f2c3 | 2014-09-17 22:18:46 -0700 | [diff] [blame] | 59 | parent=super(PlCoreBaseManager, self) |
| 60 | if hasattr(parent, "get_queryset"): |
| 61 | return parent.get_queryset().filter(deleted=False) |
| 62 | else: |
| 63 | return parent.get_query_set().filter(deleted=False) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 64 | |
Scott Baker | b24f2c3 | 2014-09-17 22:18:46 -0700 | [diff] [blame] | 65 | # deprecated in django 1.7 in favor of get_queryset(). |
Scott Baker | b08d656 | 2014-09-12 12:57:27 -0700 | [diff] [blame] | 66 | def get_query_set(self): |
| 67 | return self.get_queryset() |
| 68 | |
Scott Baker | 1211334 | 2015-02-10 15:44:30 -0800 | [diff] [blame] | 69 | class PlModelMixIn(object): |
Scott Baker | cbfb600 | 2014-10-03 00:32:37 -0700 | [diff] [blame] | 70 | # Provides useful methods for computing which objects in a model have |
| 71 | # changed. Make sure to do self._initial = self._dict in the __init__ |
| 72 | # method. |
| 73 | |
Scott Baker | 1211334 | 2015-02-10 15:44:30 -0800 | [diff] [blame] | 74 | # Also includes useful utility, like getValidators |
| 75 | |
Scott Baker | cbfb600 | 2014-10-03 00:32:37 -0700 | [diff] [blame] | 76 | # This is broken out of PlCoreBase into a Mixin so the User model can |
| 77 | # also make use of it. |
| 78 | |
| 79 | @property |
| 80 | def _dict(self): |
| 81 | return model_to_dict(self, fields=[field.name for field in |
| 82 | self._meta.fields]) |
| 83 | |
Scott Baker | daca816 | 2015-02-02 14:28:35 -0800 | [diff] [blame] | 84 | def fields_differ(self,f1,f2): |
S.Çağlar Onur | 0e59183 | 2015-02-24 17:28:09 -0500 | [diff] [blame] | 85 | if isinstance(f1,datetime.datetime) and isinstance(f2,datetime.datetime) and (timezone.is_aware(f1) != timezone.is_aware(f2)): |
| 86 | return True |
| 87 | else: |
| 88 | return (f1 != f2) |
Scott Baker | daca816 | 2015-02-02 14:28:35 -0800 | [diff] [blame] | 89 | |
Scott Baker | cbfb600 | 2014-10-03 00:32:37 -0700 | [diff] [blame] | 90 | @property |
| 91 | def diff(self): |
| 92 | d1 = self._initial |
| 93 | d2 = self._dict |
Scott Baker | daca816 | 2015-02-02 14:28:35 -0800 | [diff] [blame] | 94 | diffs = [(k, (v, d2[k])) for k, v in d1.items() if self.fields_differ(v,d2[k])] |
Scott Baker | cbfb600 | 2014-10-03 00:32:37 -0700 | [diff] [blame] | 95 | return dict(diffs) |
| 96 | |
| 97 | @property |
| 98 | def has_changed(self): |
| 99 | return bool(self.diff) |
| 100 | |
| 101 | @property |
| 102 | def changed_fields(self): |
| 103 | return self.diff.keys() |
| 104 | |
Scott Baker | cbfb600 | 2014-10-03 00:32:37 -0700 | [diff] [blame] | 105 | def has_field_changed(self, field_name): |
| 106 | return field_name in self.diff.keys() |
| 107 | |
| 108 | def get_field_diff(self, field_name): |
| 109 | return self.diff.get(field_name, None) |
| 110 | |
Scott Baker | 56f799b | 2014-11-25 11:35:19 -0800 | [diff] [blame] | 111 | #classmethod |
| 112 | def getValidators(cls): |
| 113 | """ primarily for REST API, return a dictionary of field names mapped |
| 114 | to lists of the type of validations that need to be applied to |
| 115 | those fields. |
| 116 | """ |
| 117 | validators = {} |
| 118 | for field in cls._meta.fields: |
| 119 | l = [] |
| 120 | if field.blank==False: |
| 121 | l.append("notBlank") |
Scott Baker | 0d10867 | 2014-11-26 00:53:19 -0800 | [diff] [blame] | 122 | if field.__class__.__name__=="URLField": |
| 123 | l.append("url") |
Scott Baker | 56f799b | 2014-11-25 11:35:19 -0800 | [diff] [blame] | 124 | validators[field.name] = l |
| 125 | return validators |
| 126 | |
Scott Baker | e497b3c | 2016-01-29 12:18:19 -0800 | [diff] [blame] | 127 | def get_backend_register(self, k, default=None): |
| 128 | try: |
| 129 | return json.loads(self.backend_register).get(k, default) |
| 130 | except AttributeError: |
| 131 | return default |
| 132 | |
| 133 | def set_backend_register(self, k, v): |
| 134 | br = {} |
| 135 | try: |
| 136 | br=json.loads(self.backend_register) |
| 137 | except AttributeError: |
| 138 | br={} |
| 139 | |
| 140 | br[k] = v |
| 141 | self.backend_register = json.dumps(br) |
| 142 | |
Sapan Bhatia | ca88d9a | 2015-12-22 18:38:56 +0100 | [diff] [blame] | 143 | def get_backend_details(self): |
| 144 | try: |
| 145 | scratchpad = json.loads(self.backend_register) |
| 146 | except AttributeError: |
| 147 | return (None, None, None, None) |
| 148 | |
| 149 | try: |
| 150 | exponent = scratchpad['exponent'] |
| 151 | except KeyError: |
| 152 | exponent = None |
| 153 | |
| 154 | try: |
| 155 | last_success_time = scratchpad['last_success'] |
| 156 | dt = datetime.datetime.fromtimestamp(last_success_time) |
| 157 | last_success = dt.strftime("%Y-%m-%d %H:%M") |
| 158 | except KeyError: |
| 159 | last_success = None |
| 160 | |
| 161 | try: |
| 162 | failures = scratchpad['failures'] |
| 163 | except KeyError: |
| 164 | failures=None |
| 165 | |
| 166 | try: |
| 167 | last_failure_time = scratchpad['last_failure'] |
| 168 | dt = datetime.datetime.fromtimestamp(last_failure_time) |
| 169 | last_failure = dt.strftime("%Y-%m-%d %H:%M") |
| 170 | except KeyError: |
| 171 | last_failure = None |
| 172 | |
| 173 | return (exponent, last_success, last_failure, failures) |
| 174 | |
Scott Baker | b6b474d | 2015-02-10 18:24:20 -0800 | [diff] [blame] | 175 | def get_backend_icon(self): |
Sapan Bhatia | c153c2b | 2015-05-27 19:56:15 +0200 | [diff] [blame] | 176 | is_perfect = (self.backend_status is not None) and self.backend_status.startswith("1 -") |
Scott Baker | 725acd3 | 2015-03-04 10:00:24 -0800 | [diff] [blame] | 177 | is_good = (self.backend_status is not None) and (self.backend_status.startswith("0 -") or self.backend_status.startswith("1 -")) |
| 178 | is_provisioning = self.backend_status is None or self.backend_status == "Provisioning in progress" or self.backend_status=="" |
| 179 | |
Scott Baker | b6b474d | 2015-02-10 18:24:20 -0800 | [diff] [blame] | 180 | # returns (icon_name, tooltip) |
Sapan Bhatia | c153c2b | 2015-05-27 19:56:15 +0200 | [diff] [blame] | 181 | if (self.enacted is not None) and (self.enacted >= self.updated and is_good) or is_perfect: |
Scott Baker | b6b474d | 2015-02-10 18:24:20 -0800 | [diff] [blame] | 182 | return ("success", "successfully enacted") |
| 183 | else: |
Scott Baker | 725acd3 | 2015-03-04 10:00:24 -0800 | [diff] [blame] | 184 | if is_good or is_provisioning: |
| 185 | return ("clock", "Pending sync, last_status = " + html_escape(self.backend_status, quote=True)) |
Scott Baker | b6b474d | 2015-02-10 18:24:20 -0800 | [diff] [blame] | 186 | else: |
| 187 | return ("error", html_escape(self.backend_status, quote=True)) |
| 188 | |
Scott Baker | 55aa265 | 2015-12-09 16:31:26 -0800 | [diff] [blame] | 189 | def enforce_choices(self, field, choices): |
| 190 | choices = [x[0] for x in choices] |
| 191 | for choice in choices: |
| 192 | if field==choice: |
| 193 | return |
Scott Baker | 66aa544 | 2015-12-09 16:50:40 -0800 | [diff] [blame] | 194 | if (choice==None) and (field==""): |
| 195 | # allow "" and None to be equivalent |
| 196 | return |
Scott Baker | 55aa265 | 2015-12-09 16:31:26 -0800 | [diff] [blame] | 197 | raise Exception("Field value %s is not in %s" % (field, str(choices))) |
| 198 | |
Scott Baker | b2254a5 | 2016-12-11 17:51:33 -0800 | [diff] [blame] | 199 | # For cascading deletes, we need a Collector that doesn't do fastdelete, |
| 200 | # so we get a full list of models. |
| 201 | from django.db.models.deletion import Collector |
| 202 | from django.db import router |
| 203 | class XOSCollector(Collector): |
| 204 | def can_fast_delete(self, *args, **kwargs): |
| 205 | return False |
| 206 | |
Scott Baker | 1211334 | 2015-02-10 15:44:30 -0800 | [diff] [blame] | 207 | class PlCoreBase(models.Model, PlModelMixIn): |
Sapan Bhatia | 4eb663a | 2014-04-29 14:26:10 -0400 | [diff] [blame] | 208 | objects = PlCoreBaseManager() |
Sapan Bhatia | 15bf5ac | 2014-07-21 20:06:59 -0400 | [diff] [blame] | 209 | deleted_objects = PlCoreBaseDeletionManager() |
| 210 | |
| 211 | # default values for created and updated are only there to keep evolution |
| 212 | # from failing. |
Zack Williams | f8bb7dc | 2016-04-21 15:04:20 -0700 | [diff] [blame] | 213 | created = models.DateTimeField(auto_now_add=True) |
Sapan Bhatia | 648f85e | 2016-05-06 15:16:22 -0400 | [diff] [blame] | 214 | updated = models.DateTimeField(default=timezone.now) |
Scott Baker | 2a72eaf | 2014-12-01 21:42:59 -0800 | [diff] [blame] | 215 | enacted = models.DateTimeField(null=True, blank=True, default=None) |
Sapan Bhatia | 67400ad | 2015-01-23 15:59:55 +0000 | [diff] [blame] | 216 | policed = models.DateTimeField(null=True, blank=True, default=None) |
Sapan Bhatia | c1945a6 | 2015-01-29 20:36:45 +0000 | [diff] [blame] | 217 | |
| 218 | # This is a scratchpad used by the Observer |
Scott Baker | c53d081 | 2016-05-27 14:55:44 -0700 | [diff] [blame] | 219 | backend_register = models.CharField(max_length=1024, |
Sapan Bhatia | c1945a6 | 2015-01-29 20:36:45 +0000 | [diff] [blame] | 220 | default="{}", null=True) |
| 221 | |
Scott Baker | b2254a5 | 2016-12-11 17:51:33 -0800 | [diff] [blame] | 222 | # If True, then the backend wants to delete this object |
| 223 | backend_need_delete = models.BooleanField(default=False) |
| 224 | |
Scott Baker | 85b98e7 | 2015-02-06 00:11:10 -0800 | [diff] [blame] | 225 | backend_status = models.CharField(max_length=1024, |
Sapan Bhatia | cc9d960 | 2015-01-23 16:07:24 +0000 | [diff] [blame] | 226 | default="0 - Provisioning in progress") |
Sapan Bhatia | bcc1899 | 2014-04-29 10:32:14 -0400 | [diff] [blame] | 227 | deleted = models.BooleanField(default=False) |
Scott Baker | a635cba | 2015-03-10 12:04:10 -0700 | [diff] [blame] | 228 | write_protect = models.BooleanField(default=False) |
Sapan Bhatia | 859c551 | 2015-04-21 17:37:51 -0400 | [diff] [blame] | 229 | lazy_blocked = models.BooleanField(default=False) |
Scott Baker | ced9e4f | 2016-04-14 23:41:07 -0700 | [diff] [blame] | 230 | no_sync = models.BooleanField(default=False) # prevent object sync |
| 231 | no_policy = models.BooleanField(default=False) # prevent model_policy run |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 232 | |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 233 | class Meta: |
Sapan Bhatia | 3089d83 | 2014-04-29 14:36:51 -0400 | [diff] [blame] | 234 | # Changing abstract to False would require the managers of subclasses of |
| 235 | # PlCoreBase to be customized individually. |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 236 | abstract = True |
| 237 | app_label = "core" |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 238 | |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 239 | def __init__(self, *args, **kwargs): |
| 240 | super(PlCoreBase, self).__init__(*args, **kwargs) |
Scott Baker | 1211334 | 2015-02-10 15:44:30 -0800 | [diff] [blame] | 241 | self._initial = self._dict # for PlModelMixIn |
Scott Baker | 5dc87a6 | 2014-09-23 22:41:17 -0700 | [diff] [blame] | 242 | self.silent = False |
Scott Baker | 13acdd6 | 2013-05-08 17:42:56 -0700 | [diff] [blame] | 243 | |
Sapan Bhatia | d0ac99c | 2016-02-10 17:51:26 +0100 | [diff] [blame] | 244 | def get_controller(self): |
| 245 | return self.controller |
| 246 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 247 | def can_update(self, user): |
Tony Mack | 5ff90fc | 2015-02-08 21:38:41 -0500 | [diff] [blame] | 248 | return user.can_update_root() |
Scott Baker | cbfb600 | 2014-10-03 00:32:37 -0700 | [diff] [blame] | 249 | |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 250 | def delete(self, *args, **kwds): |
Scott Baker | 6ecd426 | 2014-01-21 23:15:21 -0800 | [diff] [blame] | 251 | # so we have something to give the observer |
Sapan Bhatia | 77d1d89 | 2014-07-21 20:07:23 -0400 | [diff] [blame] | 252 | purge = kwds.get('purge',False) |
Scott Baker | c5b5060 | 2014-10-09 16:22:00 -0700 | [diff] [blame] | 253 | if purge: |
| 254 | del kwds['purge'] |
Scott Baker | 0491f6f | 2014-09-23 16:04:36 -0700 | [diff] [blame] | 255 | silent = kwds.get('silent',False) |
Scott Baker | c5b5060 | 2014-10-09 16:22:00 -0700 | [diff] [blame] | 256 | if silent: |
| 257 | del kwds['silent'] |
Sapan Bhatia | 77d1d89 | 2014-07-21 20:07:23 -0400 | [diff] [blame] | 258 | try: |
| 259 | purge = purge or observer_disabled |
| 260 | except NameError: |
| 261 | pass |
Scott Baker | 0491f6f | 2014-09-23 16:04:36 -0700 | [diff] [blame] | 262 | |
Sapan Bhatia | 77d1d89 | 2014-07-21 20:07:23 -0400 | [diff] [blame] | 263 | if (purge): |
Scott Baker | b2254a5 | 2016-12-11 17:51:33 -0800 | [diff] [blame] | 264 | journal_object(self, "delete.purge") |
Sapan Bhatia | 77d1d89 | 2014-07-21 20:07:23 -0400 | [diff] [blame] | 265 | super(PlCoreBase, self).delete(*args, **kwds) |
Sapan Bhatia | c860243 | 2014-04-29 20:33:51 -0400 | [diff] [blame] | 266 | else: |
Scott Baker | b2254a5 | 2016-12-11 17:51:33 -0800 | [diff] [blame] | 267 | if (not self.write_protect ): |
Sapan Bhatia | ce36dac | 2015-04-07 17:38:24 -0400 | [diff] [blame] | 268 | self.deleted = True |
| 269 | self.enacted=None |
Sapan Bhatia | 62d5b63 | 2015-05-09 18:07:06 +0200 | [diff] [blame] | 270 | self.policed=None |
Scott Baker | b2254a5 | 2016-12-11 17:51:33 -0800 | [diff] [blame] | 271 | journal_object(self, "delete.mark_deleted") |
Sapan Bhatia | 62d5b63 | 2015-05-09 18:07:06 +0200 | [diff] [blame] | 272 | self.save(update_fields=['enacted','deleted','policed'], silent=silent) |
Sapan Bhatia | dbaf193 | 2013-09-03 11:28:52 -0400 | [diff] [blame] | 273 | |
Scott Baker | b2254a5 | 2016-12-11 17:51:33 -0800 | [diff] [blame] | 274 | collector = XOSCollector(using=router.db_for_write(self.__class__, instance=self)) |
| 275 | collector.collect([self])
|
| 276 | with transaction.atomic():
|
| 277 | for (k, models) in collector.data.items():
|
| 278 | for model in models:
|
| 279 | if model.deleted:
|
| 280 | # in case it's already been deleted, don't delete again
|
| 281 | continue
|
| 282 | model.deleted = True
|
| 283 | model.enacted=None |
| 284 | model.policed=None |
| 285 | journal_object(model, "delete.cascade.mark_deleted", msg="root = %r" % self) |
| 286 | model.save(update_fields=['enacted','deleted','policed'], silent=silent)
|
| 287 |
|
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 288 | def save(self, *args, **kwargs): |
Scott Baker | b2254a5 | 2016-12-11 17:51:33 -0800 | [diff] [blame] | 289 | journal_object(self, "plcorebase.save") |
| 290 | |
Scott Baker | 5dc87a6 | 2014-09-23 22:41:17 -0700 | [diff] [blame] | 291 | # let the user specify silence as either a kwarg or an instance varible |
| 292 | silent = self.silent |
Scott Baker | 0491f6f | 2014-09-23 16:04:36 -0700 | [diff] [blame] | 293 | if "silent" in kwargs: |
Scott Baker | 5dc87a6 | 2014-09-23 22:41:17 -0700 | [diff] [blame] | 294 | silent=silent or kwargs.pop("silent") |
Scott Baker | 0491f6f | 2014-09-23 16:04:36 -0700 | [diff] [blame] | 295 | |
Scott Baker | b8de676 | 2016-12-05 15:56:32 -0800 | [diff] [blame] | 296 | always_update_timestamp = False |
| 297 | if "always_update_timestamp" in kwargs: |
| 298 | always_update_timestamp = always_update_timestamp or kwargs.pop("always_update_timestamp") |
| 299 | |
Scott Baker | a94b23f | 2015-04-08 13:59:56 -0700 | [diff] [blame] | 300 | # SMBAKER: if an object is trying to delete itself, or if the observer |
| 301 | # is updating an object's backend_* fields, then let it slip past the |
| 302 | # composite key check. |
| 303 | ignore_composite_key_check=False |
| 304 | if "update_fields" in kwargs: |
| 305 | ignore_composite_key_check=True |
| 306 | for field in kwargs["update_fields"]: |
| 307 | if not (field in ["backend_register", "backend_status", "deleted", "enacted", "updated"]): |
| 308 | ignore_composite_key_check=False |
| 309 | |
Scott Baker | b8de676 | 2016-12-05 15:56:32 -0800 | [diff] [blame] | 310 | if ('synchronizer' not in threading.current_thread().name) or always_update_timestamp: |
Sapan Bhatia | 648f85e | 2016-05-06 15:16:22 -0400 | [diff] [blame] | 311 | self.updated = timezone.now() |
Sapan Bhatia | f5c361b | 2016-04-27 19:19:07 +0200 | [diff] [blame] | 312 | |
Sapan Bhatia | e40f3d5 | 2016-09-16 17:05:58 +0200 | [diff] [blame] | 313 | # Transmit update via Redis |
| 314 | changed_fields = [] |
| 315 | |
| 316 | if self.pk is not None: |
| 317 | my_model = type(self) |
| 318 | try: |
| 319 | orig = my_model.objects.get(pk=self.pk) |
| 320 | |
| 321 | for f in my_model._meta.fields: |
| 322 | oval = getattr(orig, f.name) |
| 323 | nval = getattr(self, f.name) |
| 324 | if oval != nval: |
| 325 | changed_fields.append(f.name) |
| 326 | except: |
| 327 | changed_fields.append('__lookup_error') |
| 328 | |
Scott Baker | b2254a5 | 2016-12-11 17:51:33 -0800 | [diff] [blame] | 329 | journal_object(self, "plcorebase.save.super_save") |
Sapan Bhatia | 8113ebb | 2016-11-30 14:55:16 +0100 | [diff] [blame] | 330 | |
| 331 | super(PlCoreBase, self).save(*args, **kwargs) |
| 332 | |
Scott Baker | b2254a5 | 2016-12-11 17:51:33 -0800 | [diff] [blame] | 333 | journal_object(self, "plcorebase.save.super_save_returned") |
| 334 | |
Sapan Bhatia | e40f3d5 | 2016-09-16 17:05:58 +0200 | [diff] [blame] | 335 | try: |
| 336 | r = redis.Redis("redis") |
| 337 | payload = json.dumps({'pk':self.pk,'changed_fields':changed_fields}) |
| 338 | r.publish(self.__class__.__name__, payload) |
| 339 | except ConnectionError: |
| 340 | # Redis not running. |
| 341 | pass |
| 342 | |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 343 | # This is a no-op if observer_disabled is set |
Sapan Bhatia | 48e755f | 2015-02-10 11:46:51 -0500 | [diff] [blame] | 344 | # if not silent: |
| 345 | # notify_observer() |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 346 | |
Scott Baker | 165f70c | 2014-10-03 14:48:06 -0700 | [diff] [blame] | 347 | self._initial = self._dict |
Scott Baker | 13acdd6 | 2013-05-08 17:42:56 -0700 | [diff] [blame] | 348 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 349 | def save_by_user(self, user, *args, **kwds): |
Scott Baker | 0bec56f | 2014-10-02 22:50:18 -0700 | [diff] [blame] | 350 | if not self.can_update(user): |
Scott Baker | 0119c15 | 2014-10-06 22:58:48 -0700 | [diff] [blame] | 351 | if getattr(self, "_cant_update_fieldName", None) is not None: |
| 352 | raise PermissionDenied("You do not have permission to update field %s on object %s" % (self._cant_update_fieldName, self.__class__.__name__)) |
| 353 | else: |
| 354 | 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] | 355 | |
Scott Baker | 0bec56f | 2014-10-02 22:50:18 -0700 | [diff] [blame] | 356 | self.save(*args, **kwds) |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 357 | |
Tony Mack | 332ee1d | 2014-02-04 15:33:45 -0500 | [diff] [blame] | 358 | def delete_by_user(self, user, *args, **kwds): |
Scott Baker | 0bec56f | 2014-10-02 22:50:18 -0700 | [diff] [blame] | 359 | if not self.can_update(user): |
| 360 | raise PermissionDenied("You do not have permission to delete %s objects" % self.__class__.__name__) |
| 361 | self.delete(*args, **kwds) |
Tony Mack | 332ee1d | 2014-02-04 15:33:45 -0500 | [diff] [blame] | 362 | |
Scott Baker | 165f70c | 2014-10-03 14:48:06 -0700 | [diff] [blame] | 363 | @classmethod |
| 364 | def select_by_user(cls, user): |
| 365 | # This should be overridden by descendant classes that want to perform |
| 366 | # filtering of visible objects by user. |
| 367 | return cls.objects.all() |
| 368 | |
Sapan Bhatia | 998b32e | 2014-11-12 10:06:23 -0500 | [diff] [blame] | 369 | @classmethod |
| 370 | def is_ephemeral(cls): |
Sapan Bhatia | ce36dac | 2015-04-07 17:38:24 -0400 | [diff] [blame] | 371 | return cls in ephemeral_models |
Sapan Bhatia | ec2ff77 | 2016-04-06 19:03:35 +0200 | [diff] [blame] | 372 | |
| 373 | def tologdict(self): |
| 374 | try: |
| 375 | d = {'model_name':self.__class__.__name__, 'pk': self.pk} |
| 376 | except: |
| 377 | d = {} |
| 378 | |
| 379 | return d |
Sapan Bhatia | 14409aa | 2016-08-24 19:15:56 +0200 | [diff] [blame] | 380 | |
| 381 | class ModelLink: |
| 382 | def __init__(self,dest,via,into=None): |
| 383 | self.dest=dest |
| 384 | self.via=via |
| 385 | self.into=into |
| 386 | |