blob: 8d657a7b0230ef0de27689b1a5153f9902099258 [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
Sapan Bhatia39097192014-09-04 00:39:19 -04008import model_policy
Scott Baker9e990742014-03-19 22:14:58 -07009
10try:
11 # This is a no-op if observer_disabled is set to 1 in the config file
12 from observer import *
13except:
Scott Baker65d5a9a2014-05-26 15:58:09 -070014 print >> sys.stderr, "import of observer failed! printing traceback and disabling observer:"
Scott Baker9e990742014-03-19 22:14:58 -070015 import traceback
16 traceback.print_exc()
17
18 # guard against something failing
Scott Bakerfd44dfc2014-05-23 13:20:53 -070019 def notify_observer(*args, **kwargs):
Scott Baker9e990742014-03-19 22:14:58 -070020 pass
Siobhan Tully4bc09f22013-04-10 21:15:21 -040021
Sapan Bhatia3089d832014-04-29 14:36:51 -040022# This manager will be inherited by all subclasses because
23# the core model is abstract.
Sapan Bhatia15bf5ac2014-07-21 20:06:59 -040024class PlCoreBaseDeletionManager(models.Manager):
Scott Baker92f14222014-09-12 12:57:27 -070025 def get_queryset(self):
Scott Baker02b59522014-09-17 22:18:46 -070026 parent=super(PlCoreBaseDeletionManager, self)
27 if hasattr(parent, "get_queryset"):
28 return parent.get_queryset().filter(deleted=True)
29 else:
30 return parent.get_query_set().filter(deleted=True)
Sapan Bhatia15bf5ac2014-07-21 20:06:59 -040031
Scott Baker02b59522014-09-17 22:18:46 -070032 # deprecated in django 1.7 in favor of get_queryset().
Scott Baker92f14222014-09-12 12:57:27 -070033 def get_query_set(self):
34 return self.get_queryset()
35
Sapan Bhatia15bf5ac2014-07-21 20:06:59 -040036# This manager will be inherited by all subclasses because
37# the core model is abstract.
Sapan Bhatia4eb663a2014-04-29 14:26:10 -040038class PlCoreBaseManager(models.Manager):
Scott Baker92f14222014-09-12 12:57:27 -070039 def get_queryset(self):
Scott Baker02b59522014-09-17 22:18:46 -070040 parent=super(PlCoreBaseManager, self)
41 if hasattr(parent, "get_queryset"):
42 return parent.get_queryset().filter(deleted=False)
43 else:
44 return parent.get_query_set().filter(deleted=False)
Siobhan Tully4bc09f22013-04-10 21:15:21 -040045
Scott Baker02b59522014-09-17 22:18:46 -070046 # deprecated in django 1.7 in favor of get_queryset().
Scott Baker92f14222014-09-12 12:57:27 -070047 def get_query_set(self):
48 return self.get_queryset()
49
Sapan Bhatia4eb663a2014-04-29 14:26:10 -040050class PlCoreBase(models.Model):
51 objects = PlCoreBaseManager()
Sapan Bhatia15bf5ac2014-07-21 20:06:59 -040052 deleted_objects = PlCoreBaseDeletionManager()
53
54 # default values for created and updated are only there to keep evolution
55 # from failing.
Scott Baker02b59522014-09-17 22:18:46 -070056 created = models.DateTimeField(auto_now_add=True, default=timezone.now)
57 updated = models.DateTimeField(auto_now=True, default=timezone.now)
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040058 enacted = models.DateTimeField(null=True, default=None)
Sapan Bhatia34aee752014-04-28 21:06:39 -040059 backend_status = models.CharField(max_length=140,
Sapan Bhatiad507f432014-04-29 00:41:39 -040060 default="Provisioning in progress")
Sapan Bhatiabcc18992014-04-29 10:32:14 -040061 deleted = models.BooleanField(default=False)
Siobhan Tully4bc09f22013-04-10 21:15:21 -040062
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040063 class Meta:
Sapan Bhatia3089d832014-04-29 14:36:51 -040064 # Changing abstract to False would require the managers of subclasses of
65 # PlCoreBase to be customized individually.
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040066 abstract = True
67 app_label = "core"
Siobhan Tully4bc09f22013-04-10 21:15:21 -040068
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040069 def __init__(self, *args, **kwargs):
70 super(PlCoreBase, self).__init__(*args, **kwargs)
71 self.__initial = self._dict
Scott Bakerf3f895c2014-09-23 22:41:17 -070072 self.silent = False
Scott Baker13acdd62013-05-08 17:42:56 -070073
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040074 @property
75 def diff(self):
76 d1 = self.__initial
77 d2 = self._dict
78 diffs = [(k, (v, d2[k])) for k, v in d1.items() if v != d2[k]]
79 return dict(diffs)
Siobhan Tully73291342013-05-10 10:50:08 -040080
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040081 @property
82 def has_changed(self):
83 return bool(self.diff)
Siobhan Tully73291342013-05-10 10:50:08 -040084
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040085 @property
86 def changed_fields(self):
87 return self.diff.keys()
Siobhan Tully73291342013-05-10 10:50:08 -040088
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040089 def get_field_diff(self, field_name):
90 return self.diff.get(field_name, None)
Siobhan Tully73291342013-05-10 10:50:08 -040091
Tony Mack5b061472014-02-04 07:57:10 -050092 def can_update(self, user):
93 if user.is_readonly:
94 return False
95 if user.is_admin:
96 return True
97 return False
98
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040099 def delete(self, *args, **kwds):
Scott Baker6ecd4262014-01-21 23:15:21 -0800100 # so we have something to give the observer
Sapan Bhatia77d1d892014-07-21 20:07:23 -0400101 purge = kwds.get('purge',False)
Scott Baker6594bea2014-09-23 16:04:36 -0700102 silent = kwds.get('silent',False)
Sapan Bhatia77d1d892014-07-21 20:07:23 -0400103 try:
104 purge = purge or observer_disabled
105 except NameError:
106 pass
Scott Baker6594bea2014-09-23 16:04:36 -0700107
Sapan Bhatia77d1d892014-07-21 20:07:23 -0400108 if (purge):
Sapan Bhatiaad2c4a12014-07-22 01:21:05 -0400109 del kwds['purge']
Sapan Bhatia77d1d892014-07-21 20:07:23 -0400110 super(PlCoreBase, self).delete(*args, **kwds)
Sapan Bhatiac8602432014-04-29 20:33:51 -0400111 else:
112 self.deleted = True
113 self.enacted=None
Scott Baker6594bea2014-09-23 16:04:36 -0700114 self.save(update_fields=['enacted','deleted'], silent=silent)
Sapan Bhatiadbaf1932013-09-03 11:28:52 -0400115
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -0400116 def save(self, *args, **kwargs):
Scott Bakerf3f895c2014-09-23 22:41:17 -0700117 # let the user specify silence as either a kwarg or an instance varible
118 silent = self.silent
Scott Baker6594bea2014-09-23 16:04:36 -0700119 if "silent" in kwargs:
Scott Bakerf3f895c2014-09-23 22:41:17 -0700120 silent=silent or kwargs.pop("silent")
Scott Baker6594bea2014-09-23 16:04:36 -0700121
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -0400122 super(PlCoreBase, self).save(*args, **kwargs)
Sapan Bhatiac8602432014-04-29 20:33:51 -0400123
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -0400124 # This is a no-op if observer_disabled is set
Scott Baker6594bea2014-09-23 16:04:36 -0700125 if not silent:
126 notify_observer()
Sapan Bhatia66f4e612013-07-02 12:12:38 -0400127
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -0400128 self.__initial = self._dict
Scott Baker13acdd62013-05-08 17:42:56 -0700129
Tony Mack5b061472014-02-04 07:57:10 -0500130 def save_by_user(self, user, *args, **kwds):
131 if self.can_update(user):
132 self.save(*args, **kwds)
133
Tony Mack332ee1d2014-02-04 15:33:45 -0500134 def delete_by_user(self, user, *args, **kwds):
135 if self.can_update(user):
136 self.delete(*args, **kwds)
137
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -0400138 @property
139 def _dict(self):
140 return model_to_dict(self, fields=[field.name for field in
141 self._meta.fields])
Scott Baker13acdd62013-05-08 17:42:56 -0700142
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400143
144