Use templatetag for statement_table

This commit is contained in:
Edgar P. Burkhart 2025-01-02 14:41:53 +01:00
parent 786d7c2130
commit b1fddd0dd6
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227
7 changed files with 20 additions and 37 deletions

View file

@ -16,7 +16,9 @@
</p>
<section>
<h3>{% translate "Statements" %}</h3>
{% statement_table statements statements_url=statements_url new_statement_url=new_statement_url %}
{% url "new_statement" account=account.pk as ns_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 %}
</section>
{% if history %}
<section>

View file

@ -15,11 +15,9 @@
<dt>
<a href="{% url "accounts" %}">{{ "gallery-view"|remixnl }}{% translate "All accounts" %}</a>
</dt>
{% if total %}
<dd class="value">
{{ total|value }}
</dd>
{% endif %}
<dd class="value">
{{ accounts|balance|value }}
</dd>
</div>
{% else %}
<div class="more account">

View file

@ -1,5 +1,4 @@
from django.shortcuts import get_object_or_404
from django.urls import reverse_lazy
from history.utils import history
from main.views import (
NummiCreateView,
@ -37,21 +36,10 @@ class AccountDetailView(NummiDetailView):
pk_url_kwarg = "account"
def get_context_data(self, **kwargs):
_max = 6
data = super().get_context_data(**kwargs)
account = data.get("object")
_statements = account.statement_set.all()
if _statements.count() > _max:
data["statements_url"] = reverse_lazy(
"account_statements", args=(account.pk,)
)
return data | {
"new_statement_url": reverse_lazy(
"new_statement", kwargs={"account": account.pk}
),
"statements": _statements[:_max],
"history": history(Transaction.objects.filter(statement__account=account)),
}

View file

@ -12,18 +12,19 @@
<div class="split">
<section>
<h2>{% translate "Accounts" %}</h2>
{% account_table accounts index=True total=accounts|balance %}
{% account_table user.account_set.all index=True %}
</section>
<section>
<h2>{% translate "Statements" %}</h2>
{% statement_table statements statements_url=statements_url %}
{% url "statements" as s_url %}
{% statement_table statements statements_url=s_url %}
</section>
</div>
<section>
<h2>{% translate "Categories" %}</h2>
{% spaceless %}
<p>
{% for cat in categories %}
{% for cat in user.category_set.all %}
<a class="category" href="{{ cat.get_absolute_url }}">{{ cat.icon|remix }}{{ cat }}</a>
{% endfor %}
<a class="category add" href="{% url "new_category" %}">{{ "add"|remix }}{% translate "Create category" %}</a>

View file

@ -1,5 +1,3 @@
from account.models import Account
from category.models import Category
from django.contrib import messages
from django.contrib.auth import views as auth_views
from django.contrib.auth.mixins import LoginRequiredMixin
@ -15,33 +13,24 @@ from django.views.generic import (
UpdateView,
)
from history.utils import history
from statement.models import Statement
from transaction.models import Transaction
class IndexView(LoginRequiredMixin, TemplateView):
template_name = "main/index.html"
def get_context_data(self, **kwargs):
_max = 8
_transactions = Transaction.objects.filter(user=self.request.user)
_accounts = Account.objects.filter(user=self.request.user)
_user = self.request.user
_transactions = _user.transaction_set.all()
_statements = (
Statement.objects.filter(user=self.request.user)
.exclude(account__archived=True)
_user.statement_set.exclude(account__archived=True)
.order_by("account__id", "-date")
.distinct("account__id")
)
res = {
"accounts": _accounts,
"categories": Category.objects.filter(user=self.request.user),
"statements": _statements,
"history": history(_transactions.exclude(category__budget=False)),
}
if _transactions.count() > _max:
res["transactions_url"] = reverse_lazy("transactions")
res["statements_url"] = reverse_lazy("statements")
return super().get_context_data(**kwargs) | res

View file

@ -7,6 +7,6 @@
{% translate "Statements" %}
{% endblock h2 %}
{% block table %}
{% url "new_statement" as new_statement_url %}
{% statement_table statements new_statement_url=new_statement_url %}
{% url "new_statement" as ns_url %}
{% statement_table statements new_statement_url=ns_url %}
{% endblock table %}

View file

@ -16,6 +16,11 @@ def check(s, diff):
@register.inclusion_tag("statement/statement_table.html")
def statement_table(statements, **kwargs):
if (n_max := kwargs.get("n_max")) is not None:
if statements.count() <= n_max:
del kwargs["statements_url"]
statements = statements[:n_max]
return kwargs | {
"statements": statements,
}