blob: 4263f943dc6a5b89fb64efdf388906a1325dfc14 [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
Sapan Bhatiabad67742014-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 Bakerb08d6562014-09-12 12:57:27 -070025 def get_queryset(self):
Scott Bakerb24f2c32014-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 Bakerb24f2c32014-09-17 22:18:46 -070032 # deprecated in django 1.7 in favor of get_queryset().
Scott Bakerb08d6562014-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 Bakerb08d6562014-09-12 12:57:27 -070039 def get_queryset(self):
Scott Bakerb24f2c32014-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 Bakerb24f2c32014-09-17 22:18:46 -070046 # deprecated in django 1.7 in favor of get_queryset().
Scott Bakerb08d6562014-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 Bakerb24f2c32014-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 Baker13acdd62013-05-08 17:42:56 -070072
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040073 @property
74 def diff(self):
75 d1 = self.__initial
76 d2 = self._dict
77 diffs = [(k, (v, d2[k])) for k, v in d1.items() if v != d2[k]]
78 return dict(diffs)
Siobhan Tully73291342013-05-10 10:50:08 -040079
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040080 @property
81 def has_changed(self):
82 return bool(self.diff)
Siobhan Tully73291342013-05-10 10:50:08 -040083
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040084 @property
85 def changed_fields(self):
86 return self.diff.keys()
Siobhan Tully73291342013-05-10 10:50:08 -040087
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040088 def get_field_diff(self, field_name):
89 return self.diff.get(field_name, None)
Siobhan Tully73291342013-05-10 10:50:08 -040090
Tony Mack5b061472014-02-04 07:57:10 -050091 def can_update(self, user):
92 if user.is_readonly:
93 return False
94 if user.is_admin:
95 return True
96 return False
97
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040098 def delete(self, *args, **kwds):
Scott Baker6ecd4262014-01-21 23:15:21 -080099 # so we have something to give the observer
Sapan Bhatia77d1d892014-07-21 20:07:23 -0400100 purge = kwds.get('purge',False)
Scott Baker0491f6f2014-09-23 16:04:36 -0700101 silent = kwds.get('silent',False)
Sapan Bhatia77d1d892014-07-21 20:07:23 -0400102 try:
103 purge = purge or observer_disabled
104 except NameError:
105 pass
Scott Baker0491f6f2014-09-23 16:04:36 -0700106
Sapan Bhatia77d1d892014-07-21 20:07:23 -0400107 if (purge):
Sapan Bhatiaad2c4a12014-07-22 01:21:05 -0400108 del kwds['purge']
Sapan Bhatia77d1d892014-07-21 20:07:23 -0400109 super(PlCoreBase, self).delete(*args, **kwds)
Sapan Bhatiac8602432014-04-29 20:33:51 -0400110 else:
111 self.deleted = True
112 self.enacted=None
Scott Baker0491f6f2014-09-23 16:04:36 -0700113 self.save(update_fields=['enacted','deleted'], silent=silent)
Sapan Bhatiadbaf1932013-09-03 11:28:52 -0400114
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -0400115 def save(self, *args, **kwargs):
Scott Baker0491f6f2014-09-23 16:04:36 -0700116 if "silent" in kwargs:
117 silent=kwargs.pop("silent")
118 else:
119 silent=False
120
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -0400121 super(PlCoreBase, self).save(*args, **kwargs)
Sapan Bhatiac8602432014-04-29 20:33:51 -0400122
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -0400123 # This is a no-op if observer_disabled is set
Scott Baker0491f6f2014-09-23 16:04:36 -0700124 if not silent:
125 notify_observer()
Sapan Bhatia66f4e612013-07-02 12:12:38 -0400126
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -0400127 self.__initial = self._dict
Scott Baker13acdd62013-05-08 17:42:56 -0700128
Tony Mack5b061472014-02-04 07:57:10 -0500129 def save_by_user(self, user, *args, **kwds):
130 if self.can_update(user):
131 self.save(*args, **kwds)
132
Tony Mack332ee1d2014-02-04 15:33:45 -0500133 def delete_by_user(self, user, *args, **kwds):
134 if self.can_update(user):
135 self.delete(*args, **kwds)
136
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -0400137 @property
138 def _dict(self):
139 return model_to_dict(self, fields=[field.name for field in
140 self._meta.fields])
Scott Baker13acdd62013-05-08 17:42:56 -0700141
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400142
143