Skip to content

Commit

Permalink
Add to admin. Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Mar 6, 2024
1 parent e01b642 commit f0abe6d
Show file tree
Hide file tree
Showing 3 changed files with 32 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)
10 changes: 9 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,16 @@ 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 +78,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 f0abe6d

Please sign in to comment.