Fix updating of snapshot values
This commit is contained in:
parent
0184cba30d
commit
411d258cd7
4 changed files with 22 additions and 23 deletions
|
@ -71,15 +71,5 @@ class SnapshotForm(NummiForm):
|
||||||
id__in=self.cleaned_data["transactions"]
|
id__in=self.cleaned_data["transactions"]
|
||||||
)
|
)
|
||||||
|
|
||||||
to_update = list(
|
instance.transaction_set.add(*new_transactions, bulk=False)
|
||||||
new_transactions.order_by().only("snapshot").distinct("snapshot")
|
|
||||||
)
|
|
||||||
|
|
||||||
instance.account.transaction_set.add(*new_transactions)
|
|
||||||
instance.transaction_set.add(*new_transactions)
|
|
||||||
instance.update_sum()
|
|
||||||
|
|
||||||
for upd in to_update:
|
|
||||||
upd.snapshot.update_sum()
|
|
||||||
|
|
||||||
return instance
|
return instance
|
||||||
|
|
|
@ -159,10 +159,15 @@ class Snapshot(AccountModel):
|
||||||
trans.save()
|
trans.save()
|
||||||
|
|
||||||
self.diff = self.value - self.start_value
|
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):
|
def update_sum(self):
|
||||||
self.sum = self.transaction_set.aggregate(sum=models.Sum("value")).get("sum", 0)
|
self.sum = (
|
||||||
|
self.transaction_set.aggregate(sum=models.Sum("value")).get("sum", 0) or 0
|
||||||
|
)
|
||||||
super().save()
|
super().save()
|
||||||
|
|
||||||
def delete(self, *args, **kwargs):
|
def delete(self, *args, **kwargs):
|
||||||
|
@ -219,9 +224,13 @@ class Transaction(CustomModel):
|
||||||
)
|
)
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
|
if Transaction.objects.filter(pk=self.pk):
|
||||||
prev_self = Transaction.objects.get(pk=self.pk)
|
prev_self = Transaction.objects.get(pk=self.pk)
|
||||||
|
else:
|
||||||
|
prev_self = None
|
||||||
self.account = self.snapshot.account
|
self.account = self.snapshot.account
|
||||||
super().save(*args, **kwargs)
|
super().save(*args, **kwargs)
|
||||||
|
if prev_self is not None:
|
||||||
prev_self.snapshot.update_sum()
|
prev_self.snapshot.update_sum()
|
||||||
self.snapshot.update_sum()
|
self.snapshot.update_sum()
|
||||||
|
|
||||||
|
|
|
@ -71,9 +71,9 @@
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</div>
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% if snapshot.transactions %}
|
{% if snapshot.transaction_set.all %}
|
||||||
<h2>{% translate "Transactions" %} ({{ snapshot.sum|pmvalue }} / {{ snapshot.diff|pmvalue }})</h2>
|
<h2>{% translate "Transactions" %} ({{ snapshot.sum|pmvalue }} / {{ snapshot.diff|pmvalue }})</h2>
|
||||||
{% transaction_table snapshot.transactions %}
|
{% transaction_table snapshot.transaction_set.all %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endwith %}
|
{% endwith %}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
Loading…
Reference in a new issue