From 688e3500177f8e99a8aee4603872551d80f8b190 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=9D=E7=91=AA=E8=B2=93?= Date: Sun, 2 Aug 2020 09:53:26 +0800 Subject: [PATCH] Adapted dirtyfields in the data models. --- accounting/models.py | 26 ++++++++++++++++++++++---- mia_core/models.py | 6 +++--- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/accounting/models.py b/accounting/models.py index 1a85869..0ad4077 100644 --- a/accounting/models.py +++ b/accounting/models.py @@ -18,6 +18,7 @@ """The data models of the accounting application. """ +from dirtyfields import DirtyFieldsMixin from django.conf import settings from django.db import models from django.urls import reverse @@ -26,7 +27,7 @@ from mia_core.templatetags.mia_core import smart_month from mia_core.utils import get_multi_lingual_attr, set_multi_lingual_attr -class Account(models.Model): +class Account(DirtyFieldsMixin, models.Model): """An account.""" sn = models.PositiveIntegerField(primary_key=True) parent = models.ForeignKey( @@ -71,7 +72,7 @@ class Account(models.Model): set_multi_lingual_attr(self, "title", value) -class Transaction(models.Model): +class Transaction(DirtyFieldsMixin, models.Model): """An accounting transaction.""" sn = models.PositiveIntegerField(primary_key=True) date = models.DateField() @@ -218,6 +219,23 @@ class Transaction(models.Model): else: return "transfer" + def is_dirty(self, **kwargs): + """Returns whether the data of this transaction is changed and need + to be saved into the database. + + Returns: + bool: True if the data of this transaction is changed and need + to be saved into the database, or False otherwise. + """ + if super(Transaction, self).is_dirty(**kwargs): + return True + if len([x for x in self.records if x.is_dirty(**kwargs)]) > 0: + return True + kept = [x.pk for x in self.records] + if len([x for x in self.record_set.all() if x.pk not in kept]) > 0: + return True + return False + def get_absolute_url(self): """Returns the URL to view this transaction.""" if self.is_cash_expense: @@ -231,7 +249,7 @@ class Transaction(models.Model): "accounting:transactions.show", args=("transfer", self)) -class Record(models.Model): +class Record(DirtyFieldsMixin, models.Model): """An accounting record.""" sn = models.PositiveIntegerField(primary_key=True) transaction = models.ForeignKey( @@ -354,7 +372,7 @@ class Record(models.Model): self._is_existing_equipment = value -class RecordSummary(models.Model): +class RecordSummary(DirtyFieldsMixin, models.Model): """A summary record.""" month = models.DateField(primary_key=True) credit = models.PositiveIntegerField() diff --git a/mia_core/models.py b/mia_core/models.py index fdd8d64..e2a4a57 100644 --- a/mia_core/models.py +++ b/mia_core/models.py @@ -18,13 +18,13 @@ """The data models of the Mia core application. """ - +from dirtyfields import DirtyFieldsMixin from django.db import models from mia_core.utils import get_multi_lingual_attr, set_multi_lingual_attr -class Country(models.Model): +class Country(DirtyFieldsMixin, models.Model): """A country.""" sn = models.PositiveIntegerField(primary_key=True) code = models.CharField(max_length=2, unique=True) @@ -62,7 +62,7 @@ class Country(models.Model): db_table = "countries" -class User(models.Model): +class User(DirtyFieldsMixin, models.Model): """A user.""" sn = models.PositiveIntegerField(primary_key=True) login_id = models.CharField(max_length=32, unique=True, db_column="id")