Use templatetag for history_plot

This commit is contained in:
Edgar P. Burkhart 2025-01-02 15:03:10 +01:00
parent b1fddd0dd6
commit 229033aac0
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227
8 changed files with 49 additions and 57 deletions

View file

@ -1,5 +1,6 @@
from uuid import uuid4 from uuid import uuid4
from django.apps import apps
from django.db import models from django.db import models
from django.urls import reverse from django.urls import reverse
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
@ -33,6 +34,12 @@ class Account(UserModel):
def get_delete_url(self): def get_delete_url(self):
return reverse("del_account", args=(self.pk,)) return reverse("del_account", args=(self.pk,))
@property
def transactions(self):
return apps.get_model("transaction", "Transaction").objects.filter(
statement__account=self
)
class Meta: class Meta:
ordering = ["-default", "name"] ordering = ["-default", "name"]
verbose_name = _("Account") verbose_name = _("Account")

View file

@ -1,8 +1,8 @@
{% extends "main/base.html" %} {% extends "main/base.html" %}
{% load main_extras statement_extras %} {% load main_extras history_extras statement_extras %}
{% load i18n %} {% load i18n %}
{% block title %} {% block title %}
{{ object }} {{ block.super }} {{ account }} {{ block.super }}
{% endblock title %} {% endblock title %}
{% block link %} {% block link %}
{{ block.super }} {{ block.super }}
@ -10,9 +10,9 @@
{% css "main/css/plot.css" %} {% css "main/css/plot.css" %}
{% endblock link %} {% endblock link %}
{% block body %} {% block body %}
<h2>{{ object.icon|remix }}{{ object }}</h2> <h2>{{ account.icon|remix }}{{ account }}</h2>
<p> <p>
<a href="{% url "edit_account" object.pk %}">{{ "edit"|remix }}{% translate "Edit account" %}</a> <a href="{% url "edit_account" account.pk %}">{{ "edit"|remix }}{% translate "Edit account" %}</a>
</p> </p>
<section> <section>
<h3>{% translate "Statements" %}</h3> <h3>{% translate "Statements" %}</h3>
@ -20,10 +20,8 @@
{% url "account_statements" account=account.pk as s_url %} {% 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 %} {% statement_table account.statement_set.all statements_url=s_url new_statement_url=ns_url n_max=6 %}
</section> </section>
{% if history %} <section>
<section> <h3>{% translate "History" %}</h3>
<h3>{% translate "History" %}</h3> {% history_plot account.transactions %}
{% include "history/plot.html" %} </section>
</section>
{% endif %}
{% endblock body %} {% endblock body %}

View file

@ -1,5 +1,4 @@
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
from history.utils import history
from main.views import ( from main.views import (
NummiCreateView, NummiCreateView,
NummiDeleteView, NummiDeleteView,
@ -8,7 +7,6 @@ from main.views import (
NummiUpdateView, NummiUpdateView,
) )
from statement.views import StatementListView from statement.views import StatementListView
from transaction.models import Transaction
from transaction.views import TransactionListView from transaction.views import TransactionListView
from .forms import AccountForm from .forms import AccountForm
@ -34,14 +32,7 @@ class AccountDeleteView(NummiDeleteView):
class AccountDetailView(NummiDetailView): class AccountDetailView(NummiDetailView):
model = Account model = Account
pk_url_kwarg = "account" pk_url_kwarg = "account"
context_object_name = "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)),
}
class AccountMixin: class AccountMixin:

View file

@ -1,24 +1,24 @@
{% extends "main/base.html" %} {% extends "main/base.html" %}
{% load main_extras i18n %} {% load i18n main_extras history_extras %}
{% block title %}{{ object }} {{ block.super }}{% endblock %} {% block title %}
{{ category }} {{ block.super }}
{% endblock title %}
{% block link %} {% block link %}
{{ block.super }} {{ block.super }}
{% css "main/css/table.css" %} {% css "main/css/table.css" %}
{% css "main/css/plot.css" %} {% css "main/css/plot.css" %}
{% endblock %} {% endblock link %}
{% block body %} {% block body %}
<h2>{{ object.icon|remix }}{{ object }}</h2> <h2>{{ category.icon|remix }}{{ category }}</h2>
<p> <p>
<a href="{% url "edit_category" object.pk %}">{{ "edit"|remix }}{% translate "Edit category" %}</a> <a href="{% url "edit_category" category.pk %}">{{ "edit"|remix }}{% translate "Edit category" %}</a>
</p> </p>
<section> <section>
<h3>{% translate "Transactions" %}</h3> <h3>{% translate "Transactions" %}</h3>
{% include "transaction/transaction_table.html" %} {% include "transaction/transaction_table.html" %}
</section> </section>
{% if history %} <section>
<section> <h3>{% translate "History" %}</h3>
<h3>{% translate "History" %}</h3> {% history_plot category.transaction_set.all %}
{% include "history/plot.html" %} </section>
</section> {% endblock body %}
{% endif %}
{% endblock %}

View file

@ -1,6 +1,5 @@
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 history.utils import history
from main.views import ( from main.views import (
NummiCreateView, NummiCreateView,
NummiDeleteView, NummiDeleteView,
@ -27,6 +26,7 @@ class CategoryUpdateView(NummiUpdateView):
class CategoryDetailView(NummiDetailView): class CategoryDetailView(NummiDetailView):
model = Category model = Category
pk_url_kwarg = "category" pk_url_kwarg = "category"
context_object_name = "category"
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
_max = 8 _max = 8
@ -39,9 +39,7 @@ class CategoryDetailView(NummiDetailView):
"category_transactions", args=(category.pk,) "category_transactions", args=(category.pk,)
) )
return data | { return data
"history": history(category.transaction_set),
}
class CategoryDeleteView(NummiDeleteView): class CategoryDeleteView(NummiDeleteView):

View file

@ -2,11 +2,19 @@ import math
from django import template from django import template
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
from history.utils import history
from main.templatetags.main_extras import pmrvalue, remix from main.templatetags.main_extras import pmrvalue, remix
register = template.Library() register = template.Library()
@register.inclusion_tag("history/plot.html")
def history_plot(transactions, **kwargs):
return kwargs | {
"history": history(transactions),
}
@register.simple_tag @register.simple_tag
def calendar_opacity(v, vmax): def calendar_opacity(v, vmax):
if v is None: if v is None:

View file

@ -1,6 +1,6 @@
{% extends "main/base.html" %} {% extends "main/base.html" %}
{% load static %} {% load static %}
{% load main_extras account_extras statement_extras %} {% load main_extras account_extras history_extras statement_extras %}
{% load i18n %} {% load i18n %}
{% block link %} {% block link %}
{{ block.super }} {{ block.super }}
@ -31,10 +31,8 @@
</p> </p>
{% endspaceless %} {% endspaceless %}
</section> </section>
{% if history %} <section>
<section> <h2>{% translate "History" %}</h2>
<h2>{% translate "History" %}</h2> {% history_plot user.transaction_set.all %}
{% include "history/plot.html" %} </section>
</section>
{% endif %}
{% endblock body %} {% endblock body %}

View file

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