Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Besoins): Correction sur le nombre d'occurrences en status Nouveau sur le filtre par type #1126

Merged
merged 3 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 15 additions & 16 deletions lemarche/tenders/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,21 @@ def with_siae_stats(self):
),
)

def unread(self, user):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

est-ce que tu as vu des ralentissements dans le chargement des pages ? est-ce qu'on pourra mesure facilement avant/après ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Je ne vois pas de ralentissement entre hier et le 12. À surveiller.
impact_latence_19

return (
self.is_live()
.filter(tendersiae__siae__in=user.siaes.all())
.filter(tendersiae__detail_display_date__isnull=True)
.distinct()
)

def unread_stats(self, user):
aggregates = {
f"unread_count_{kind}_annotated": Count(Case(When(kind=kind, then=1), output_field=IntegerField()))
for kind, _ in tender_constants.KIND_CHOICES
}
return self.unread(user).aggregate(**aggregates)

def with_network_siae_stats(self, network_siaes):
return self.annotate(
network_siae_email_send_count_annotated=Sum(
Expand Down Expand Up @@ -983,22 +998,6 @@ def with_status(self):
)
)

def unread_stats(self, user):
limit_date = datetime.today()
aggregates = {
f"unread_count_{kind}_annotated": 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)
)

def email_click_reminder(self, gte_days_ago, lt_days_ago):
return (
self.filter(email_send_date__gte=gte_days_ago)
Expand Down
35 changes: 29 additions & 6 deletions lemarche/tenders/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,35 @@ def test_with_network_siae_stats(self):
# self.assertEqual(tender_with_siae_1.siae_detail_contact_click_since_last_seen_date_count_annotated, 1)


class TenderModelQuerysetUnreadStatsTest(TestCase):
@classmethod
def setUpTestData(cls):
cls.user_siae = UserFactory(kind=User.KIND_SIAE)
_ = UserFactory(kind=User.KIND_SIAE)

siae_with_tender_1 = SiaeFactory(users=[cls.user_siae])
siae_with_tender_2 = SiaeFactory(users=[cls.user_siae])
siae_with_tender_3 = SiaeFactory()
tender_with_siae_1 = TenderFactory(siaes=[siae_with_tender_1, siae_with_tender_2], deadline_date=date_tomorrow)
TenderSiae.objects.create(tender=tender_with_siae_1, siae=siae_with_tender_3)

tender_with_siae_2 = TenderFactory()
TenderSiae.objects.create(
tender=tender_with_siae_2,
siae=siae_with_tender_1,
detail_display_date=timezone.now(), # the user read it
)
_ = TenderFactory(deadline_date=date_tomorrow)
_ = TenderFactory(siaes=[siae_with_tender_1], deadline_date=date_tomorrow, kind=tender_constants.KIND_QUOTE)
_ = TenderFactory(siaes=[siae_with_tender_1], deadline_date=date_tomorrow, kind=tender_constants.KIND_TENDER)

def test_unread_stats(self):
stats = Tender.objects.unread_stats(user=self.user_siae)
self.assertEqual(stats[f"unread_count_{tender_constants.KIND_TENDER}_annotated"], 1)
self.assertEqual(stats[f"unread_count_{tender_constants.KIND_QUOTE}_annotated"], 2)
self.assertEqual(stats[f"unread_count_{tender_constants.KIND_PROJECT}_annotated"], 0)


class TenderMigrationToSelectTest(TestCase):
def test_migrate_amount_to_select(self):
# the migrations can't be imported directly, it's a syntax error.
Expand Down Expand Up @@ -843,12 +872,6 @@ def test_detail_contact_click_post_reminder(self):
1,
)

def test_unread_stats(self):
stats = TenderSiae.objects.unread_stats(user=self.user_siae)
self.assertEqual(stats[f"unread_count_{tender_constants.KIND_TENDER}_annotated"], 1)
self.assertEqual(stats[f"unread_count_{tender_constants.KIND_QUOTE}_annotated"], 1)
self.assertEqual(stats[f"unread_count_{tender_constants.KIND_PROJECT}_annotated"], 0)

def test_status_property(self):
self.assertEqual(
TenderSiae.objects.get(id=self.tendersiae_1.id).status,
Expand Down
10 changes: 2 additions & 8 deletions lemarche/users/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from datetime import datetime

from django.conf import settings
from django.contrib.auth.base_user import BaseUserManager
from django.contrib.auth.models import AbstractUser
Expand Down Expand Up @@ -364,13 +362,9 @@ def has_tender_siae(self, tender=None):

@property
def tender_siae_unread_count(self):
from lemarche.tenders.models import TenderSiae
from lemarche.tenders.models import Tender

limit_date = datetime.today()
qs = TenderSiae.objects.filter(
siae__in=self.siaes.all(), tender__validated_at__isnull=False, tender__deadline_date__gt=limit_date
).filter(detail_display_date__isnull=True)
return qs.count()
return Tender.objects.unread(self).count()


@receiver(post_save, sender=User)
Expand Down
2 changes: 1 addition & 1 deletion lemarche/www/tenders/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ class TenderFilterForm(forms.Form):
def __init__(self, user, *args, **kwargs):
super().__init__(*args, **kwargs)

stats = TenderSiae.objects.unread_stats(user=user)
stats = Tender.objects.unread_stats(user=user)
new_choices = []
for kind_key, kind_label in self.FORM_KIND_CHOICES:
count_key = f"unread_count_{kind_key}_annotated"
Expand Down
Loading