only save numberCores to OS metadata if it changes
[plstackapi.git] / plstackapi / core / models / plcorebase.py
1 import os
2 from django.db import models
3 from django.forms.models import model_to_dict
4
5 class PlCoreBase(models.Model):
6
7     created = models.DateTimeField(auto_now_add=True)
8     updated = models.DateTimeField(auto_now=True)
9
10     class Meta:
11         abstract = True
12         app_label = "core"
13
14     def __init__(self, *args, **kwargs):
15         super(PlCoreBase, self).__init__(*args, **kwargs)
16         self.__initial = self._dict
17
18     @property
19     def diff(self):\r
20         d1 = self.__initial\r
21         d2 = self._dict\r
22         diffs = [(k, (v, d2[k])) for k, v in d1.items() if v != d2[k]]\r
23         return dict(diffs)\r
24 \r
25     @property\r
26     def has_changed(self):\r
27         return bool(self.diff)\r
28 \r
29     @property\r
30     def changed_fields(self):\r
31         return self.diff.keys()\r
32 \r
33     def get_field_diff(self, field_name):\r
34         return self.diff.get(field_name, None)\r
35 \r
36     def save(self, *args, **kwargs):\r
37         super(PlCoreBase, self).save(*args, **kwargs)\r
38         self.__initial = self._dict
39
40     @property
41     def _dict(self):\r
42         return model_to_dict(self, fields=[field.name for field in\r
43                              self._meta.fields])
44
45
46