diff --git a/per/drf_views.py b/per/drf_views.py index 31e25ed63..3c4728a34 100644 --- a/per/drf_views.py +++ b/per/drf_views.py @@ -4,7 +4,6 @@ from django.conf import settings from django.db import transaction from django.db.models import Count, F, Prefetch, Q -from django.db.models.functions import ExtractYear from django.http import HttpResponse from django.shortcuts import get_object_or_404 from django.utils.translation import get_language as django_get_language @@ -932,33 +931,39 @@ def stats(self, request): """ Get the Ops Learning stats based on the filters """ - ops_data = OpsLearning.objects.aggregate( - operations_included=Count("appeal_code", distinct=True), - learning_extracts=Count("id", filter=Q(is_validated=True), distinct=True), - sector_covered=Count("sector_validated", distinct=True), - source_used=Count("appeal_code__appealdocument", distinct=True), + ops_data = ( + super() + .get_queryset() + .aggregate( + operations_included=Count("appeal_code", distinct=True), + learning_extracts=Count("id", filter=Q(is_validated=True), distinct=True), + sector_covered=Count("sector_validated", distinct=True), + source_used=Count("appeal_code__appealdocument", distinct=True), + ) ) learning_by_sector = ( SectorTag.objects.filter(title__isnull=False) .annotate(count=Count("validated_sectors", distinct=True)) - .values("id", "title", "count") + .values("title", "count") ) learning_by_region = ( - Region.objects.annotate(count=Count("appeal__opslearning", distinct=True)) + Region.objects.filter(appeal__opslearning__is_validated=True) + .annotate(count=Count("appeal__opslearning", distinct=True)) .values("id", "label", "count") .order_by("label") ) learning_by_country = ( - Country.objects.annotate(operation_count=Count("appeal__opslearning", distinct=True)) + Country.objects.filter(appeal__opslearning__is_validated=True) + .annotate(operation_count=Count("appeal__opslearning", distinct=True)) .values("id", "name", "operation_count") .order_by("name") ) sources_overtime = { - str(appeal_type_label): OpsLearning.objects.filter(appeal_code__atype=appeal_type) - .annotate(year=ExtractYear(F("appeal_code__start_date"))) + str(appeal_type_label): OpsLearning.objects.filter(appeal_code__atype=appeal_type, is_validated=True) + .annotate(year=F(("appeal_code__start_date"))) .values("year") .annotate(count=Count("appeal_code__appealdocument", distinct=True)) .order_by("year")