blob: 000e77e0b78b6a4d3bf25296425e48842149c46e [file] [log] [blame]
Siobhan Tully4bc09f22013-04-10 21:15:21 -04001import os
Scott Baker65d5a9a2014-05-26 15:58:09 -07002import sys
Siobhan Tully4bc09f22013-04-10 21:15:21 -04003from django.db import models
Scott Baker13acdd62013-05-08 17:42:56 -07004from django.forms.models import model_to_dict
Scott Bakerc1c45f82014-01-21 16:23:51 -08005from django.core.urlresolvers import reverse
Scott Baker6ecd4262014-01-21 23:15:21 -08006from django.forms.models import model_to_dict
Scott Bakerb24f2c32014-09-17 22:18:46 -07007from django.utils import timezone
Scott Baker0bec56f2014-10-02 22:50:18 -07008from django.core.exceptions import PermissionDenied
Sapan Bhatiabad67742014-09-04 00:39:19 -04009import model_policy
Scott Baker9e990742014-03-19 22:14:58 -070010
11try:
12 # This is a no-op if observer_disabled is set to 1 in the config file
13 from observer import *
14except:
Scott Baker65d5a9a2014-05-26 15:58:09 -070015 print >> sys.stderr, "import of observer failed! printing traceback and disabling observer:"
Scott Baker9e990742014-03-19 22:14:58 -070016 import traceback
17 traceback.print_exc()
18
19 # guard against something failing
Scott Bakerfd44dfc2014-05-23 13:20:53 -070020 def notify_observer(*args, **kwargs):
Scott Baker9e990742014-03-19 22:14:58 -070021 pass
Siobhan Tully4bc09f22013-04-10 21:15:21 -040022
Sapan Bhatia3089d832014-04-29 14:36:51 -040023# This manager will be inherited by all subclasses because
24# the core model is abstract.
Sapan Bhatia15bf5ac2014-07-21 20:06:59 -040025class PlCoreBaseDeletionManager(models.Manager):
Scott Bakerb08d6562014-09-12 12:57:27 -070026 def get_queryset(self):
Scott Bakerb24f2c32014-09-17 22:18:46 -070027 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 Bhatia15bf5ac2014-07-21 20:06:59 -040032
Scott Bakerb24f2c32014-09-17 22:18:46 -070033 # deprecated in django 1.7 in favor of get_queryset().
Scott Bakerb08d6562014-09-12 12:57:27 -070034 def get_query_set(self):
35 return self.get_queryset()
36
Sapan Bhatia15bf5ac2014-07-21 20:06:59 -040037# This manager will be inherited by all subclasses because
38# the core model is abstract.
Sapan Bhatia4eb663a2014-04-29 14:26:10 -040039class PlCoreBaseManager(models.Manager):
Scott Bakerb08d6562014-09-12 12:57:27 -070040 def get_queryset(self):
Scott Bakerb24f2c32014-09-17 22:18:46 -070041 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 Tully4bc09f22013-04-10 21:15:21 -040046
Scott Bakerb24f2c32014-09-17 22:18:46 -070047 # deprecated in django 1.7 in favor of get_queryset().
Scott Bakerb08d6562014-09-12 12:57:27 -070048 def get_query_set(self):
49 return self.get_queryset()
50
Scott Bakercbfb6002014-10-03 00:32:37 -070051class 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
Scott Bakercbfb6002014-10-03 00:32:37 -070079 def has_field_changed(self, field_name):
80 return field_name in self.diff.keys()
81
82 def get_field_diff(self, field_name):
83 return self.diff.get(field_name, None)
84
Scott Bakercbfb6002014-10-03 00:32:37 -070085class PlCoreBase(models.Model, DiffModelMixIn):
Sapan Bhatia4eb663a2014-04-29 14:26:10 -040086 objects = PlCoreBaseManager()
Sapan Bhatia15bf5ac2014-07-21 20:06:59 -040087 deleted_objects = PlCoreBaseDeletionManager()
88
89 # default values for created and updated are only there to keep evolution
90 # from failing.
Scott Bakerb24f2c32014-09-17 22:18:46 -070091 created = models.DateTimeField(auto_now_add=True, default=timezone.now)
92 updated = models.DateTimeField(auto_now=True, default=timezone.now)
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040093 enacted = models.DateTimeField(null=True, default=None)
Sapan Bhatia34aee752014-04-28 21:06:39 -040094 backend_status = models.CharField(max_length=140,
Sapan Bhatiad507f432014-04-29 00:41:39 -040095 default="Provisioning in progress")
Sapan Bhatiabcc18992014-04-29 10:32:14 -040096 deleted = models.BooleanField(default=False)
Siobhan Tully4bc09f22013-04-10 21:15:21 -040097
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040098 class Meta:
Sapan Bhatia3089d832014-04-29 14:36:51 -040099 # Changing abstract to False would require the managers of subclasses of
100 # PlCoreBase to be customized individually.
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -0400101 abstract = True
102 app_label = "core"
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400103
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -0400104 def __init__(self, *args, **kwargs):
105 super(PlCoreBase, self).__init__(*args, **kwargs)
Scott Bakercbfb6002014-10-03 00:32:37 -0700106 self._initial = self._dict # for DiffModelMixIn
Scott Baker5dc87a62014-09-23 22:41:17 -0700107 self.silent = False
Scott Baker13acdd62013-05-08 17:42:56 -0700108
Tony Mack5b061472014-02-04 07:57:10 -0500109 def can_update(self, user):
110 if user.is_readonly:
111 return False
112 if user.is_admin:
113 return True
Tony Mack5b061472014-02-04 07:57:10 -0500114
Scott Baker0119c152014-10-06 22:58:48 -0700115 return False
Scott Bakercbfb6002014-10-03 00:32:37 -0700116
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -0400117 def delete(self, *args, **kwds):
Scott Baker6ecd4262014-01-21 23:15:21 -0800118 # so we have something to give the observer
Sapan Bhatia77d1d892014-07-21 20:07:23 -0400119 purge = kwds.get('purge',False)
Scott Baker0491f6f2014-09-23 16:04:36 -0700120 silent = kwds.get('silent',False)
Sapan Bhatia77d1d892014-07-21 20:07:23 -0400121 try:
122 purge = purge or observer_disabled
123 except NameError:
124 pass
Scott Baker0491f6f2014-09-23 16:04:36 -0700125
Sapan Bhatia77d1d892014-07-21 20:07:23 -0400126 if (purge):
Sapan Bhatiaad2c4a12014-07-22 01:21:05 -0400127 del kwds['purge']
Sapan Bhatia77d1d892014-07-21 20:07:23 -0400128 super(PlCoreBase, self).delete(*args, **kwds)
Sapan Bhatiac8602432014-04-29 20:33:51 -0400129 else:
130 self.deleted = True
131 self.enacted=None
Scott Baker0491f6f2014-09-23 16:04:36 -0700132 self.save(update_fields=['enacted','deleted'], silent=silent)
Sapan Bhatiadbaf1932013-09-03 11:28:52 -0400133
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -0400134 def save(self, *args, **kwargs):
Scott Baker5dc87a62014-09-23 22:41:17 -0700135 # let the user specify silence as either a kwarg or an instance varible
136 silent = self.silent
Scott Baker0491f6f2014-09-23 16:04:36 -0700137 if "silent" in kwargs:
Scott Baker5dc87a62014-09-23 22:41:17 -0700138 silent=silent or kwargs.pop("silent")
Scott Baker0491f6f2014-09-23 16:04:36 -0700139
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -0400140 super(PlCoreBase, self).save(*args, **kwargs)
Sapan Bhatiac8602432014-04-29 20:33:51 -0400141
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -0400142 # This is a no-op if observer_disabled is set
Scott Baker0491f6f2014-09-23 16:04:36 -0700143 if not silent:
144 notify_observer()
Sapan Bhatia66f4e612013-07-02 12:12:38 -0400145
Scott Baker165f70c2014-10-03 14:48:06 -0700146 self._initial = self._dict
Scott Baker13acdd62013-05-08 17:42:56 -0700147
Tony Mack5b061472014-02-04 07:57:10 -0500148 def save_by_user(self, user, *args, **kwds):
Scott Baker0bec56f2014-10-02 22:50:18 -0700149 if not self.can_update(user):
Scott Baker0119c152014-10-06 22:58:48 -0700150 if getattr(self, "_cant_update_fieldName", None) is not None:
151 raise PermissionDenied("You do not have permission to update field %s on object %s" % (self._cant_update_fieldName, self.__class__.__name__))
152 else:
153 raise PermissionDenied("You do not have permission to update %s objects" % self.__class__.__name__)
Scott Bakercbfb6002014-10-03 00:32:37 -0700154
Scott Baker0bec56f2014-10-02 22:50:18 -0700155 self.save(*args, **kwds)
Tony Mack5b061472014-02-04 07:57:10 -0500156
Tony Mack332ee1d2014-02-04 15:33:45 -0500157 def delete_by_user(self, user, *args, **kwds):
Scott Baker0bec56f2014-10-02 22:50:18 -0700158 if not self.can_update(user):
159 raise PermissionDenied("You do not have permission to delete %s objects" % self.__class__.__name__)
160 self.delete(*args, **kwds)
Tony Mack332ee1d2014-02-04 15:33:45 -0500161
Scott Baker165f70c2014-10-03 14:48:06 -0700162 @classmethod
163 def select_by_user(cls, user):
164 # This should be overridden by descendant classes that want to perform
165 # filtering of visible objects by user.
166 return cls.objects.all()
167
Scott Baker13acdd62013-05-08 17:42:56 -0700168
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400169
170