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 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")

View file

@ -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 %}
<h2>{{ object.icon|remix }}{{ object }}</h2>
<h2>{{ account.icon|remix }}{{ account }}</h2>
<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>
<section>
<h3>{% translate "Statements" %}</h3>
@ -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 %}
</section>
{% if history %}
<section>
<h3>{% translate "History" %}</h3>
{% include "history/plot.html" %}
</section>
{% endif %}
<section>
<h3>{% translate "History" %}</h3>
{% history_plot account.transactions %}
</section>
{% endblock body %}

View file

@ -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:

View file

@ -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 %}
<h2>{{ object.icon|remix }}{{ object }}</h2>
<h2>{{ category.icon|remix }}{{ category }}</h2>
<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>
<section>
<h3>{% translate "Transactions" %}</h3>
{% include "transaction/transaction_table.html" %}
</section>
{% if history %}
<section>
<h3>{% translate "History" %}</h3>
{% include "history/plot.html" %}
</section>
{% endif %}
{% endblock %}
<section>
<h3>{% translate "History" %}</h3>
{% history_plot category.transaction_set.all %}
</section>
{% endblock body %}

View file

@ -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):

View file

@ -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:

View file

@ -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 @@
</p>
{% endspaceless %}
</section>
{% if history %}
<section>
<h2>{% translate "History" %}</h2>
{% include "history/plot.html" %}
</section>
{% endif %}
<section>
<h2>{% translate "History" %}</h2>
{% history_plot user.transaction_set.all %}
</section>
{% endblock body %}

View file

@ -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):