From 73df4f25c88e658d51e7b6312af0da0819a301ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Reuiller?= Date: Wed, 6 Mar 2024 11:58:01 +0100 Subject: [PATCH] tender list : add unread count on kind filter --- lemarche/tenders/models.py | 14 ++++++++++++++ lemarche/tenders/tests.py | 10 +++++++++- lemarche/www/tenders/forms.py | 14 ++++++++++++++ lemarche/www/tenders/tests.py | 17 +++++++++++++++++ lemarche/www/tenders/views.py | 2 +- 5 files changed, 55 insertions(+), 2 deletions(-) diff --git a/lemarche/tenders/models.py b/lemarche/tenders/models.py index cdc966730..c2bfceb63 100644 --- a/lemarche/tenders/models.py +++ b/lemarche/tenders/models.py @@ -897,6 +897,20 @@ def detail_contact_click_post_reminder(self, gte_days_ago, lt_days_ago): detail_contact_click_date__lt=lt_days_ago ) + def unread_counts(self, user): + limit_date = datetime.today() + aggregates = { + f"count_{kind}": Count(Case(When(tender__kind=kind, then=1), output_field=IntegerField()), distinct=True) + for kind, _ in tender_constants.KIND_CHOICES + } + return ( + self.filter( + siae__in=user.siaes.all(), tender__validated_at__isnull=False, tender__deadline_date__gt=limit_date + ) + .filter(detail_display_date__isnull=True) + .aggregate(**aggregates) + ) + class TenderQuestion(models.Model): text = models.TextField(verbose_name="Intitulé de la question", blank=False) diff --git a/lemarche/tenders/tests.py b/lemarche/tenders/tests.py index 5453ae288..e04895afb 100644 --- a/lemarche/tenders/tests.py +++ b/lemarche/tenders/tests.py @@ -786,7 +786,9 @@ def setUpTestData(cls): siae_with_tender_5 = SiaeFactory() cls.siae_without_tender = SiaeFactory() cls.tender_with_siae_1 = TenderFactory( - siaes=[cls.siae_with_tender_1, siae_with_tender_2], deadline_date=date_tomorrow + siaes=[cls.siae_with_tender_1, siae_with_tender_2], + deadline_date=date_tomorrow, + kind=tender_constants.KIND_TENDER, ) TenderSiae.objects.create( tender=cls.tender_with_siae_1, siae=siae_with_tender_3, email_send_date=date_last_week @@ -830,6 +832,12 @@ def test_detail_contact_click_post_reminder(self): 1, ) + def test_unread_counts(self): + counts = TenderSiae.objects.unread_counts(user=self.user_siae) + self.assertEqual(counts[f"count_{tender_constants.KIND_TENDER}"], 1) + self.assertEqual(counts[f"count_{tender_constants.KIND_QUOTE}"], 1) + self.assertEqual(counts[f"count_{tender_constants.KIND_PROJECT}"], 0) + class TenderAdminTest(TestCase): def setUp(cls): diff --git a/lemarche/www/tenders/forms.py b/lemarche/www/tenders/forms.py index 2e9227291..248efee01 100644 --- a/lemarche/www/tenders/forms.py +++ b/lemarche/www/tenders/forms.py @@ -361,3 +361,17 @@ class TenderFilterForm(forms.Form): ), required=False, ) + + def __init__(self, user, *args, **kwargs): + super().__init__(*args, **kwargs) + + counts = TenderSiae.objects.unread_counts(user=user) + new_choices = [] + for kind_key, kind_label in self.FORM_KIND_CHOICES: + count_key = f"count_{kind_key}" + if count_key in counts and counts[count_key] > 0: + new_choices.append((kind_key, f"{kind_label} ({counts[count_key]})")) + else: + new_choices.append((kind_key, kind_label)) + + self.fields["kind"].choices = new_choices diff --git a/lemarche/www/tenders/tests.py b/lemarche/www/tenders/tests.py index e0b6871db..2cbe4e83b 100644 --- a/lemarche/www/tenders/tests.py +++ b/lemarche/www/tenders/tests.py @@ -585,12 +585,29 @@ def test_siae_user_should_only_see_filtered_kind(self): response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.context["tenders"]), 2) + self.assertContains( + response, + f'', + 1, + html=True, + ) + self.assertContains( + response, + f'', + 1, + html=True, + ) url = reverse("tenders:list") response = self.client.get(f"{url}?kind={tender_constants.KIND_TENDER}") self.assertEqual(response.status_code, 200) self.assertEqual(len(response.context["tenders"]), 1) self.assertEqual(response.context["tenders"][0], self.tender_4) + expected_option = ( + f'" + ) + self.assertContains(response, expected_option, 1, html=True) class TenderDetailViewTest(TestCase): diff --git a/lemarche/www/tenders/views.py b/lemarche/www/tenders/views.py index 555c72ed6..254a8bfaa 100644 --- a/lemarche/www/tenders/views.py +++ b/lemarche/www/tenders/views.py @@ -298,7 +298,7 @@ def get_queryset(self): if self.status: qs = qs.filter(status=self.status) - self.filter_form = TenderFilterForm(data=self.request.GET) + self.filter_form = TenderFilterForm(data=self.request.GET, user=self.request.user) if self.filter_form.is_valid(): kind = self.filter_form.cleaned_data.get("kind") if kind: