blob: 446245bc7d76b896f077cf54afe24f5924f2abe7 [file] [log] [blame]
Scott Bakerfd44dfc2014-05-23 13:20:53 -07001import datetime
Siobhan Tully4bc09f22013-04-10 21:15:21 -04002import os
Scott Baker65d5a9a2014-05-26 15:58:09 -07003import sys
Siobhan Tully4bc09f22013-04-10 21:15:21 -04004from django.db import models
Scott Baker13acdd62013-05-08 17:42:56 -07005from django.forms.models import model_to_dict
Scott Bakerc1c45f82014-01-21 16:23:51 -08006from django.core.urlresolvers import reverse
Scott Baker6ecd4262014-01-21 23:15:21 -08007from django.forms.models import model_to_dict
Scott Baker9e990742014-03-19 22:14:58 -07008
9try:
10 # This is a no-op if observer_disabled is set to 1 in the config file
11 from observer import *
12except:
Scott Baker65d5a9a2014-05-26 15:58:09 -070013 print >> sys.stderr, "import of observer failed! printing traceback and disabling observer:"
Scott Baker9e990742014-03-19 22:14:58 -070014 import traceback
15 traceback.print_exc()
16
17 # guard against something failing
Scott Bakerfd44dfc2014-05-23 13:20:53 -070018 def notify_observer(*args, **kwargs):
Scott Baker9e990742014-03-19 22:14:58 -070019 pass
Siobhan Tully4bc09f22013-04-10 21:15:21 -040020
Sapan Bhatia3089d832014-04-29 14:36:51 -040021# This manager will be inherited by all subclasses because
22# the core model is abstract.
Sapan Bhatia15bf5ac2014-07-21 20:06:59 -040023class PlCoreBaseDeletionManager(models.Manager):
24 def get_query_set(self):
Sapan Bhatiabbcc1902014-07-22 01:11:36 -040025 return super(PlCoreBaseDeletionManager, self).get_query_set().filter(deleted=True)
Sapan Bhatia15bf5ac2014-07-21 20:06:59 -040026
27# This manager will be inherited by all subclasses because
28# the core model is abstract.
Sapan Bhatia4eb663a2014-04-29 14:26:10 -040029class PlCoreBaseManager(models.Manager):
30 def get_query_set(self):
31 return super(PlCoreBaseManager, self).get_query_set().filter(deleted=False)
Siobhan Tully4bc09f22013-04-10 21:15:21 -040032
Sapan Bhatia4eb663a2014-04-29 14:26:10 -040033class PlCoreBase(models.Model):
34 objects = PlCoreBaseManager()
Sapan Bhatia15bf5ac2014-07-21 20:06:59 -040035 deleted_objects = PlCoreBaseDeletionManager()
36
37 # default values for created and updated are only there to keep evolution
38 # from failing.
Scott Bakerfd44dfc2014-05-23 13:20:53 -070039 created = models.DateTimeField(auto_now_add=True, default=datetime.datetime.now())
40 updated = models.DateTimeField(auto_now=True, default=datetime.datetime.now())
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040041 enacted = models.DateTimeField(null=True, default=None)
Sapan Bhatia34aee752014-04-28 21:06:39 -040042 backend_status = models.CharField(max_length=140,
Sapan Bhatiad507f432014-04-29 00:41:39 -040043 default="Provisioning in progress")
Sapan Bhatiabcc18992014-04-29 10:32:14 -040044 deleted = models.BooleanField(default=False)
Siobhan Tully4bc09f22013-04-10 21:15:21 -040045
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040046 class Meta:
Sapan Bhatia3089d832014-04-29 14:36:51 -040047 # Changing abstract to False would require the managers of subclasses of
48 # PlCoreBase to be customized individually.
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040049 abstract = True
50 app_label = "core"
Siobhan Tully4bc09f22013-04-10 21:15:21 -040051
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040052 def __init__(self, *args, **kwargs):
53 super(PlCoreBase, self).__init__(*args, **kwargs)
54 self.__initial = self._dict
Scott Baker13acdd62013-05-08 17:42:56 -070055
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040056 @property
57 def diff(self):
58 d1 = self.__initial
59 d2 = self._dict
60 diffs = [(k, (v, d2[k])) for k, v in d1.items() if v != d2[k]]
61 return dict(diffs)
Siobhan Tully73291342013-05-10 10:50:08 -040062
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040063 @property
64 def has_changed(self):
65 return bool(self.diff)
Siobhan Tully73291342013-05-10 10:50:08 -040066
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040067 @property
68 def changed_fields(self):
69 return self.diff.keys()
Siobhan Tully73291342013-05-10 10:50:08 -040070
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040071 def get_field_diff(self, field_name):
72 return self.diff.get(field_name, None)
Siobhan Tully73291342013-05-10 10:50:08 -040073
Tony Mack5b061472014-02-04 07:57:10 -050074 def can_update(self, user):
75 if user.is_readonly:
76 return False
77 if user.is_admin:
78 return True
79 return False
80
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040081 def delete(self, *args, **kwds):
Scott Baker6ecd4262014-01-21 23:15:21 -080082 # so we have something to give the observer
Sapan Bhatia77d1d892014-07-21 20:07:23 -040083 purge = kwds.get('purge',False)
84 try:
85 purge = purge or observer_disabled
86 except NameError:
87 pass
88
89 if (purge):
Sapan Bhatiaad2c4a12014-07-22 01:21:05 -040090 del kwds['purge']
Sapan Bhatia77d1d892014-07-21 20:07:23 -040091 super(PlCoreBase, self).delete(*args, **kwds)
Sapan Bhatiac8602432014-04-29 20:33:51 -040092 else:
93 self.deleted = True
94 self.enacted=None
95 self.save(update_fields=['enacted','deleted'])
Scott Baker6ecd4262014-01-21 23:15:21 -080096
Sapan Bhatiadbaf1932013-09-03 11:28:52 -040097
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -040098 def save(self, *args, **kwargs):
99 super(PlCoreBase, self).save(*args, **kwargs)
Sapan Bhatiac8602432014-04-29 20:33:51 -0400100
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -0400101 # This is a no-op if observer_disabled is set
102 notify_observer()
Sapan Bhatia66f4e612013-07-02 12:12:38 -0400103
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -0400104 self.__initial = self._dict
Scott Baker13acdd62013-05-08 17:42:56 -0700105
Tony Mack5b061472014-02-04 07:57:10 -0500106 def save_by_user(self, user, *args, **kwds):
107 if self.can_update(user):
108 self.save(*args, **kwds)
109
Tony Mack332ee1d2014-02-04 15:33:45 -0500110 def delete_by_user(self, user, *args, **kwds):
111 if self.can_update(user):
112 self.delete(*args, **kwds)
113
Sapan Bhatia9c2c8fa2013-10-16 13:26:05 -0400114 @property
115 def _dict(self):
116 return model_to_dict(self, fields=[field.name for field in
117 self._meta.fields])
Scott Baker13acdd62013-05-08 17:42:56 -0700118
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400119
120