Skip to content

Commit

Permalink
Code: update annotated count
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Oct 25, 2023
1 parent cabbb9f commit 525a26e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
21 changes: 13 additions & 8 deletions lemarche/cpv/admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from django.contrib import admin
from django.db.models import Count
from django.urls import reverse
from django.utils.html import format_html

Expand Down Expand Up @@ -27,7 +26,14 @@ def queryset(self, request, queryset):

@admin.register(Code, site=admin_site)
class CodeAdmin(admin.ModelAdmin):
list_display = ["cpv_code", "name", "hierarchy_level", "nb_sectors", "created_at", "updated_at"]
list_display = [
"cpv_code",
"name",
"hierarchy_level",
"sector_count_annotated_with_link",
"created_at",
"updated_at",
]
list_filter = ["hierarchy_level", HasSectorFilter]

autocomplete_fields = ["sectors"]
Expand Down Expand Up @@ -56,13 +62,12 @@ def has_delete_permission(self, request, obj=None):

def get_queryset(self, request):
qs = super().get_queryset(request)
qs = qs.prefetch_related("sectors")
qs = qs.annotate(sector_count=Count("sectors", distinct=True))
qs = qs.with_sector_stats()
return qs

def nb_sectors(self, code):
def sector_count_annotated_with_link(self, code):
url = reverse("admin:sectors_sector_changelist") + f"?cpv_codes__in={code.id}"
return format_html(f'<a href="{url}">{code.sector_count}</a>')
return format_html(f'<a href="{url}">{code.sector_count_annotated}</a>')

nb_sectors.short_description = "Nombre de secteurs correspondants"
nb_sectors.admin_order_field = "sector_count"
sector_count_annotated_with_link.short_description = "Nombre de secteurs correspondants"
sector_count_annotated_with_link.admin_order_field = "sector_count_annotated"
4 changes: 4 additions & 0 deletions lemarche/cpv/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models
from django.db.models import Count
from django.template.defaultfilters import slugify
from django.utils import timezone

Expand All @@ -9,6 +10,9 @@ def has_sector(self):
"""Only return codes who have at least 1 Sector."""
return self.prefetch_related("sectors").filter(sectors__isnull=False).distinct()

def with_sector_stats(self):
return self.annotate(sector_count_annotated=Count("sectors", distinct=True))


class Code(models.Model):
name = models.CharField(verbose_name="Nom", max_length=255)
Expand Down
5 changes: 5 additions & 0 deletions lemarche/cpv/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,8 @@ def setUpTestData(cls):
def test_has_sector(self):
self.assertEqual(Code.objects.count(), 2)
self.assertEqual(Code.objects.has_sector().count(), 1)

def test_with_sector_stats(self):
code_queryset = Code.objects.with_sector_stats()
self.assertEqual(code_queryset.get(id=self.code.id).sector_count_annotated, 0)
self.assertEqual(code_queryset.get(id=self.code_with_sector.id).sector_count_annotated, 1)

0 comments on commit 525a26e

Please sign in to comment.