From 917531aa9757bc16014b54d7c693354ae2b77c5f Mon Sep 17 00:00:00 2001 From: "Edgar P. Burkhart" Date: Wed, 19 Apr 2023 18:59:37 +0200 Subject: [PATCH] Add history function to view --- nummi/main/views.py | 80 ++++++++++++++------------------------------- 1 file changed, 24 insertions(+), 56 deletions(-) diff --git a/nummi/main/views.py b/nummi/main/views.py index c81e778..806b473 100644 --- a/nummi/main/views.py +++ b/nummi/main/views.py @@ -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), }