From 1b1e0c52881834c7c77b42cf8ed8dba4335e7c1e Mon Sep 17 00:00:00 2001 From: sinavir Date: Tue, 18 Mar 2025 14:17:51 +0100 Subject: [PATCH] feat(cof_clubs): Display archived accounting periods in detail view --- .../templates/cof_clubs/club_detail.html | 30 +++++---- cof_clubs/views.py | 64 ++++++++----------- 2 files changed, 46 insertions(+), 48 deletions(-) diff --git a/cof_clubs/templates/cof_clubs/club_detail.html b/cof_clubs/templates/cof_clubs/club_detail.html index 26840150..146a6c33 100644 --- a/cof_clubs/templates/cof_clubs/club_detail.html +++ b/cof_clubs/templates/cof_clubs/club_detail.html @@ -3,14 +3,13 @@ {% block content %} -{% regroup lines by accounting_period__name as lines_list %}

{{ object.name }}

Respos:
{% for r in object.respos.all %} -
{{r.username}}
+ {% empty %}
Ce club n'a pas de respo déclaré
{% endfor%} @@ -20,16 +19,23 @@
Gestionnaires additionels du budget:
{% for r in object.budget_managers.all %} -
{{r.username}}
+ {% endfor%}
{% endif %} - {% for period in lines_list %} + {% for period in accounting_periods %}

- {{ period.grouper }} -

+ {{ period.name }} + {% if period.is_archived %} +
Archivé
+ {% else %} + + {% endif %} + + {% for line in period.lines %} + {% if forloop.first %} @@ -41,7 +47,7 @@ - {% for line in period.list %} + {% endif %} @@ -80,14 +86,14 @@ - {% endfor %} + {% if forloop.last %}
{{ line.date|date:"SHORT_DATE_FORMAT" }} {{ line.label }}
- + {% endif %} + {% empty %} +
Le club n'a pas demandé de budget sur cet exercice.
+ {% endfor %} {% endfor %}
{% endblock content %} - - - diff --git a/cof_clubs/views.py b/cof_clubs/views.py index 7ddba042..27d9084b 100644 --- a/cof_clubs/views.py +++ b/cof_clubs/views.py @@ -115,44 +115,36 @@ class ClubDetailView(AccessMixin, DetailView): def get_context_data(self, *args, **kwargs): ctx = super().get_context_data(*args, **kwargs) - lines_qs = ( - ctx["object"] - .clubbudgetline_set.filter( - accounting_period__is_archived=False, + club = ctx["object"] + accounting_periods_qs = ClubBudgetAccountingPeriod.objects.prefetch_related( + "clubbudgetline_set" + ).order_by("is_archived", "name") + accounting_periods = accounting_periods_qs.values("name", "is_archived", "id") + + user_manages_club = ( + self.request.user in club.respos.all() + or self.request.user in club.budget_managers.all() + ) # We don't have to check buro here since it is checked later + # Fill accounting period data with the budget lines + # (and compute editing rights and partial sums) + for i, period in enumerate(accounting_periods): + s = 0 + lines = ( + accounting_periods_qs[i] + .clubbudgetline_set.filter(club=club) + .order_by("date") + .values("amount", "label", "date", "id", "posted") ) - .prefetch_related("club__respos", "club__budget_managers") - .order_by( - "accounting_period__is_archived", "accounting_period__name", "date" - ) - ) - lines = lines_qs.values( - "accounting_period__name", - "accounting_period__id", - "date", - "amount", - "label", - "id", - "posted", - "accounting_period__is_archived", - ) - # Compute rights and partial sums - s = 0 - current_period = None - for i, line in enumerate(lines): - if line["accounting_period__id"] != current_period: - current_period = line["accounting_period__id"] - s = 0 - s += line["amount"] - line["can_edit"] = self.request.user.profile.is_buro or ( - not line["posted"] - and not line["accounting_period__is_archived"] - and ( - self.request.user in lines_qs[i].club.respos.all() - or self.request.user in lines_qs[i].club.budget_managers.all() + for j, value in enumerate(lines): + s += value["amount"] + value["can_edit"] = self.request.user.profile.is_buro or ( + not value["posted"] + and not period["is_archived"] + and user_manages_club ) - ) - line["remaining"] = s - ctx["lines"] = lines + value["remaining"] = s + accounting_periods[i]["lines"] = lines + ctx["accounting_periods"] = accounting_periods return ctx