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 | |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 41 | def delete(self, *args, **kwds): |
Scott Baker | 6ecd426 | 2014-01-21 23:15:21 -0800 | [diff] [blame] | 42 | # so we have something to give the observer |
| 43 | pk = self.pk |
| 44 | model_dict = model_to_dict(self) |
| 45 | for (k,v) in model_dict.items(): |
| 46 | # things like datetime are not JSON serializable |
| 47 | model_dict[k] = str(v) |
| 48 | |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 49 | super(PlCoreBase, self).delete(*args, **kwds) |
Sapan Bhatia | dbaf193 | 2013-09-03 11:28:52 -0400 | [diff] [blame] | 50 | |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 51 | # This is a no-op if observer_disabled is set |
Scott Baker | 6ecd426 | 2014-01-21 23:15:21 -0800 | [diff] [blame] | 52 | notify_observer(model=self, delete=True, pk=pk, model_dict=model_dict) |
Sapan Bhatia | dbaf193 | 2013-09-03 11:28:52 -0400 | [diff] [blame] | 53 | |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 54 | def save(self, *args, **kwargs): |
| 55 | super(PlCoreBase, self).save(*args, **kwargs) |
| 56 | |
| 57 | # This is a no-op if observer_disabled is set |
| 58 | notify_observer() |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 59 | |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 60 | self.__initial = self._dict |
Scott Baker | 13acdd6 | 2013-05-08 17:42:56 -0700 | [diff] [blame] | 61 | |
Sapan Bhatia | 9c2c8fa | 2013-10-16 13:26:05 -0400 | [diff] [blame] | 62 | @property |
| 63 | def _dict(self): |
| 64 | return model_to_dict(self, fields=[field.name for field in |
| 65 | self._meta.fields]) |
Scott Baker | 13acdd6 | 2013-05-08 17:42:56 -0700 | [diff] [blame] | 66 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 67 | |
| 68 | |