Skip to content

Commit

Permalink
SiaeGroup: update annotated count
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Oct 20, 2023
1 parent 8ccd821 commit cc47e8d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
17 changes: 8 additions & 9 deletions lemarche/siaes/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from django.contrib.contenttypes.admin import GenericTabularInline
from django.contrib.gis import admin as gis_admin
from django.db import models
from django.db.models import Count
from django.urls import reverse
from django.utils.html import format_html, mark_safe
from fieldsets_with_inlines import FieldsetsInlineMixin
Expand Down Expand Up @@ -717,14 +716,14 @@ def image_url_display(self, siae_image):

@admin.register(SiaeGroup, site=admin_site)
class SiaeGroupAdmin(admin.ModelAdmin):
list_display = ["id", "name", "nb_siaes", "created_at"]
list_display = ["id", "name", "siae_live_count_annotated_with_link", "created_at"]
search_fields = ["id", "name"]
search_help_text = "Cherche sur les champs : ID, Nom"

prepopulated_fields = {"slug": ("name",)}
autocomplete_fields = ["sectors"]
readonly_fields = [f"{field}_last_updated" for field in SiaeGroup.TRACK_UPDATE_FIELDS] + [
"nb_siaes",
"siae_live_count_annotated_with_link",
"logo_url_display",
"created_at",
"updated_at",
Expand All @@ -745,7 +744,7 @@ class SiaeGroupAdmin(admin.ModelAdmin):
"year_constitution",
"siae_count",
"siae_count_last_updated",
"nb_siaes",
"siae_live_count_annotated_with_link",
"employees_insertion_count",
"employees_insertion_count_last_updated",
"employees_permanent_count",
Expand Down Expand Up @@ -782,15 +781,15 @@ class SiaeGroupAdmin(admin.ModelAdmin):

def get_queryset(self, request):
qs = super().get_queryset(request)
qs = qs.annotate(siae_count_live=Count("siaes", distinct=True))
qs = qs.with_siae_stats()
return qs

def nb_siaes(self, siae_group):
def siae_live_count_annotated_with_link(self, siae_group):
url = reverse("admin:siaes_siae_changelist") + f"?groups__in={siae_group.id}"
return format_html(f'<a href="{url}">{siae_group.siae_count_live}</a>')
return format_html(f'<a href="{url}">{siae_group.siae_live_count_annotated}</a>')

nb_siaes.short_description = "Nombre de structures (live)"
nb_siaes.admin_order_field = "siae_count_live"
siae_live_count_annotated_with_link.short_description = "Nombre de structures (live)"
siae_live_count_annotated_with_link.admin_order_field = "siae_live_count_annotated"

def logo_url_display(self, siae_group):
if siae_group.logo_url:
Expand Down
5 changes: 5 additions & 0 deletions lemarche/siaes/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ class Meta:
name = factory.Faker("company", locale="fr_FR")
# slug auto-generated

@factory.post_generation
def siaes(self, create, extracted, **kwargs):
if extracted:
self.siaes.add(*extracted)


class SiaeFactory(DjangoModelFactory):
class Meta:
Expand Down
7 changes: 7 additions & 0 deletions lemarche/siaes/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ def get_city_filter(perimeter, with_country=False):
return filters


class SiaeGroupQuerySet(models.QuerySet):
def with_siae_stats(self):
return self.annotate(siae_live_count_annotated=Count("siaes", distinct=True))


class SiaeGroup(models.Model):
TRACK_UPDATE_FIELDS = [
# set last_updated fields
Expand Down Expand Up @@ -119,6 +124,8 @@ class SiaeGroup(models.Model):
created_at = models.DateTimeField("Date de création", default=timezone.now)
updated_at = models.DateTimeField("Date de modification", auto_now=True)

objects = models.Manager.from_queryset(SiaeGroupQuerySet)()

class Meta:
verbose_name = "Groupement"
verbose_name_plural = "Groupements"
Expand Down
14 changes: 14 additions & 0 deletions lemarche/siaes/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ def test_update_last_updated_fields(self):
self.assertNotEqual(siae_group.employees_insertion_count_last_updated, employees_insertion_count_last_updated)


class SiaeGroupQuerySetTest(TestCase):
@classmethod
def setUpTestData(cls):
cls.siae_1 = SiaeFactory()
cls.siae_2 = SiaeFactory(is_active=False)
cls.siae_group = SiaeGroupFactory()
cls.siae_group_with_siaes = SiaeGroupFactory(siaes=[cls.siae_1, cls.siae_2])

def test_with_siae_stats(self):
siae_group_queryset = SiaeGroupFactory.objects.with_siae_stats()
self.assertEqual(siae_group_queryset.get(id=self.siae_group.id).siae_live_count_annotated, 0)
self.assertEqual(siae_group_queryset.get(id=self.siae_group_with_siaes.id).siae_live_count_annotated, 1)


class SiaeModelTest(TestCase):
def setUp(self):
pass
Expand Down

0 comments on commit cc47e8d

Please sign in to comment.