Scott Baker | fd44dfc | 2014-05-23 13:20:53 -0700 | [diff] [blame] | 1 | import datetime |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 2 | import os |
Scott Baker | 65d5a9a | 2014-05-26 15:58:09 -0700 | [diff] [blame] | 3 | import sys |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 4 | from django.db import models |
Scott Baker | 13acdd6 | 2013-05-08 17:42:56 -0700 | [diff] [blame] | 5 | from django.forms.models import model_to_dict |
Scott Baker | c1c45f8 | 2014-01-21 16:23:51 -0800 | [diff] [blame] | 6 | from django.core.urlresolvers import reverse |
Scott Baker | 6ecd426 | 2014-01-21 23:15:21 -0800 | [diff] [blame] | 7 | from django.forms.models import model_to_dict |
Scott Baker | 9e99074 | 2014-03-19 22:14:58 -0700 | [diff] [blame] | 8 | |
| 9 | try: |
| 10 | # This is a no-op if observer_disabled is set to 1 in the config file |
| 11 | from observer import * |
| 12 | except: |
Scott Baker | 65d5a9a | 2014-05-26 15:58:09 -0700 | [diff] [blame] | 13 | print >> sys.stderr, "import of observer failed! printing traceback and disabling observer:" |
Scott Baker | 9e99074 | 2014-03-19 22:14:58 -0700 | [diff] [blame] | 14 | import traceback |
| 15 | traceback.print_exc() |
| 16 | |
| 17 | # guard against something failing |
Scott Baker | fd44dfc | 2014-05-23 13:20:53 -0700 | [diff] [blame] | 18 | def notify_observer(*args, **kwargs): |
Scott Baker | 9e99074 | 2014-03-19 22:14:58 -0700 | [diff] [blame] | 19 | pass |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 20 | |
| 21 | class PlCoreBase(models.Model): |
| 22 | |
Scott Baker | fd44dfc | 2014-05-23 13:20:53 -0700 | [diff] [blame] | 23 | # default values for created and updated are only there to keep evolution |
| 24 | # from failing. |
| 25 | |
| 26 | created = models.DateTimeField(auto_now_add=True, default=datetime.datetime.now()) |
| 27 | updated = models.DateTimeField(auto_now=True, default=datetime.datetime.now()) |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 28 | enacted = models.DateTimeField(null=True, default=None) |
Sapan Bhatia | 34aee75 | 2014-04-28 21:06:39 -0400 | [diff] [blame] | 29 | backend_status = models.CharField(max_length=140, |
| 30 | default="Ok") |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 31 | |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 32 | class Meta: |
| 33 | abstract = True |
| 34 | app_label = "core" |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 35 | |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 36 | def __init__(self, *args, **kwargs): |
| 37 | super(PlCoreBase, self).__init__(*args, **kwargs) |
| 38 | self.__initial = self._dict |
Scott Baker | 13acdd6 | 2013-05-08 17:42:56 -0700 | [diff] [blame] | 39 | |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 40 | @property |
| 41 | def diff(self): |
| 42 | d1 = self.__initial |
| 43 | d2 = self._dict |
| 44 | diffs = [(k, (v, d2[k])) for k, v in d1.items() if v != d2[k]] |
| 45 | return dict(diffs) |
Siobhan Tully | 7329134 | 2013-05-10 10:50:08 -0400 | [diff] [blame] | 46 | |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 47 | @property |
| 48 | def has_changed(self): |
| 49 | return bool(self.diff) |
Siobhan Tully | 7329134 | 2013-05-10 10:50:08 -0400 | [diff] [blame] | 50 | |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 51 | @property |
| 52 | def changed_fields(self): |
| 53 | return self.diff.keys() |
Siobhan Tully | 7329134 | 2013-05-10 10:50:08 -0400 | [diff] [blame] | 54 | |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 55 | def get_field_diff(self, field_name): |
| 56 | return self.diff.get(field_name, None) |
Siobhan Tully | 7329134 | 2013-05-10 10:50:08 -0400 | [diff] [blame] | 57 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 58 | def can_update(self, user): |
| 59 | if user.is_readonly: |
| 60 | return False |
| 61 | if user.is_admin: |
| 62 | return True |
| 63 | return False |
| 64 | |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 65 | def delete(self, *args, **kwds): |
Scott Baker | 6ecd426 | 2014-01-21 23:15:21 -0800 | [diff] [blame] | 66 | # so we have something to give the observer |
| 67 | pk = self.pk |
| 68 | model_dict = model_to_dict(self) |
| 69 | for (k,v) in model_dict.items(): |
| 70 | # things like datetime are not JSON serializable |
| 71 | model_dict[k] = str(v) |
| 72 | |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 73 | super(PlCoreBase, self).delete(*args, **kwds) |
Sapan Bhatia | dbaf193 | 2013-09-03 11:28:52 -0400 | [diff] [blame] | 74 | |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 75 | # This is a no-op if observer_disabled is set |
Scott Baker | 6ecd426 | 2014-01-21 23:15:21 -0800 | [diff] [blame] | 76 | notify_observer(model=self, delete=True, pk=pk, model_dict=model_dict) |
Sapan Bhatia | dbaf193 | 2013-09-03 11:28:52 -0400 | [diff] [blame] | 77 | |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 78 | def save(self, *args, **kwargs): |
| 79 | super(PlCoreBase, self).save(*args, **kwargs) |
| 80 | |
| 81 | # This is a no-op if observer_disabled is set |
| 82 | notify_observer() |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 83 | |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 84 | self.__initial = self._dict |
Scott Baker | 13acdd6 | 2013-05-08 17:42:56 -0700 | [diff] [blame] | 85 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 86 | def save_by_user(self, user, *args, **kwds): |
| 87 | if self.can_update(user): |
| 88 | self.save(*args, **kwds) |
| 89 | |
Tony Mack | 332ee1d | 2014-02-04 15:33:45 -0500 | [diff] [blame] | 90 | def delete_by_user(self, user, *args, **kwds): |
| 91 | if self.can_update(user): |
| 92 | self.delete(*args, **kwds) |
| 93 | |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 94 | @property |
| 95 | def _dict(self): |
| 96 | return model_to_dict(self, fields=[field.name for field in |
| 97 | self._meta.fields]) |
Scott Baker | 13acdd6 | 2013-05-08 17:42:56 -0700 | [diff] [blame] | 98 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 99 | |
| 100 | |