Move statement sum and diff to properties instead of model fields
Fix #32
This commit is contained in:
parent
26f97dd179
commit
a6fc7f5a92
2 changed files with 29 additions and 27 deletions
|
@ -0,0 +1,20 @@
|
||||||
|
# Generated by Django 4.2 on 2025-01-03 13:23
|
||||||
|
|
||||||
|
from django.db import migrations
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
dependencies = [
|
||||||
|
("statement", "0002_alter_statement_table"),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.RemoveField(
|
||||||
|
model_name="statement",
|
||||||
|
name="diff",
|
||||||
|
),
|
||||||
|
migrations.RemoveField(
|
||||||
|
model_name="statement",
|
||||||
|
name="sum",
|
||||||
|
),
|
||||||
|
]
|
|
@ -4,7 +4,7 @@ from uuid import uuid4
|
||||||
|
|
||||||
from account.models import AccountModel
|
from account.models import AccountModel
|
||||||
from django.core.validators import FileExtensionValidator
|
from django.core.validators import FileExtensionValidator
|
||||||
from django.db import models, transaction
|
from django.db import models
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
from media.utils import get_path
|
from media.utils import get_path
|
||||||
|
@ -22,20 +22,6 @@ class Statement(AccountModel):
|
||||||
start_value = models.DecimalField(
|
start_value = models.DecimalField(
|
||||||
max_digits=12, decimal_places=2, default=0, verbose_name=_("Start value")
|
max_digits=12, decimal_places=2, default=0, verbose_name=_("Start value")
|
||||||
)
|
)
|
||||||
diff = models.DecimalField(
|
|
||||||
max_digits=12,
|
|
||||||
decimal_places=2,
|
|
||||||
default=0,
|
|
||||||
verbose_name=_("Difference"),
|
|
||||||
editable=False,
|
|
||||||
)
|
|
||||||
sum = models.DecimalField(
|
|
||||||
max_digits=12,
|
|
||||||
decimal_places=2,
|
|
||||||
default=0,
|
|
||||||
verbose_name=_("Transaction difference"),
|
|
||||||
editable=False,
|
|
||||||
)
|
|
||||||
file = models.FileField(
|
file = models.FileField(
|
||||||
upload_to=get_path,
|
upload_to=get_path,
|
||||||
validators=[FileExtensionValidator(["pdf"])],
|
validators=[FileExtensionValidator(["pdf"])],
|
||||||
|
@ -54,21 +40,17 @@ class Statement(AccountModel):
|
||||||
if _prever.file and _prever.file != self.file:
|
if _prever.file and _prever.file != self.file:
|
||||||
Path(_prever.file.path).unlink(missing_ok=True)
|
Path(_prever.file.path).unlink(missing_ok=True)
|
||||||
|
|
||||||
with transaction.atomic():
|
|
||||||
for trans in self.transaction_set.all():
|
|
||||||
trans.save()
|
|
||||||
|
|
||||||
self.diff = self.value - self.start_value
|
|
||||||
self.sum = (
|
|
||||||
self.transaction_set.aggregate(sum=models.Sum("value")).get("sum", 0) or 0
|
|
||||||
)
|
|
||||||
super().save(*args, **kwargs)
|
super().save(*args, **kwargs)
|
||||||
|
|
||||||
def update_sum(self):
|
@property
|
||||||
self.sum = (
|
def sum(self):
|
||||||
self.transaction_set.aggregate(sum=models.Sum("value")).get("sum", 0) or 0
|
return self.transaction_set.aggregate(sum=models.Sum("value", default=0)).get(
|
||||||
|
"sum"
|
||||||
)
|
)
|
||||||
super().save()
|
|
||||||
|
@property
|
||||||
|
def diff(self):
|
||||||
|
return self.value - self.start_value
|
||||||
|
|
||||||
def delete(self, *args, **kwargs):
|
def delete(self, *args, **kwargs):
|
||||||
if self.file:
|
if self.file:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue