Skip to content

Commit

Permalink
Ajout d'un filtre 'since_days' dans la méthode 'with_tender_stats'
Browse files Browse the repository at this point in the history
  • Loading branch information
chloend committed Dec 2, 2024
1 parent 82b0664 commit cdff506
Showing 1 changed file with 29 additions and 31 deletions.
60 changes: 29 additions & 31 deletions lemarche/siaes/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import timedelta
from uuid import uuid4

from django.conf import settings
Expand Down Expand Up @@ -78,6 +79,24 @@ def get_city_filter(perimeter, with_country=False):
return filters


def count_field(field_name, date_limit):
"""
Helper method to construct a conditional count annotation.
"""
condition = (
Q(**{f"tendersiae__{field_name}__gte": date_limit})
if date_limit
else Q(**{f"tendersiae__{field_name}__isnull": False})
)
return Sum(
Case(
When(condition, then=1),
default=0,
output_field=IntegerField(),
)
)


class SiaeGroupQuerySet(models.QuerySet):
def with_siae_stats(self):
return self.annotate(siae_count_annotated=Count("siaes", distinct=True))
Expand Down Expand Up @@ -425,41 +444,20 @@ def filter_with_tender_tendersiae_status(self, tender, tendersiae_status=None):

return qs.distinct()

def with_tender_stats(self):
def with_tender_stats(self, since_days=None):
"""
Enrich each Siae with stats on their linked Tender
Enrich each Siae with stats on their linked Tender.
Optionally, limit the stats to the last `since_days` days.
"""
date_limit = timezone.now() - timedelta(days=since_days) if since_days else None

return self.annotate(
tender_count_annotated=Count("tenders", distinct=True),
tender_email_send_count_annotated=Sum(
Case(When(tendersiae__email_send_date__isnull=False, then=1), default=0, output_field=IntegerField())
),
tender_email_link_click_count_annotated=Sum(
Case(
When(tendersiae__email_link_click_date__isnull=False, then=1),
default=0,
output_field=IntegerField(),
)
),
tender_detail_display_count_annotated=Sum(
Case(
When(tendersiae__detail_display_date__isnull=False, then=1), default=0, output_field=IntegerField()
)
),
tender_detail_contact_click_count_annotated=Sum(
Case(
When(tendersiae__detail_contact_click_date__isnull=False, then=1),
default=0,
output_field=IntegerField(),
)
),
tender_detail_not_interested_count_annotated=Sum(
Case(
When(tendersiae__detail_not_interested_click_date__isnull=False, then=1),
default=0,
output_field=IntegerField(),
)
),
tender_email_send_count_annotated=count_field("email_send_date", date_limit),
tender_email_link_click_count_annotated=count_field("email_link_click_date", date_limit),
tender_detail_display_count_annotated=count_field("detail_display_date", date_limit),
tender_detail_contact_click_count_annotated=count_field("detail_contact_click_date", date_limit),
tender_detail_not_interested_count_annotated=count_field("detail_not_interested_click_date", date_limit),
)

def with_brand_or_name(self, with_order_by=False):
Expand Down

0 comments on commit cdff506

Please sign in to comment.