Add category plot on history pages

This commit is contained in:
Edgar P. Burkhart 2023-04-22 15:44:27 +02:00
parent 681651109a
commit b828324220
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227
6 changed files with 50 additions and 25 deletions

View file

@ -20,7 +20,7 @@
{% spaceless %} {% spaceless %}
{% for cat in categories.data %} {% for cat in categories.data %}
<tr> <tr>
<th scope="row"> <th scope="row" class="l">
{% if cat.category %}{{ cat.category__name }}{% endif %} {% if cat.category %}{{ cat.category__name }}{% endif %}
</th> </th>
<td class="c"> <td class="c">

22
nummi/category/utils.py Normal file
View file

@ -0,0 +1,22 @@
from django.db import models
def get_categories(transactions):
categories = (
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")
)
return {
"data": categories,
"max": max(
categories.aggregate(
max=models.Max("sum_p", default=0),
min=models.Min("sum_m", default=0),
).values(),
),
}

View file

@ -163,8 +163,10 @@ footer {
a.big-link { a.big-link {
margin-right: 1em; margin-right: 1em;
} }
[class^="ri-"] { a [class^="ri-"] {
margin-right: 0.5em; margin-right: 0.5em;
}
[class^="ri-"] {
font-weight: normal; font-weight: normal;
} }

View file

@ -1,5 +1,5 @@
from account.models import Account from account.models import Account
from django.db import models from category.utils import get_categories
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
from django.urls import reverse_lazy from django.urls import reverse_lazy
from main.views import NummiCreateView, NummiDeleteView, NummiListView, NummiUpdateView from main.views import NummiCreateView, NummiDeleteView, NummiListView, NummiUpdateView
@ -43,24 +43,7 @@ class StatementUpdateView(NummiUpdateView):
_transactions = statement.transaction_set.all() _transactions = statement.transaction_set.all()
if _transactions: if _transactions:
_categories = ( data["categories"] = get_categories(_transactions)
_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")
)
data["categories"] = {
"data": _categories,
"max": max(
_categories.aggregate(
max=models.Max("sum_p", default=0),
min=models.Min("sum_m", default=0),
).values(),
),
}
return data | { return data | {
"account": statement.account, "account": statement.account,

View file

@ -1,2 +1,16 @@
{% extends "transaction/transaction_list.html" %} {% extends "transaction/transaction_list.html" %}
{% load i18n %}
{% load static %}
{% block link %}
{{ block.super }}
<link rel="stylesheet"
href="{% static 'main/css/plot.css' %}"
type="text/css" />
{% endblock %}
{% block h2 %}{{ month|date:"F Y"|capfirst }}{% endblock %} {% block h2 %}{{ month|date:"F Y"|capfirst }}{% endblock %}
{% block table %}
<h3>{% translate "Transactions" %}</h3>
{{ block.super }}
<h3>{% translate "History" %}</h3>
{% include "category/category_plot.html" %}
{% endblock %}

View file

@ -1,5 +1,6 @@
from account.models import Account from account.models import Account
from category.models import Category from category.models import Category
from category.utils import get_categories
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
from django.urls import reverse_lazy from django.urls import reverse_lazy
from django.views.generic.dates import MonthArchiveView from django.views.generic.dates import MonthArchiveView
@ -132,8 +133,11 @@ class TransactionMonthView(UserMixin, MonthArchiveView):
return super().get_queryset() return super().get_queryset()
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
if "account" in self.kwargs: context_data = super().get_context_data(**kwargs)
return super().get_context_data(**kwargs) | {"account": self.account}
if "category" in self.kwargs: if "category" in self.kwargs:
return super().get_context_data(**kwargs) | {"category": self.category} return context_data | {"category": self.category}
return super().get_context_data(**kwargs)
context_data["categories"] = get_categories(context_data["transactions"])
if "account" in self.kwargs:
return context_data | {"account": self.account}
return context_data