Scott Baker | fd44dfc | 2014-05-23 13:20:53 -0700 | [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 |
Sapan Bhatia | 3909719 | 2014-09-04 00:39:19 -0400 | [diff] [blame] | 8 | import model_policy |
Scott Baker | 9e99074 | 2014-03-19 22:14:58 -0700 | [diff] [blame] | 9 | |
| 10 | try: |
| 11 | # This is a no-op if observer_disabled is set to 1 in the config file |
| 12 | from observer import * |
| 13 | except: |
Scott Baker | 65d5a9a | 2014-05-26 15:58:09 -0700 | [diff] [blame] | 14 | print >> sys.stderr, "import of observer failed! printing traceback and disabling observer:" |
Scott Baker | 9e99074 | 2014-03-19 22:14:58 -0700 | [diff] [blame] | 15 | import traceback |
| 16 | traceback.print_exc() |
| 17 | |
| 18 | # guard against something failing |
Scott Baker | fd44dfc | 2014-05-23 13:20:53 -0700 | [diff] [blame] | 19 | def notify_observer(*args, **kwargs): |
Scott Baker | 9e99074 | 2014-03-19 22:14:58 -0700 | [diff] [blame] | 20 | pass |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 21 | |
Sapan Bhatia | 3089d83 | 2014-04-29 14:36:51 -0400 | [diff] [blame] | 22 | # This manager will be inherited by all subclasses because |
| 23 | # the core model is abstract. |
Sapan Bhatia | 15bf5ac | 2014-07-21 20:06:59 -0400 | [diff] [blame] | 24 | class PlCoreBaseDeletionManager(models.Manager): |
Scott Baker | 92f1422 | 2014-09-12 12:57:27 -0700 | [diff] [blame] | 25 | def get_queryset(self): |
Sapan Bhatia | bbcc190 | 2014-07-22 01:11:36 -0400 | [diff] [blame] | 26 | return super(PlCoreBaseDeletionManager, self).get_query_set().filter(deleted=True) |
Sapan Bhatia | 15bf5ac | 2014-07-21 20:06:59 -0400 | [diff] [blame] | 27 | |
Scott Baker | 92f1422 | 2014-09-12 12:57:27 -0700 | [diff] [blame] | 28 | # deprecated in django 1.7 in favor of get_queryset() |
| 29 | def get_query_set(self): |
| 30 | return self.get_queryset() |
| 31 | |
Sapan Bhatia | 15bf5ac | 2014-07-21 20:06:59 -0400 | [diff] [blame] | 32 | # This manager will be inherited by all subclasses because |
| 33 | # the core model is abstract. |
Sapan Bhatia | 4eb663a | 2014-04-29 14:26:10 -0400 | [diff] [blame] | 34 | class PlCoreBaseManager(models.Manager): |
Scott Baker | 92f1422 | 2014-09-12 12:57:27 -0700 | [diff] [blame] | 35 | def get_queryset(self): |
Sapan Bhatia | 4eb663a | 2014-04-29 14:26:10 -0400 | [diff] [blame] | 36 | return super(PlCoreBaseManager, self).get_query_set().filter(deleted=False) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 37 | |
Scott Baker | 92f1422 | 2014-09-12 12:57:27 -0700 | [diff] [blame] | 38 | # deprecated in django 1.7 in favor of get_queryset() |
| 39 | def get_query_set(self): |
| 40 | return self.get_queryset() |
| 41 | |
Sapan Bhatia | 4eb663a | 2014-04-29 14:26:10 -0400 | [diff] [blame] | 42 | class PlCoreBase(models.Model): |
| 43 | objects = PlCoreBaseManager() |
Sapan Bhatia | 15bf5ac | 2014-07-21 20:06:59 -0400 | [diff] [blame] | 44 | deleted_objects = PlCoreBaseDeletionManager() |
| 45 | |
| 46 | # default values for created and updated are only there to keep evolution |
| 47 | # from failing. |
Scott Baker | 09dd0c0 | 2014-09-12 12:05:51 -0700 | [diff] [blame] | 48 | created = models.DateTimeField(auto_now_add=True, default=datetime.datetime.now) |
| 49 | updated = models.DateTimeField(auto_now=True, default=datetime.datetime.now) |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 50 | enacted = models.DateTimeField(null=True, default=None) |
Sapan Bhatia | 34aee75 | 2014-04-28 21:06:39 -0400 | [diff] [blame] | 51 | backend_status = models.CharField(max_length=140, |
Sapan Bhatia | d507f43 | 2014-04-29 00:41:39 -0400 | [diff] [blame] | 52 | default="Provisioning in progress") |
Sapan Bhatia | bcc1899 | 2014-04-29 10:32:14 -0400 | [diff] [blame] | 53 | deleted = models.BooleanField(default=False) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 54 | |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 55 | class Meta: |
Sapan Bhatia | 3089d83 | 2014-04-29 14:36:51 -0400 | [diff] [blame] | 56 | # Changing abstract to False would require the managers of subclasses of |
| 57 | # PlCoreBase to be customized individually. |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 58 | abstract = True |
| 59 | app_label = "core" |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 60 | |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 61 | def __init__(self, *args, **kwargs): |
| 62 | super(PlCoreBase, self).__init__(*args, **kwargs) |
| 63 | self.__initial = self._dict |
Scott Baker | 13acdd6 | 2013-05-08 17:42:56 -0700 | [diff] [blame] | 64 | |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 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) |
Siobhan Tully | 7329134 | 2013-05-10 10:50:08 -0400 | [diff] [blame] | 71 | |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 72 | @property |
| 73 | def has_changed(self): |
| 74 | return bool(self.diff) |
Siobhan Tully | 7329134 | 2013-05-10 10:50:08 -0400 | [diff] [blame] | 75 | |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 76 | @property |
| 77 | def changed_fields(self): |
| 78 | return self.diff.keys() |
Siobhan Tully | 7329134 | 2013-05-10 10:50:08 -0400 | [diff] [blame] | 79 | |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 80 | def get_field_diff(self, field_name): |
| 81 | return self.diff.get(field_name, None) |
Siobhan Tully | 7329134 | 2013-05-10 10:50:08 -0400 | [diff] [blame] | 82 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 83 | def can_update(self, user): |
| 84 | if user.is_readonly: |
| 85 | return False |
| 86 | if user.is_admin: |
| 87 | return True |
| 88 | return False |
| 89 | |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 90 | def delete(self, *args, **kwds): |
Scott Baker | 6ecd426 | 2014-01-21 23:15:21 -0800 | [diff] [blame] | 91 | # so we have something to give the observer |
Sapan Bhatia | 77d1d89 | 2014-07-21 20:07:23 -0400 | [diff] [blame] | 92 | purge = kwds.get('purge',False) |
| 93 | try: |
| 94 | purge = purge or observer_disabled |
| 95 | except NameError: |
| 96 | pass |
| 97 | |
| 98 | if (purge): |
Sapan Bhatia | ad2c4a1 | 2014-07-22 01:21:05 -0400 | [diff] [blame] | 99 | del kwds['purge'] |
Sapan Bhatia | 77d1d89 | 2014-07-21 20:07:23 -0400 | [diff] [blame] | 100 | super(PlCoreBase, self).delete(*args, **kwds) |
Sapan Bhatia | c860243 | 2014-04-29 20:33:51 -0400 | [diff] [blame] | 101 | else: |
| 102 | self.deleted = True |
| 103 | self.enacted=None |
| 104 | self.save(update_fields=['enacted','deleted']) |
Scott Baker | 6ecd426 | 2014-01-21 23:15:21 -0800 | [diff] [blame] | 105 | |
Sapan Bhatia | dbaf193 | 2013-09-03 11:28:52 -0400 | [diff] [blame] | 106 | |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 107 | def save(self, *args, **kwargs): |
| 108 | super(PlCoreBase, self).save(*args, **kwargs) |
Sapan Bhatia | c860243 | 2014-04-29 20:33:51 -0400 | [diff] [blame] | 109 | |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 110 | # This is a no-op if observer_disabled is set |
| 111 | notify_observer() |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 112 | |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 113 | self.__initial = self._dict |
Scott Baker | 13acdd6 | 2013-05-08 17:42:56 -0700 | [diff] [blame] | 114 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 115 | def save_by_user(self, user, *args, **kwds): |
| 116 | if self.can_update(user): |
| 117 | self.save(*args, **kwds) |
| 118 | |
Tony Mack | 332ee1d | 2014-02-04 15:33:45 -0500 | [diff] [blame] | 119 | def delete_by_user(self, user, *args, **kwds): |
| 120 | if self.can_update(user): |
| 121 | self.delete(*args, **kwds) |
| 122 | |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 123 | @property |
| 124 | def _dict(self): |
| 125 | return model_to_dict(self, fields=[field.name for field in |
| 126 | self._meta.fields]) |
Scott Baker | 13acdd6 | 2013-05-08 17:42:56 -0700 | [diff] [blame] | 127 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 128 | |
| 129 | |