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