From 229033aac0d80774bf56d80c6e757dd56000869c Mon Sep 17 00:00:00 2001 From: "Edgar P. Burkhart" Date: Thu, 2 Jan 2025 15:03:10 +0100 Subject: [PATCH] Use templatetag for history_plot --- nummi/account/models.py | 7 ++++++ .../templates/account/account_detail.html | 18 +++++++------- nummi/account/views.py | 11 +-------- .../templates/category/category_detail.html | 24 +++++++++---------- nummi/category/views.py | 6 ++--- nummi/history/templatetags/history_extras.py | 8 +++++++ nummi/main/templates/main/index.html | 12 ++++------ nummi/main/views.py | 20 +++++----------- 8 files changed, 49 insertions(+), 57 deletions(-) diff --git a/nummi/account/models.py b/nummi/account/models.py index 5321ad5..16aca17 100644 --- a/nummi/account/models.py +++ b/nummi/account/models.py @@ -1,5 +1,6 @@ from uuid import uuid4 +from django.apps import apps from django.db import models from django.urls import reverse from django.utils.translation import gettext_lazy as _ @@ -33,6 +34,12 @@ class Account(UserModel): def get_delete_url(self): return reverse("del_account", args=(self.pk,)) + @property + def transactions(self): + return apps.get_model("transaction", "Transaction").objects.filter( + statement__account=self + ) + class Meta: ordering = ["-default", "name"] verbose_name = _("Account") diff --git a/nummi/account/templates/account/account_detail.html b/nummi/account/templates/account/account_detail.html index a4b6676..5b7fa17 100644 --- a/nummi/account/templates/account/account_detail.html +++ b/nummi/account/templates/account/account_detail.html @@ -1,8 +1,8 @@ {% extends "main/base.html" %} -{% load main_extras statement_extras %} +{% load main_extras history_extras statement_extras %} {% load i18n %} {% block title %} - {{ object }} – {{ block.super }} + {{ account }} – {{ block.super }} {% endblock title %} {% block link %} {{ block.super }} @@ -10,9 +10,9 @@ {% css "main/css/plot.css" %} {% endblock link %} {% block body %} -

{{ object.icon|remix }}{{ object }}

+

{{ account.icon|remix }}{{ account }}

- {{ "edit"|remix }}{% translate "Edit account" %} + {{ "edit"|remix }}{% translate "Edit account" %}

{% translate "Statements" %}

@@ -20,10 +20,8 @@ {% url "account_statements" account=account.pk as s_url %} {% statement_table account.statement_set.all statements_url=s_url new_statement_url=ns_url n_max=6 %}
- {% if history %} -
-

{% translate "History" %}

- {% include "history/plot.html" %} -
- {% endif %} +
+

{% translate "History" %}

+ {% history_plot account.transactions %} +
{% endblock body %} diff --git a/nummi/account/views.py b/nummi/account/views.py index bf63ae1..174a228 100644 --- a/nummi/account/views.py +++ b/nummi/account/views.py @@ -1,5 +1,4 @@ from django.shortcuts import get_object_or_404 -from history.utils import history from main.views import ( NummiCreateView, NummiDeleteView, @@ -8,7 +7,6 @@ from main.views import ( NummiUpdateView, ) from statement.views import StatementListView -from transaction.models import Transaction from transaction.views import TransactionListView from .forms import AccountForm @@ -34,14 +32,7 @@ class AccountDeleteView(NummiDeleteView): class AccountDetailView(NummiDetailView): model = Account pk_url_kwarg = "account" - - def get_context_data(self, **kwargs): - data = super().get_context_data(**kwargs) - account = data.get("object") - - return data | { - "history": history(Transaction.objects.filter(statement__account=account)), - } + context_object_name = "account" class AccountMixin: diff --git a/nummi/category/templates/category/category_detail.html b/nummi/category/templates/category/category_detail.html index 7380893..d2e4674 100644 --- a/nummi/category/templates/category/category_detail.html +++ b/nummi/category/templates/category/category_detail.html @@ -1,24 +1,24 @@ {% extends "main/base.html" %} -{% load main_extras i18n %} -{% block title %}{{ object }} – {{ block.super }}{% endblock %} +{% load i18n main_extras history_extras %} +{% block title %} + {{ category }} – {{ block.super }} +{% endblock title %} {% block link %} {{ block.super }} {% css "main/css/table.css" %} {% css "main/css/plot.css" %} -{% endblock %} +{% endblock link %} {% block body %} -

{{ object.icon|remix }}{{ object }}

+

{{ category.icon|remix }}{{ category }}

- {{ "edit"|remix }}{% translate "Edit category" %} + {{ "edit"|remix }}{% translate "Edit category" %}

{% translate "Transactions" %}

{% include "transaction/transaction_table.html" %}
- {% if history %} -
-

{% translate "History" %}

- {% include "history/plot.html" %} -
- {% endif %} -{% endblock %} +
+

{% translate "History" %}

+ {% history_plot category.transaction_set.all %} +
+{% endblock body %} diff --git a/nummi/category/views.py b/nummi/category/views.py index f01d908..c34a422 100644 --- a/nummi/category/views.py +++ b/nummi/category/views.py @@ -1,6 +1,5 @@ from django.shortcuts import get_object_or_404 from django.urls import reverse_lazy -from history.utils import history from main.views import ( NummiCreateView, NummiDeleteView, @@ -27,6 +26,7 @@ class CategoryUpdateView(NummiUpdateView): class CategoryDetailView(NummiDetailView): model = Category pk_url_kwarg = "category" + context_object_name = "category" def get_context_data(self, **kwargs): _max = 8 @@ -39,9 +39,7 @@ class CategoryDetailView(NummiDetailView): "category_transactions", args=(category.pk,) ) - return data | { - "history": history(category.transaction_set), - } + return data class CategoryDeleteView(NummiDeleteView): diff --git a/nummi/history/templatetags/history_extras.py b/nummi/history/templatetags/history_extras.py index 53b7b82..d7cd2c1 100644 --- a/nummi/history/templatetags/history_extras.py +++ b/nummi/history/templatetags/history_extras.py @@ -2,11 +2,19 @@ import math from django import template from django.utils.safestring import mark_safe +from history.utils import history from main.templatetags.main_extras import pmrvalue, remix register = template.Library() +@register.inclusion_tag("history/plot.html") +def history_plot(transactions, **kwargs): + return kwargs | { + "history": history(transactions), + } + + @register.simple_tag def calendar_opacity(v, vmax): if v is None: diff --git a/nummi/main/templates/main/index.html b/nummi/main/templates/main/index.html index fd57b90..ce81ed3 100644 --- a/nummi/main/templates/main/index.html +++ b/nummi/main/templates/main/index.html @@ -1,6 +1,6 @@ {% extends "main/base.html" %} {% load static %} -{% load main_extras account_extras statement_extras %} +{% load main_extras account_extras history_extras statement_extras %} {% load i18n %} {% block link %} {{ block.super }} @@ -31,10 +31,8 @@

{% endspaceless %} - {% if history %} -
-

{% translate "History" %}

- {% include "history/plot.html" %} -
- {% endif %} +
+

{% translate "History" %}

+ {% history_plot user.transaction_set.all %} +
{% endblock body %} diff --git a/nummi/main/views.py b/nummi/main/views.py index df08d7c..0b569d9 100644 --- a/nummi/main/views.py +++ b/nummi/main/views.py @@ -12,28 +12,20 @@ from django.views.generic import ( TemplateView, UpdateView, ) -from history.utils import history class IndexView(LoginRequiredMixin, TemplateView): template_name = "main/index.html" def get_context_data(self, **kwargs): - _user = self.request.user - _transactions = _user.transaction_set.all() - _statements = ( - _user.statement_set.exclude(account__archived=True) - .order_by("account__id", "-date") - .distinct("account__id") - ) - - res = { - "statements": _statements, - "history": history(_transactions.exclude(category__budget=False)), + return super().get_context_data(**kwargs) | { + "statements": ( + self.request.user.statement_set.exclude(account__archived=True) + .order_by("account__id", "-date") + .distinct("account__id") + ) } - return super().get_context_data(**kwargs) | res - class UserMixin(LoginRequiredMixin): def get_queryset(self, **kwargs):