Skip to content

Commit

Permalink
feat(Admin): Secteurs : afficher le nombre de besoins concernés (#1118)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn authored Mar 6, 2024
1 parent 7c52702 commit 8674309
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
21 changes: 18 additions & 3 deletions lemarche/sectors/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,20 @@ def sector_count_annotated_with_link(self, sector_group):
url = reverse("admin:sectors_sector_changelist") + f"?group__id__exact={sector_group.id}"
return format_html(f'<a href="{url}">{sector_group.sector_count_annotated}</a>')

sector_count_annotated_with_link.short_description = "Nombre de secteurs d'activité"
sector_count_annotated_with_link.short_description = "Secteurs d'activité"
sector_count_annotated_with_link.admin_order_field = "sector_count_annotated"


@admin.register(Sector, site=admin_site)
class SectorAdmin(admin.ModelAdmin):
list_display = ["id", "name", "siae_count_annotated_with_link", "group", "created_at"]
list_display = [
"id",
"name",
"siae_count_annotated_with_link",
"tender_count_annotated_with_link",
"group",
"created_at",
]
list_filter = ["group"]
search_fields = ["id", "name"]
search_help_text = "Cherche sur les champs : ID, Nom"
Expand All @@ -41,11 +48,19 @@ class SectorAdmin(admin.ModelAdmin):
def get_queryset(self, request):
qs = super().get_queryset(request)
qs = qs.with_siae_stats()
qs = qs.with_tender_stats()
return qs

def siae_count_annotated_with_link(self, sector):
url = reverse("admin:siaes_siae_changelist") + f"?sectors__id__exact={sector.id}"
return format_html(f'<a href="{url}">{sector.siae_count_annotated}</a>')

siae_count_annotated_with_link.short_description = "Nombre de structures rattachées"
siae_count_annotated_with_link.short_description = "Structures rattachées"
siae_count_annotated_with_link.admin_order_field = "siae_count_annotated"

def tender_count_annotated_with_link(self, sector):
url = reverse("admin:tenders_tender_changelist") + f"?sectors__id__exact={sector.id}"
return format_html(f'<a href="{url}">{sector.tender_count_annotated}</a>')

tender_count_annotated_with_link.short_description = "Besoins concernés"
tender_count_annotated_with_link.admin_order_field = "tender_count_annotated"
5 changes: 5 additions & 0 deletions lemarche/sectors/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@ class Meta:
def siaes(self, create, extracted, **kwargs):
if extracted:
self.siaes.add(*extracted)

@factory.post_generation
def tenders(self, create, extracted, **kwargs):
if extracted:
self.tenders.add(*extracted)
3 changes: 3 additions & 0 deletions lemarche/sectors/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ def form_filter_queryset(self):
def with_siae_stats(self):
return self.annotate(siae_count_annotated=Count("siaes", distinct=True))

def with_tender_stats(self):
return self.annotate(tender_count_annotated=Count("tenders", distinct=True))


class Sector(models.Model):
name = models.CharField(verbose_name="Nom", max_length=255)
Expand Down
12 changes: 11 additions & 1 deletion lemarche/sectors/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from lemarche.sectors.factories import SectorFactory, SectorGroupFactory
from lemarche.sectors.models import Sector, SectorGroup
from lemarche.siaes.factories import SiaeFactory
from lemarche.tenders.factories import TenderFactory


class SectorGroupModelTest(TestCase):
Expand Down Expand Up @@ -51,14 +52,18 @@ class SectorQuerysetModelTest(TestCase):
def setUpTestData(cls):
cls.siae_1 = SiaeFactory()
cls.siae_2 = SiaeFactory()
cls.tender_1 = TenderFactory()
cls.tender_2 = TenderFactory()
cls.sector_group_1 = SectorGroupFactory(name="Informatique")
cls.sector_group_2 = SectorGroupFactory(name="Bricolage")
cls.sector_1_1 = SectorFactory(name="Développement de logiciel", group=cls.sector_group_1)
cls.sector_1_2 = SectorFactory(name="Dépannage informatique", group=cls.sector_group_1)
cls.sector_1_3 = SectorFactory(name="Autre", group=cls.sector_group_1)
cls.sector_2_1 = SectorFactory(name="Plomberie", group=cls.sector_group_2)
cls.sector_2_2 = SectorFactory(name="Autre (Bricolage)", group=cls.sector_group_2)
cls.sector_3 = SectorFactory(name="Un secteur seul", group=None, siaes=[cls.siae_1, cls.siae_2])
cls.sector_3 = SectorFactory(
name="Un secteur seul", group=None, siaes=[cls.siae_1, cls.siae_2], tenders=[cls.tender_1, cls.tender_2]
)

def test_form_filter_queryset(self):
sectors = Sector.objects.form_filter_queryset()
Expand All @@ -75,3 +80,8 @@ def test_with_siae_stats(self):
sector_queryset = Sector.objects.with_siae_stats()
self.assertEqual(sector_queryset.get(id=self.sector_1_1.id).siae_count_annotated, 0)
self.assertEqual(sector_queryset.get(id=self.sector_3.id).siae_count_annotated, 2)

def test_with_tender_stats(self):
sector_queryset = Sector.objects.with_tender_stats()
self.assertEqual(sector_queryset.get(id=self.sector_1_1.id).tender_count_annotated, 0)
self.assertEqual(sector_queryset.get(id=self.sector_3.id).tender_count_annotated, 2)

0 comments on commit 8674309

Please sign in to comment.