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