blob: b9692c6df3fe500bc22fdbac2f3c25aeabf29634 [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 Baker02b59522014-09-17 22:18:46 -07007from django.utils import timezone
Scott Bakere5f41b02014-10-02 22:50:18 -07008from django.core.exceptions import PermissionDenied
Sapan Bhatia39097192014-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 Baker92f14222014-09-12 12:57:27 -070026 def get_queryset(self):
Scott Baker02b59522014-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 Baker02b59522014-09-17 22:18:46 -070033 # deprecated in django 1.7 in favor of get_queryset().
Scott Baker92f14222014-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 Baker92f14222014-09-12 12:57:27 -070040 def get_queryset(self):
Scott Baker02b59522014-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 Baker02b59522014-09-17 22:18:46 -070047 # deprecated in django 1.7 in favor of get_queryset().
Scott Baker92f14222014-09-12 12:57:27 -070048 def get_query_set(self):
49 return self.get_queryset()
50
Sapan Bhatia4eb663a2014-04-29 14:26:10 -040051class PlCoreBase(models.Model):
52 objects = PlCoreBaseManager()
Sapan Bhatia15bf5ac2014-07-21 20:06:59 -040053 deleted_objects = PlCoreBaseDeletionManager()
54
55 # default values for created and updated are only there to keep evolution
56 # from failing.
Scott Baker02b59522014-09-17 22:18:46 -070057 created = models.DateTimeField(auto_now_add=True, default=timezone.now)
58 updated = models.DateTimeField(auto_now=True, default=timezone.now)
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040059 enacted = models.DateTimeField(null=True, default=None)
Sapan Bhatia34aee752014-04-28 21:06:39 -040060 backend_status = models.CharField(max_length=140,
Sapan Bhatiad507f432014-04-29 00:41:39 -040061 default="Provisioning in progress")
Sapan Bhatiabcc18992014-04-29 10:32:14 -040062 deleted = models.BooleanField(default=False)
Siobhan Tully4bc09f22013-04-10 21:15:21 -040063
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040064 class Meta:
Sapan Bhatia3089d832014-04-29 14:36:51 -040065 # Changing abstract to False would require the managers of subclasses of
66 # PlCoreBase to be customized individually.
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040067 abstract = True
68 app_label = "core"
Siobhan Tully4bc09f22013-04-10 21:15:21 -040069
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040070 def __init__(self, *args, **kwargs):
71 super(PlCoreBase, self).__init__(*args, **kwargs)
72 self.__initial = self._dict
Scott Bakerf3f895c2014-09-23 22:41:17 -070073 self.silent = False
Scott Baker13acdd62013-05-08 17:42:56 -070074
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040075 @property
76 def diff(self):
77 d1 = self.__initial
78 d2 = self._dict
79 diffs = [(k, (v, d2[k])) for k, v in d1.items() if v != d2[k]]
80 return dict(diffs)
Siobhan Tully73291342013-05-10 10:50:08 -040081
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040082 @property
83 def has_changed(self):
84 return bool(self.diff)
Siobhan Tully73291342013-05-10 10:50:08 -040085
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040086 @property
87 def changed_fields(self):
88 return self.diff.keys()
Siobhan Tully73291342013-05-10 10:50:08 -040089
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040090 def get_field_diff(self, field_name):
91 return self.diff.get(field_name, None)
Siobhan Tully73291342013-05-10 10:50:08 -040092
Tony Mack5b061472014-02-04 07:57:10 -050093 def can_update(self, user):
94 if user.is_readonly:
95 return False
96 if user.is_admin:
97 return True
98 return False
99
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -0400100 def delete(self, *args, **kwds):
Scott Baker6ecd4262014-01-21 23:15:21 -0800101 # so we have something to give the observer
Sapan Bhatia77d1d892014-07-21 20:07:23 -0400102 purge = kwds.get('purge',False)
Scott Baker6594bea2014-09-23 16:04:36 -0700103 silent = kwds.get('silent',False)
Sapan Bhatia77d1d892014-07-21 20:07:23 -0400104 try:
105 purge = purge or observer_disabled
106 except NameError:
107 pass
Scott Baker6594bea2014-09-23 16:04:36 -0700108
Sapan Bhatia77d1d892014-07-21 20:07:23 -0400109 if (purge):
Sapan Bhatiaad2c4a12014-07-22 01:21:05 -0400110 del kwds['purge']
Sapan Bhatia77d1d892014-07-21 20:07:23 -0400111 super(PlCoreBase, self).delete(*args, **kwds)
Sapan Bhatiac8602432014-04-29 20:33:51 -0400112 else:
113 self.deleted = True
114 self.enacted=None
Scott Baker6594bea2014-09-23 16:04:36 -0700115 self.save(update_fields=['enacted','deleted'], silent=silent)
Sapan Bhatiadbaf1932013-09-03 11:28:52 -0400116
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -0400117 def save(self, *args, **kwargs):
Scott Bakerf3f895c2014-09-23 22:41:17 -0700118 # let the user specify silence as either a kwarg or an instance varible
119 silent = self.silent
Scott Baker6594bea2014-09-23 16:04:36 -0700120 if "silent" in kwargs:
Scott Bakerf3f895c2014-09-23 22:41:17 -0700121 silent=silent or kwargs.pop("silent")
Scott Baker6594bea2014-09-23 16:04:36 -0700122
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -0400123 super(PlCoreBase, self).save(*args, **kwargs)
Sapan Bhatiac8602432014-04-29 20:33:51 -0400124
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -0400125 # This is a no-op if observer_disabled is set
Scott Baker6594bea2014-09-23 16:04:36 -0700126 if not silent:
127 notify_observer()
Sapan Bhatia66f4e612013-07-02 12:12:38 -0400128
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -0400129 self.__initial = self._dict
Scott Baker13acdd62013-05-08 17:42:56 -0700130
Tony Mack5b061472014-02-04 07:57:10 -0500131 def save_by_user(self, user, *args, **kwds):
Scott Bakere5f41b02014-10-02 22:50:18 -0700132 if not self.can_update(user):
133 raise PermissionDenied("You do not have permission to update %s objects" % self.__class__.__name__)
134 self.save(*args, **kwds)
Tony Mack5b061472014-02-04 07:57:10 -0500135
Tony Mack332ee1d2014-02-04 15:33:45 -0500136 def delete_by_user(self, user, *args, **kwds):
Scott Bakere5f41b02014-10-02 22:50:18 -0700137 if not self.can_update(user):
138 raise PermissionDenied("You do not have permission to delete %s objects" % self.__class__.__name__)
139 self.delete(*args, **kwds)
Tony Mack332ee1d2014-02-04 15:33:45 -0500140
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -0400141 @property
142 def _dict(self):
143 return model_to_dict(self, fields=[field.name for field in
144 self._meta.fields])
Scott Baker13acdd62013-05-08 17:42:56 -0700145
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400146
147