Adapted dirtyfields in the data models.

This commit is contained in:
依瑪貓 2020-08-02 09:53:26 +08:00
parent 330b43c71b
commit 688e350017
2 changed files with 25 additions and 7 deletions

View File

@ -18,6 +18,7 @@
"""The data models of the accounting application. """The data models of the accounting application.
""" """
from dirtyfields import DirtyFieldsMixin
from django.conf import settings from django.conf import settings
from django.db import models from django.db import models
from django.urls import reverse 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 from mia_core.utils import get_multi_lingual_attr, set_multi_lingual_attr
class Account(models.Model): class Account(DirtyFieldsMixin, models.Model):
"""An account.""" """An account."""
sn = models.PositiveIntegerField(primary_key=True) sn = models.PositiveIntegerField(primary_key=True)
parent = models.ForeignKey( parent = models.ForeignKey(
@ -71,7 +72,7 @@ class Account(models.Model):
set_multi_lingual_attr(self, "title", value) set_multi_lingual_attr(self, "title", value)
class Transaction(models.Model): class Transaction(DirtyFieldsMixin, models.Model):
"""An accounting transaction.""" """An accounting transaction."""
sn = models.PositiveIntegerField(primary_key=True) sn = models.PositiveIntegerField(primary_key=True)
date = models.DateField() date = models.DateField()
@ -218,6 +219,23 @@ class Transaction(models.Model):
else: else:
return "transfer" 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): def get_absolute_url(self):
"""Returns the URL to view this transaction.""" """Returns the URL to view this transaction."""
if self.is_cash_expense: if self.is_cash_expense:
@ -231,7 +249,7 @@ class Transaction(models.Model):
"accounting:transactions.show", args=("transfer", self)) "accounting:transactions.show", args=("transfer", self))
class Record(models.Model): class Record(DirtyFieldsMixin, models.Model):
"""An accounting record.""" """An accounting record."""
sn = models.PositiveIntegerField(primary_key=True) sn = models.PositiveIntegerField(primary_key=True)
transaction = models.ForeignKey( transaction = models.ForeignKey(
@ -354,7 +372,7 @@ class Record(models.Model):
self._is_existing_equipment = value self._is_existing_equipment = value
class RecordSummary(models.Model): class RecordSummary(DirtyFieldsMixin, models.Model):
"""A summary record.""" """A summary record."""
month = models.DateField(primary_key=True) month = models.DateField(primary_key=True)
credit = models.PositiveIntegerField() credit = models.PositiveIntegerField()

View File

@ -18,13 +18,13 @@
"""The data models of the Mia core application. """The data models of the Mia core application.
""" """
from dirtyfields import DirtyFieldsMixin
from django.db import models from django.db import models
from mia_core.utils import get_multi_lingual_attr, set_multi_lingual_attr from mia_core.utils import get_multi_lingual_attr, set_multi_lingual_attr
class Country(models.Model): class Country(DirtyFieldsMixin, models.Model):
"""A country.""" """A country."""
sn = models.PositiveIntegerField(primary_key=True) sn = models.PositiveIntegerField(primary_key=True)
code = models.CharField(max_length=2, unique=True) code = models.CharField(max_length=2, unique=True)
@ -62,7 +62,7 @@ class Country(models.Model):
db_table = "countries" db_table = "countries"
class User(models.Model): class User(DirtyFieldsMixin, models.Model):
"""A user.""" """A user."""
sn = models.PositiveIntegerField(primary_key=True) sn = models.PositiveIntegerField(primary_key=True)
login_id = models.CharField(max_length=32, unique=True, db_column="id") login_id = models.CharField(max_length=32, unique=True, db_column="id")