From 514a111937644b52e1a52999b4b78966eaad2988 Mon Sep 17 00:00:00 2001 From: imperosol Date: Wed, 23 Sep 2026 08:39:54 +0200 Subject: [PATCH] optimize subscriptions stats --- .../templates/subscription/stats.jinja | 25 ++++++++++--------- subscription/views.py | 23 ++++++++++++++++- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/subscription/templates/subscription/stats.jinja b/subscription/templates/subscription/stats.jinja index c695d996..a1dd22d0 100644 --- a/subscription/templates/subscription/stats.jinja +++ b/subscription/templates/subscription/stats.jinja @@ -13,12 +13,6 @@ - {% trans %}Total subscriptions{% endtrans %} : {{ subscriptions_total.count() }}

- {% trans %}Subscriptions by type{% endtrans %}

- {% for location in locations %} - {{ location[1] }} : {{ subscriptions_total.filter(location=location[0]).count() }}
- {% endfor %} -
@@ -28,20 +22,27 @@ {% endfor %} + + + {% for location in locations %} + + {% endfor %} + + {% for type in subscriptions_types %} - - {% set subscriptions_total_type = subscriptions_total.filter(subscription_type=type) %} + {% for location in locations %} {% endfor %} - + {% endfor %}
{% trans %}Total{% endtrans %}
{% trans %}All subscriptions{% endtrans %}{{ total_location[location[0]] }}
{{ total_location.values()|sum }}
{{ subscriptions_types[type]['name'] }}{{ subscriptions_types[type]['name'] }} - {% set subscriptions_total_type_location = subscriptions_total_type.filter(location=location[0]) %} - {% trans %}Total{% endtrans %} : {{ subscriptions_total_type_location.count()}}
{% for p_type in payment_types %} - {{ p_type[1] }} : {{ subscriptions_total_type_location.filter(payment_method=p_type[0]).count()}}
+ {% set subtotal = subscriptions[type][location[0]][p_type[0]] %} + {% if subtotal > 0 %} + {{ p_type[1] }} : {{ subtotal }}
+ {% endif %} {% endfor %}
{{subscriptions_total_type.count()}}{{ total_type[type] }}
diff --git a/subscription/views.py b/subscription/views.py index 8c790647..8ab28134 100644 --- a/subscription/views.py +++ b/subscription/views.py @@ -12,11 +12,13 @@ # OR WITHIN THE LOCAL FILE "LICENSE" # # +from collections import defaultdict from django.conf import settings from django.contrib.auth.forms import PasswordResetForm from django.contrib.auth.mixins import PermissionRequiredMixin from django.core.exceptions import PermissionDenied +from django.db.models import Count from django.urls import reverse from django.utils.timezone import localdate from django.utils.translation import gettext_lazy as _ @@ -102,9 +104,28 @@ class SubscriptionsStatsView(TemplateView): def get_context_data(self, **kwargs): kwargs = super().get_context_data(**kwargs) today = localdate() - kwargs["subscriptions_total"] = Subscription.objects.filter( + qs = Subscription.objects.filter( subscription_end__gte=today, subscription_start__lte=today ) + grouped = qs.values("subscription_type", "location", "payment_method").annotate( + count=Count("*") + ) + by_location = qs.values("location").annotate(count=Count("*")) + by_type = qs.values("subscription_type").annotate(count=Count("*")) + kwargs["subscriptions"] = defaultdict( + lambda: defaultdict(lambda: defaultdict(int)) + ) + for sub in grouped: + kwargs["subscriptions"][sub["subscription_type"]][sub["location"]][ + sub["payment_method"] + ] = sub["count"] + kwargs["total_location"] = defaultdict( + int, {i["location"]: i["count"] for i in by_location} + ) + kwargs["total_type"] = defaultdict( + int, {i["subscription_type"]: i["count"] for i in by_type} + ) + kwargs["subscriptions_types"] = settings.SITH_SUBSCRIPTIONS kwargs["payment_types"] = settings.SITH_SUBSCRIPTION_PAYMENT_METHOD kwargs["locations"] = settings.SITH_SUBSCRIPTION_LOCATIONS