Add history function to view

This commit is contained in:
Edgar P. Burkhart 2023-04-19 18:59:37 +02:00
parent 5534a4352d
commit 917531aa97
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227

View file

@ -34,6 +34,27 @@ from .forms import (
from .models import Account, Category, Invoice, Snapshot, Transaction
def history(transaction_set):
_history = (
transaction_set.values(month=models.functions.TruncMonth("date"))
.annotate(
sum_p=models.Sum("value", filter=models.Q(value__gt=0)),
sum_m=models.Sum("value", filter=models.Q(value__lt=0)),
sum=models.Sum("value"),
)
.order_by("-month")
)
return {
"data": _history,
"max": max(
_history.aggregate(
max=models.Max("sum_p", default=0),
min=-models.Min("sum_m", default=0),
).values(),
),
}
class IndexView(LoginRequiredMixin, TemplateView):
template_name = "main/index.html"
@ -41,31 +62,13 @@ class IndexView(LoginRequiredMixin, TemplateView):
_max = 8
_transactions = Transaction.objects.filter(user=self.request.user)
_snapshots = Snapshot.objects.filter(user=self.request.user)
_history = (
_transactions.filter(category__budget=True)
.values(month=models.functions.TruncMonth("date"))
.annotate(
sum_p=models.Sum("value", filter=models.Q(value__gt=0)),
sum_m=models.Sum("value", filter=models.Q(value__lt=0)),
sum=models.Sum("value"),
)
.order_by("-month")
)
res = {
"accounts": Account.objects.filter(user=self.request.user),
"transactions": _transactions[:_max],
"categories": Category.objects.filter(user=self.request.user),
"snapshots": _snapshots[:_max],
"history": {
"data": _history,
"max": max(
_history.aggregate(
max=models.Max("sum_p"),
min=-models.Min("sum_m"),
).values(),
),
},
"history": history(_transactions.filter(category__budget=True)),
}
if _transactions.count() > _max:
res["transactions_url"] = reverse_lazy("transactions")
@ -194,31 +197,13 @@ class AccountUpdateView(NummiUpdateView):
"account_snapshots", args=(account.pk,)
)
_history = (
account.transaction_set.values(month=models.functions.TruncMonth("date"))
.annotate(
sum_p=models.Sum("value", filter=models.Q(value__gt=0)),
sum_m=models.Sum("value", filter=models.Q(value__lt=0)),
sum=models.Sum("value"),
)
.order_by("-month")
)
return data | {
"transactions": _transactions[:8],
"new_snapshot_url": reverse_lazy(
"snapshot", kwargs={"account": account.pk}
),
"snapshots": _snapshots[:8],
"history": {
"data": _history,
"max": max(
_history.aggregate(
max=models.Max("sum_p", default=0),
min=-models.Min("sum_m", default=0),
).values(),
),
},
"history": history(account.transaction_set),
}
@ -256,30 +241,13 @@ class CategoryUpdateView(NummiUpdateView):
def get_context_data(self, **kwargs):
data = super().get_context_data(**kwargs)
category = data["form"].instance
_history = (
category.transaction_set.values(month=models.functions.TruncMonth("date"))
.annotate(
sum_p=models.Sum("value", filter=models.Q(value__gt=0)),
sum_m=models.Sum("value", filter=models.Q(value__lt=0)),
sum=models.Sum("value"),
)
.order_by("-month")
)
return data | {
"transactions": category.transaction_set.all()[:8],
"transactions_url": reverse_lazy(
"category_transactions", args=(category.pk,)
),
"history": {
"data": _history,
"max": max(
_history.aggregate(
max=models.Max("sum_p", default=0),
min=-models.Min("sum_m", default=0),
).values(),
),
},
"history": history(category.transaction_set),
}