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