Skip to content

Commit

Permalink
tender list : add unread count on kind filter
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastienReuiller committed Mar 6, 2024
1 parent 6f014b3 commit 73df4f2
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 2 deletions.
14 changes: 14 additions & 0 deletions lemarche/tenders/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 9 additions & 1 deletion lemarche/tenders/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
14 changes: 14 additions & 0 deletions lemarche/www/tenders/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 17 additions & 0 deletions lemarche/www/tenders/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'<option value="{tender_constants.KIND_QUOTE}">{tender_constants.KIND_QUOTE_DISPLAY}</option>',
1,
html=True,
)
self.assertContains(
response,
f'<option value="{tender_constants.KIND_TENDER}">{tender_constants.KIND_TENDER_DISPLAY} (1)</option>',
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'<option value="{tender_constants.KIND_TENDER}" selected>'
f"{tender_constants.KIND_TENDER_DISPLAY} (1)</option>"
)
self.assertContains(response, expected_option, 1, html=True)


class TenderDetailViewTest(TestCase):
Expand Down
2 changes: 1 addition & 1 deletion lemarche/www/tenders/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 73df4f2

Please sign in to comment.