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 |
Sapan Bhatia | bd02841 | 2013-09-03 11:53:18 -0400 | [diff] [blame^] | 4 | from observer.event_manager import EventSender |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 5 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 6 | |
| 7 | class PlCoreBase(models.Model): |
| 8 | |
Sapan Bhatia | 541f953 | 2013-09-03 11:43:55 -0400 | [diff] [blame] | 9 | created = models.DateTimeField(auto_now_add=True) |
| 10 | updated = models.DateTimeField(auto_now=True) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 11 | |
Sapan Bhatia | 541f953 | 2013-09-03 11:43:55 -0400 | [diff] [blame] | 12 | class Meta: |
| 13 | abstract = True |
| 14 | app_label = "core" |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 15 | |
Sapan Bhatia | 541f953 | 2013-09-03 11:43:55 -0400 | [diff] [blame] | 16 | def __init__(self, *args, **kwargs): |
| 17 | super(PlCoreBase, self).__init__(*args, **kwargs) |
| 18 | self.__initial = self._dict |
Scott Baker | 13acdd6 | 2013-05-08 17:42:56 -0700 | [diff] [blame] | 19 | |
Sapan Bhatia | 541f953 | 2013-09-03 11:43:55 -0400 | [diff] [blame] | 20 | @property |
| 21 | def diff(self): |
| 22 | d1 = self.__initial |
| 23 | d2 = self._dict |
| 24 | diffs = [(k, (v, d2[k])) for k, v in d1.items() if v != d2[k]] |
| 25 | return dict(diffs) |
Siobhan Tully | 7329134 | 2013-05-10 10:50:08 -0400 | [diff] [blame] | 26 | |
Sapan Bhatia | 541f953 | 2013-09-03 11:43:55 -0400 | [diff] [blame] | 27 | @property |
| 28 | def has_changed(self): |
| 29 | return bool(self.diff) |
Siobhan Tully | 7329134 | 2013-05-10 10:50:08 -0400 | [diff] [blame] | 30 | |
Sapan Bhatia | 541f953 | 2013-09-03 11:43:55 -0400 | [diff] [blame] | 31 | @property |
| 32 | def changed_fields(self): |
| 33 | return self.diff.keys() |
Siobhan Tully | 7329134 | 2013-05-10 10:50:08 -0400 | [diff] [blame] | 34 | |
Sapan Bhatia | 541f953 | 2013-09-03 11:43:55 -0400 | [diff] [blame] | 35 | def get_field_diff(self, field_name): |
| 36 | return self.diff.get(field_name, None) |
Siobhan Tully | 7329134 | 2013-05-10 10:50:08 -0400 | [diff] [blame] | 37 | |
Sapan Bhatia | dbaf193 | 2013-09-03 11:28:52 -0400 | [diff] [blame] | 38 | def delete(self, *args, **kwds): |
Sapan Bhatia | 541f953 | 2013-09-03 11:43:55 -0400 | [diff] [blame] | 39 | super(PlCoreBase, self).delete(*args, **kwds) |
Sapan Bhatia | dbaf193 | 2013-09-03 11:28:52 -0400 | [diff] [blame] | 40 | |
| 41 | EventSender().fire({'delete_flag':True,'model':self.__name__}) |
| 42 | |
Sapan Bhatia | 541f953 | 2013-09-03 11:43:55 -0400 | [diff] [blame] | 43 | def save(self, *args, **kwargs): |
| 44 | super(PlCoreBase, self).save(*args, **kwargs) |
| 45 | |
| 46 | # Tell the observer that the source database has been updated |
| 47 | EventSender().fire() |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 48 | |
Sapan Bhatia | 541f953 | 2013-09-03 11:43:55 -0400 | [diff] [blame] | 49 | self.__initial = self._dict |
Scott Baker | 13acdd6 | 2013-05-08 17:42:56 -0700 | [diff] [blame] | 50 | |
Sapan Bhatia | 541f953 | 2013-09-03 11:43:55 -0400 | [diff] [blame] | 51 | @property |
| 52 | def _dict(self): |
| 53 | return model_to_dict(self, fields=[field.name for field in |
| 54 | self._meta.fields]) |
Scott Baker | 13acdd6 | 2013-05-08 17:42:56 -0700 | [diff] [blame] | 55 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 56 | |
| 57 | |