Move category plot to templatetag, hide non-budget categories

This commit is contained in:
Edgar P. Burkhart 2023-05-03 12:01:25 +02:00
parent 819bd20fdf
commit b9e129e80a
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227
7 changed files with 23 additions and 26 deletions

View file

@ -18,7 +18,7 @@
</thead> </thead>
<tbody> <tbody>
{% spaceless %} {% spaceless %}
{% for cat in categories.data %} {% for cat in categories %}
<tr> <tr>
<th scope="row" class="l"> <th scope="row" class="l">
{% if cat.category %} {% if cat.category %}
@ -34,25 +34,27 @@
</td> </td>
<td class="value">{{ cat.sum_m|pmrvalue }}</td> <td class="value">{{ cat.sum_m|pmrvalue }}</td>
<td class="bar m"> <td class="bar m">
<div style="width: {% widthratio cat.sum_m categories.max -100 %}%"></div> <div style="width: {% widthratio cat.sum_m max -100 %}%"></div>
{% if cat.sum < 0 %} {% if cat.sum < 0 %}
<div class="tot" <div class="tot" style="width:{% widthratio cat.sum max -100 %}%">
style="width:{% widthratio cat.sum categories.max -100 %}%">
<span>{{ cat.sum|pmrvalue }}</span> <span>{{ cat.sum|pmrvalue }}</span>
</div> </div>
{% endif %} {% endif %}
</td> </td>
<td class="bar p"> <td class="bar p">
<div style="width: {% widthratio cat.sum_p categories.max 100 %}%"></div> <div style="width: {% widthratio cat.sum_p max 100 %}%"></div>
{% if cat.sum > 0 %} {% if cat.sum > 0 %}
<div class="tot" <div class="tot" style="width:{% widthratio cat.sum max 100 %}%">
style="width:{% widthratio cat.sum categories.max 100 %}%">
<span>{{ cat.sum|pmrvalue }}</span> <span>{{ cat.sum|pmrvalue }}</span>
</div> </div>
{% endif %} {% endif %}
</td> </td>
<td class="value">{{ cat.sum_p|pmrvalue }}</td> <td class="value">{{ cat.sum_p|pmrvalue }}</td>
</tr> </tr>
{% empty %}
<tr>
<td class="empty" colspan="6">{% translate "No transaction" %}</td>
</tr>
{% endfor %} {% endfor %}
{% endspaceless %} {% endspaceless %}
</tbody> </tbody>

View file

View file

@ -1,9 +1,14 @@
from django import template
from django.db import models from django.db import models
register = template.Library()
def get_categories(transactions):
@register.inclusion_tag("category/category_plot.html")
def category_plot(transactions):
categories = ( categories = (
transactions.values("category", "category__name", "category__icon") transactions.filter(category__budget=True)
.values("category", "category__name", "category__icon")
.annotate( .annotate(
sum=models.Sum("value"), sum=models.Sum("value"),
sum_m=models.Sum("value", filter=models.Q(value__lt=0)), sum_m=models.Sum("value", filter=models.Q(value__lt=0)),
@ -12,7 +17,7 @@ def get_categories(transactions):
.order_by("-sum") .order_by("-sum")
) )
return { return {
"data": categories, "categories": categories,
"max": max( "max": max(
categories.aggregate( categories.aggregate(
max=models.Max("sum_p", default=0), max=models.Max("sum_p", default=0),

View file

@ -1,6 +1,5 @@
{% extends "main/form/base.html" %} {% extends "main/form/base.html" %}
{% load main_extras %} {% load i18n main_extras category %}
{% load i18n %}
{% block title_new %} {% block title_new %}
{% translate "Create statement" %} {% translate "Create statement" %}
{% endblock %} {% endblock %}
@ -19,11 +18,9 @@
{% endif %} {% endif %}
{% endblock %} {% endblock %}
{% block tables %} {% block tables %}
{% if categories %}
<h3>{% translate "Categories" %}</h3>
{% include "category/category_plot.html" %}
{% endif %}
{% if not form.instance|adding %} {% if not form.instance|adding %}
<h3>{% translate "Categories" %}</h3>
{% category_plot transactions %}
<h3>{% translate "Transactions" %} ({{ form.instance.sum|pmvalue }} / {{ form.instance.diff|pmvalue }})</h3> <h3>{% translate "Transactions" %} ({{ form.instance.sum|pmvalue }} / {{ form.instance.diff|pmvalue }})</h3>
{% include "transaction/transaction_table.html" %} {% include "transaction/transaction_table.html" %}
{% endif %} {% endif %}

View file

@ -1,5 +1,4 @@
from account.models import Account from account.models import Account
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
@ -42,8 +41,6 @@ class StatementUpdateView(NummiUpdateView):
statement = data["form"].instance statement = data["form"].instance
_transactions = statement.transaction_set.all() _transactions = statement.transaction_set.all()
if _transactions:
data["categories"] = get_categories(_transactions)
return data | { return data | {
"account": statement.account, "account": statement.account,

View file

@ -1,6 +1,5 @@
{% extends "transaction/transaction_list.html" %} {% extends "transaction/transaction_list.html" %}
{% load i18n %} {% load i18n static category %}
{% load static %}
{% block link %} {% block link %}
{{ block.super }} {{ block.super }}
<link rel="stylesheet" <link rel="stylesheet"
@ -11,8 +10,8 @@
{% block table %} {% block table %}
<h3>{% translate "Transactions" %}</h3> <h3>{% translate "Transactions" %}</h3>
{{ block.super }} {{ block.super }}
{% if categories %} {% if not category %}
<h3>{% translate "Categories" %}</h3> <h3>{% translate "Categories" %}</h3>
{% include "category/category_plot.html" %} {% category_plot transactions %}
{% endif %} {% endif %}
{% endblock %} {% endblock %}

View file

@ -1,6 +1,5 @@
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
@ -136,8 +135,6 @@ class TransactionMonthView(UserMixin, MonthArchiveView):
context_data = super().get_context_data(**kwargs) context_data = super().get_context_data(**kwargs)
if "category" in self.kwargs: if "category" in self.kwargs:
return context_data | {"category": self.category} return context_data | {"category": self.category}
context_data["categories"] = get_categories(context_data["transactions"])
if "account" in self.kwargs: if "account" in self.kwargs:
return context_data | {"account": self.account} return context_data | {"account": self.account}
return context_data return context_data