Merge pull request #1495 from ae-utbm/cotis-stat-quick-fix

Small improvements on subscription stats view
This commit is contained in:
thomas girod authored and GitHub committed 2026-10-02 13:49:01 +02:00
commit 0bd557ae73
5 files changed
+64 -64

No files matched your search

+14 -2
View File
@@ -144,10 +144,22 @@ class Command(BaseCommand):
) )
def create_subscriptions(self, users: list[User]): def create_subscriptions(self, users: list[User]):
subscription_types = [
"un-semestre",
"deux-semestres",
"cursus-tronc-commun",
"cursus-branche",
]
def prepare_subscription(_user: User, start_date: date) -> Subscription: def prepare_subscription(_user: User, start_date: date) -> Subscription:
payment_method = random.choice(settings.SITH_SUBSCRIPTION_PAYMENT_METHOD)[0] payment_method = random.choice(settings.SITH_SUBSCRIPTION_PAYMENT_METHOD)[0]
duration = random.randint(1, 4) subscription_type = random.choice(subscription_types)
s = Subscription(member=_user, payment_method=payment_method) s = Subscription(
member=_user,
payment_method=payment_method,
subscription_type=subscription_type,
)
duration = settings.SITH_SUBSCRIPTIONS[subscription_type]["duration"]
s.subscription_start = s.compute_start(d=start_date, duration=duration) s.subscription_start = s.compute_start(d=start_date, duration=duration)
s.subscription_end = s.compute_end(duration) s.subscription_end = s.compute_end(duration)
return s return s
+1 -12
View File
@@ -8,22 +8,11 @@ from django.utils.translation import gettext_lazy as _
from core.models import User from core.models import User
from core.utils import get_last_promo from core.utils import get_last_promo
from core.views.forms import SelectDate, SelectDateTime from core.views.forms import SelectDate
from core.views.widgets.ajax_select import AutoCompleteSelectUser from core.views.widgets.ajax_select import AutoCompleteSelectUser
from subscription.models import Subscription from subscription.models import Subscription
class SelectionDateForm(forms.Form):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["start_date"] = forms.DateTimeField(
label=_("Start date"), widget=SelectDateTime, required=True
)
self.fields["end_date"] = forms.DateTimeField(
label=_("End date"), widget=SelectDateTime, required=True
)
class SubscriptionForm(forms.ModelForm): class SubscriptionForm(forms.ModelForm):
allowed_payment_methods = ["CARD", "CASH", "AE_ACCOUNT"] allowed_payment_methods = ["CARD", "CASH", "AE_ACCOUNT"]
@@ -1,6 +1,13 @@
import { BarController, BarElement, CategoryScale, Chart, LinearScale } from "chart.js"; import {
BarController,
BarElement,
CategoryScale,
Chart,
LinearScale,
Tooltip,
} from "chart.js";
Chart.register(BarController, BarElement, CategoryScale, LinearScale); Chart.register(BarController, BarElement, CategoryScale, LinearScale, Tooltip);
function getRandomColor() { function getRandomColor() {
const letters = "0123456789ABCDEF"; const letters = "0123456789ABCDEF";
+14 -26
View File
@@ -11,51 +11,39 @@
{% block content %} {% block content %}
<p>
<form>
{{ form.start_date.label }}<br>
{{ form.start_date }}<br><br>
{{ form.end_date.label }}<br>
{{ form.end_date }}<br>
<p><input type="submit" value="{% trans %}Go{% endtrans %}" /></p>
</form>
</p>
<canvas id="statsChart" width="400" height="200"></canvas> <canvas id="statsChart" width="400" height="200"></canvas>
<p>
{% trans %}Total subscriptions{% endtrans %} : {{ subscriptions_total.count() }}<br><br>
{% trans %}Subscriptions by type{% endtrans %}<br><br>
{% for location in locations %}
{{ location[1] }} : <i class="nb">{{ subscriptions_total.filter(location=location[0]).count() }}</i><br>
{% endfor %}
<p>
<br> <br>
<table> <table>
<tr> <thead>
<th>{% trans %}Subscription type{% endtrans %}</th> <th>{% trans %}Subscription type{% endtrans %}</th>
{% for location in locations %} {% for location in locations %}
<th>{{ location[1] }}</th> <th>{{ location[1] }}</th>
{% endfor %} {% endfor %}
<th id="graphLabel">{% trans %}Total{% endtrans %}</th> <th id="graphLabel">{% trans %}Total{% endtrans %}</th>
</thead>
<tr>
<td>{% trans %}All subscriptions{% endtrans %}</td>
{% for location in locations %}
<td><i class="nb">{{ total_location[location[0]] }}</i><br></td>
{% endfor %}
<td><i class="nb">{{ total_location.values()|sum }}</i></td>
</tr>
{% for type in subscriptions_types %} {% for type in subscriptions_types %}
<tr> <tr>
<td><i class="types">{{ subscriptions_types[type]['name'] }}</i></td> <td><i class="types">{{ subscriptions_types[type]['name'] }}</i></td>
{% set subscriptions_total_type = subscriptions_total.filter(subscription_type=type) %}
{% for location in locations %} {% for location in locations %}
<td> <td>
{% set subscriptions_total_type_location = subscriptions_total_type.filter(location=location[0]) %}
{% trans %}Total{% endtrans %} : {{ subscriptions_total_type_location.count()}}<br>
{% for p_type in payment_types %} {% for p_type in payment_types %}
{{ p_type[1] }} : <i class="nb">{{ subscriptions_total_type_location.filter(payment_method=p_type[0]).count()}}</i><br> {% set subtotal = subscriptions[type][location[0]][p_type[0]] %}
{% if subtotal > 0 %}
{{ p_type[1] }} : <i class="nb">{{ subtotal }}</i><br>
{% endif %}
{% endfor %} {% endfor %}
</td> </td>
{% endfor %} {% endfor %}
<td class="total"><i class="nb">{{subscriptions_total_type.count()}}</i> <td class="total"><i class="nb">{{ total_type[type] }}</i></td>
</tr> </tr>
{% endfor %} {% endfor %}
</table> </table>
{% endblock %} {% endblock %}
+26 -22
View File
@@ -12,21 +12,21 @@
# OR WITHIN THE LOCAL FILE "LICENSE" # OR WITHIN THE LOCAL FILE "LICENSE"
# #
# #
from collections import defaultdict
from django.conf import settings from django.conf import settings
from django.contrib.auth.forms import PasswordResetForm from django.contrib.auth.forms import PasswordResetForm
from django.contrib.auth.mixins import PermissionRequiredMixin from django.contrib.auth.mixins import PermissionRequiredMixin
from django.core.exceptions import PermissionDenied from django.core.exceptions import PermissionDenied
from django.urls import reverse, reverse_lazy from django.db.models import Count
from django.urls import reverse
from django.utils.timezone import localdate from django.utils.timezone import localdate
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from django.views.generic import CreateView, DetailView, TemplateView from django.views.generic import CreateView, DetailView, TemplateView
from django.views.generic.edit import FormView
from core.views import FragmentMixin, UseFragmentsMixin from core.views import FragmentMixin, UseFragmentsMixin
from core.views.group import PermissionGroupsUpdateView from core.views.group import PermissionGroupsUpdateView
from subscription.forms import ( from subscription.forms import (
SelectionDateForm,
SubscriptionExistingUserForm, SubscriptionExistingUserForm,
SubscriptionNewUserForm, SubscriptionNewUserForm,
) )
@@ -93,35 +93,39 @@ class SubscriptionPermissionView(PermissionGroupsUpdateView):
extra_context = {"object_name": _("the groups that can create subscriptions")} extra_context = {"object_name": _("the groups that can create subscriptions")}
class SubscriptionsStatsView(FormView): class SubscriptionsStatsView(TemplateView):
template_name = "subscription/stats.jinja" template_name = "subscription/stats.jinja"
form_class = SelectionDateForm
success_url = reverse_lazy("subscriptions:stats")
def dispatch(self, request, *arg, **kwargs): def dispatch(self, request, *arg, **kwargs):
self.start_date = localdate()
self.end_date = self.start_date
if request.user.is_root or request.user.is_board_member: if request.user.is_root or request.user.is_board_member:
return super().dispatch(request, *arg, **kwargs) return super().dispatch(request, *arg, **kwargs)
raise PermissionDenied raise PermissionDenied
def post(self, request, *args, **kwargs):
self.form = self.get_form()
self.start_date = self.form["start_date"]
self.end_date = self.form["end_date"]
return super().post(request, *args, **kwargs)
def get_initial(self):
return {
"start_date": self.start_date.strftime("%Y-%m-%d %H:%M:%S"),
"end_date": self.end_date.strftime("%Y-%m-%d %H:%M:%S"),
}
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
kwargs = super().get_context_data(**kwargs) kwargs = super().get_context_data(**kwargs)
kwargs["subscriptions_total"] = Subscription.objects.filter( today = localdate()
subscription_end__gte=self.end_date, subscription_start__lte=self.start_date 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["subscriptions_types"] = settings.SITH_SUBSCRIPTIONS
kwargs["payment_types"] = settings.SITH_SUBSCRIPTION_PAYMENT_METHOD kwargs["payment_types"] = settings.SITH_SUBSCRIPTION_PAYMENT_METHOD
kwargs["locations"] = settings.SITH_SUBSCRIPTION_LOCATIONS kwargs["locations"] = settings.SITH_SUBSCRIPTION_LOCATIONS