diff --git a/scipost_django/finances/templates/finances/_hx_country_level_data.html b/scipost_django/finances/templates/finances/_hx_country_level_data.html index db604e25e80b681ee10a086410cac48f9685d101..12ca9005648c45bf32657194f062fac2a77c73ac 100644 --- a/scipost_django/finances/templates/finances/_hx_country_level_data.html +++ b/scipost_django/finances/templates/finances/_hx_country_level_data.html @@ -15,16 +15,16 @@ <tbody> <tr class="bg-light"> <td>Cumulative</td> - <td style="text-align: right;">{{ cumulative.expenditures }}</td> - <td style="text-align: right;">{{ cumulative.subsidy_income }}</td> - <td style="text-align: right;">{{ cumulative.impact_on_reserves }}</td> + <td style="text-align: right;">€{{ cumulative.expenditures }}</td> + <td style="text-align: right;">€{{ cumulative.subsidy_income }}</td> + <td style="text-align: right;">€{{ cumulative.impact_on_reserves }}</td> </tr> {% for year, val in per_year.items %} <tr> <td>{{ year }}</td> - <td style="text-align: right;">{{ val.expenditures }}</td> - <td style="text-align: right;">{{ val.subsidy_income }}</td> - <td style="text-align: right;">{{ val.impact_on_reserves }}</td> + <td style="text-align: right;">€{{ val.expenditures }}</td> + <td style="text-align: right;">€{{ val.subsidy_income }}</td> + <td style="text-align: right;">€{{ val.impact_on_reserves }}</td> </tr> {% endfor %} </tbody> diff --git a/scipost_django/finances/templates/finances/business_model.html b/scipost_django/finances/templates/finances/business_model.html index d44f3188f6780704d2b2d82f96590527242e1e49..85bf148c89b6204a579165a355a86ad68ae3c5c5 100644 --- a/scipost_django/finances/templates/finances/business_model.html +++ b/scipost_django/finances/templates/finances/business_model.html @@ -580,7 +580,7 @@ </p> </div> - <h3 class="highlight">Closing words</h3> + <h2 class="highlight">Closing words</h2> <div class="m-2"> <p> As far as our operations are concerned, we run them in the most efficient way possible, diff --git a/scipost_django/finances/templates/finances/country_level_data.html b/scipost_django/finances/templates/finances/country_level_data.html index e01d3767d70e449fe816b67f7f29b316fda6ec7f..83981e70f791362e5ca32d8bb7e34d48233ac3cb 100644 --- a/scipost_django/finances/templates/finances/country_level_data.html +++ b/scipost_django/finances/templates/finances/country_level_data.html @@ -31,49 +31,60 @@ <thead> <tr> <th colspan="2">Country</th> - <th style="text-align: right;">Expenditures</th> - <th style="text-align: right;">Subsidy income</th> - <th style="text-align: right;">Impact on reserves</th> + <th style="text-align: right;">Expenditures [rank]<br> + <a href="{% url 'finances:country_level_data' %}?ordering=expenditures">{% include 'bi/sort-up.html' %}</a> + <a href="{% url 'finances:country_level_data' %}?ordering=expenditures&reverse=true">{% include 'bi/sort-down.html' %}</a> + </th> + <th style="text-align: right;">Subsidy income [rank]<br> + <a href="{% url 'finances:country_level_data' %}?ordering=subsidy_income&reverse=true">{% include 'bi/sort-up.html' %}</a> + <a href="{% url 'finances:country_level_data' %}?ordering=subsidy_income">{% include 'bi/sort-down.html' %}</a> + </th> + <th style="text-align: right;">Impact on reserves [rank]<br> + <a href="{% url 'finances:country_level_data' %}?ordering=impact&reverse=true">{% include 'bi/sort-up.html' %}</a> + <a href="{% url 'finances:country_level_data' %}?ordering=impact">{% include 'bi/sort-down.html' %}</a> + </th> </tr> </thead> <tbody> - {% for item in countrydata|dictsortreversed:"impact_on_reserves" %} - {% if item.impact_on_reserves != 0 %} - {% get_country item.country as country_obj %} - <tr class="bg-light"> - <td> - <a hx-get="{% url 'finances:_hx_country_level_data' country=item.country %}" - hx-target="#country_data-{{ item.country }}" - > - <i class="{{ country_obj.flag_css }}"></i> - </a> - </td> - <td>{{ country_obj.name }}</td> - <td style="text-align: right;">{{ item.expenditures }}</td> - <td style="text-align: right;">{{ item.subsidy_income }}</td> - <td style="text-align: right;">{{ item.impact_on_reserves }}</td> - </tr> - <tr class="bg-light"> - <td colspan="5"> - <details id="country_data-{{ item.country }}-details" - class="ms-4 mb-4"> - <summary>Toggle details</summary> - <div id="country_data-{{ item.country }}-details-contents" - class="mt-4 p-2 ps-4" - hx-get="{% url 'finances:_hx_country_level_data' country=item.country %}" - hx-trigger="toggle once from:#country_data-{{ item.country }}-details" + {% with nr=countrydata|length %} + {% for item in countrydata %} + {% if item.impact_on_reserves != 0 %} + {% get_country item.country as country_obj %} + <tr class="bg-light"> + <td> + <a hx-get="{% url 'finances:_hx_country_level_data' country=item.country %}" + hx-target="#country_data-{{ item.country }}" > - <button class="htmx-indicator btn btn-sm btn-warning p-2" type="button" disabled> - <small><strong>Loading...</strong></small> - <div class="spinner-grow spinner-grow-sm ms-2" role="status" aria-hidden="true"></div> - </button> - </div> - </details> - </td> - </tr> - <tr><td></td></tr> - {% endif %} - {% endfor %} + <i class="{{ country_obj.flag_css }}"></i> + </a> + </td> + <td>{{ country_obj.name }}</td> + <td style="text-align: right;">€{{ item.expenditures }} [{{ item.expenditures_rank }}/{{ nr }}]</td> + <td style="text-align: right;">€{{ item.subsidy_income }} [{{ item.subsidy_income_rank }}/{{ nr }}]</td> + <td style="text-align: right;">€{{ item.impact_on_reserves }} [{{ item.impact_on_reserves_rank }}/{{ nr }}]</td> + </tr> + <tr class="bg-light"> + <td colspan="5"> + <details id="country_data-{{ item.country }}-details" + class="ms-4 mb-4"> + <summary>Toggle details</summary> + <div id="country_data-{{ item.country }}-details-contents" + class="mt-4 p-2 ps-4" + hx-get="{% url 'finances:_hx_country_level_data' country=item.country %}" + hx-trigger="toggle once from:#country_data-{{ item.country }}-details" + > + <button class="htmx-indicator btn btn-sm btn-warning p-2" type="button" disabled> + <small><strong>Loading...</strong></small> + <div class="spinner-grow spinner-grow-sm ms-2" role="status" aria-hidden="true"></div> + </button> + </div> + </details> + </td> + </tr> + <tr><td></td></tr> + {% endif %} + {% endfor %} + {% endwith %} </tbody> </table> diff --git a/scipost_django/finances/views.py b/scipost_django/finances/views.py index 9ad74424b3340b009b9f6ef2a0eef2f5d9d54ce2..95c04df8e7e28a25590dc55b5f3b9248500a26a6 100644 --- a/scipost_django/finances/views.py +++ b/scipost_django/finances/views.py @@ -224,15 +224,52 @@ def country_level_data(request): ] context = { "countrycodes": countrycodes, - "countrydata": [], } + countrydatalist = [] for country in countrycodes: country_organizations = Organization.objects.filter(country=country) - countrydata = {"country": country, "subsidy_income": 0, "expenditures": 0, "impact_on_reserves": 0} + countrydata = { + "country": country, + "expenditures": 0, + "expenditures_rank": None, + "subsidy_income": 0, + "subsidy_income_rank": None, + "impact_on_reserves": 0, + "impact_on_reserves_rank": None, + } for organization in country_organizations: for key in ("subsidy_income", "expenditures", "impact_on_reserves"): countrydata[key] += organization.cf_balance_info["cumulative"][key] - context["countrydata"] += [countrydata,] + countrydatalist += [ + countrydata, + ] + # Determine the ranks + countrydatalist.sort(key=lambda country: country["expenditures"], reverse=True) + for idx, c in enumerate(countrydatalist): + c["expenditures_rank"] = idx + 1 + countrydatalist.sort(key=lambda country: country["subsidy_income"], reverse=True) + for idx, c in enumerate(countrydatalist): + c["subsidy_income_rank"] = idx + 1 + countrydatalist.sort( + key=lambda country: country["impact_on_reserves"], reverse=True + ) + for idx, c in enumerate(countrydatalist): + c["impact_on_reserves_rank"] = idx + 1 + + ordering = request.GET.get("ordering", None) + reverse_ordering = request.GET.get("reverse", None) + if ordering == "expenditures": + countrydatalist.sort(key=lambda country: country["expenditures"]) + elif ordering == "subsidy_income": + countrydatalist.sort(key=lambda country: country["subsidy_income"]) + elif ordering == "impact": + countrydatalist.sort(key=lambda country: country["impact_on_reserves"]) + if reverse_ordering == "true": + countrydatalist.reverse() + print( + f"{reverse_ordering = } {type(reverse_ordering) = } {reverse_ordering == True}" + ) + context["countrydata"] = countrydatalist return render(request, "finances/country_level_data.html", context) @@ -263,9 +300,9 @@ def _hx_country_level_data(request, country): context["per_year"][year]["expenditures"] += organization.cf_balance_info[ year ]["expenditures"]["total"]["expenditures"] - context["per_year"][year]["impact_on_reserves"] += organization.cf_balance_info[year][ + context["per_year"][year][ "impact_on_reserves" - ] + ] += organization.cf_balance_info[year]["impact_on_reserves"] return render(request, "finances/_hx_country_level_data.html", context)