From 63c7ab497eba67b1ca3ee37442340b82d1298849 Mon Sep 17 00:00:00 2001 From: "Edgar P. Burkhart" Date: Tue, 24 May 2022 20:46:05 +0200 Subject: [PATCH] Fix add snapshot page --- nummi/main/templates/main/snapshot.html | 2 ++ nummi/main/views.py | 39 +++++++++++++------------ 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/nummi/main/templates/main/snapshot.html b/nummi/main/templates/main/snapshot.html index 0318343..4850ddd 100644 --- a/nummi/main/templates/main/snapshot.html +++ b/nummi/main/templates/main/snapshot.html @@ -32,6 +32,7 @@ +{% if categories %}

Plot

{% for cat in categories %} @@ -83,6 +84,7 @@
{% endfor %} +{% endif %} {% if snapshot.transactions %}

Transactions ({{ sum|pmvalue }} / {{ snapshot.diff|pmvalue }})

diff --git a/nummi/main/views.py b/nummi/main/views.py index e8ca412..c239e65 100644 --- a/nummi/main/views.py +++ b/nummi/main/views.py @@ -163,27 +163,28 @@ def snapshot(request, uuid=None): "form": _form, } - context["categories"] = ( - _snapshot.transactions.values("category", "category__name", "category__icon") - .annotate( - sum=models.Sum("value"), - sum_m=models.Sum("value", filter=models.Q(value__lt=0)), - sum_p=models.Sum("value", filter=models.Q(value__gt=0)), - ) - .order_by("-sum") - ) - context["cat_lim"] = max( - map( - abs, - context["categories"] - .aggregate( - max=models.Max("sum_p"), - min=models.Min("sum_m"), + if _snapshot.transactions: + context["categories"] = ( + _snapshot.transactions.values("category", "category__name", "category__icon") + .annotate( + sum=models.Sum("value"), + sum_m=models.Sum("value", filter=models.Q(value__lt=0)), + sum_p=models.Sum("value", filter=models.Q(value__gt=0)), ) - .values(), + .order_by("-sum") ) - ) - context["cat_lim_m"] = -context["cat_lim"] + context["cat_lim"] = max( + map( + abs, + context["categories"] + .aggregate( + max=models.Max("sum_p"), + min=models.Min("sum_m"), + ) + .values(), + ) + ) + context["cat_lim_m"] = -context["cat_lim"] return render( request, "main/snapshot.html",