Skip to content

Commit

Permalink
add admin stats
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastienReuiller committed Dec 21, 2023
1 parent 7027b0a commit 0b91b83
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
1 change: 1 addition & 0 deletions lemarche/siaes/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ def lookup_allowed(self, lookup, *args, **kwargs):
"tendersiae__email_link_click_date__isnull",
"tendersiae__detail_display_date__isnull",
"tendersiae__detail_contact_click_date__isnull",
"tendersiae__detail_cocontracting_click_date__isnull",
]:
return True
return super().lookup_allowed(lookup, *args, **kwargs)
Expand Down
18 changes: 18 additions & 0 deletions lemarche/tenders/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ class TenderAdmin(FieldsetsInlineMixin, admin.ModelAdmin):
"siae_detail_display_count_annotated_with_link",
# "siae_email_link_click_or_detail_display_count_annotated_with_link",
"siae_detail_contact_click_count_annotated_with_link",
"siae_detail_cocontracting_click_count_annotated_with_link",
"siae_transactioned",
"created_at",
"validated_at",
Expand Down Expand Up @@ -173,6 +174,7 @@ class TenderAdmin(FieldsetsInlineMixin, admin.ModelAdmin):
"siae_detail_display_count_annotated_with_link",
"siae_email_link_click_or_detail_display_count_annotated_with_link",
"siae_detail_contact_click_count_annotated_with_link",
"siae_detail_cocontracting_click_count_annotated_with_link",
"logs_display",
"extra_data_display",
"source",
Expand Down Expand Up @@ -277,6 +279,7 @@ class TenderAdmin(FieldsetsInlineMixin, admin.ModelAdmin):
"siae_detail_display_count_annotated_with_link",
"siae_email_link_click_or_detail_display_count_annotated_with_link",
"siae_detail_contact_click_count_annotated_with_link",
"siae_detail_cocontracting_click_count_annotated_with_link",
)
},
),
Expand Down Expand Up @@ -348,6 +351,7 @@ def lookup_allowed(self, lookup, *args, **kwargs):
"tendersiae__email_link_click_date__isnull",
"tendersiae__detail_display_date__isnull",
"tendersiae__detail_contact_click_date__isnull",
"tendersiae__detail_cocontracting_click_date__isnull",
]:
return True
return super().lookup_allowed(lookup, *args, **kwargs)
Expand Down Expand Up @@ -468,6 +472,20 @@ def siae_detail_contact_click_count_annotated_with_link(self, tender):
siae_detail_contact_click_count_annotated_with_link.short_description = "S. intéressées"
siae_detail_contact_click_count_annotated_with_link.admin_order_field = "siae_detail_contact_click_count_annotated"

def siae_detail_cocontracting_click_count_annotated_with_link(self, tender):
url = (
reverse("admin:siaes_siae_changelist")
+ f"?tenders__in={tender.id}&tendersiae__detail_cocontracting_click_date__isnull=False"
)
return format_html(
f'<a href="{url}">{getattr(tender, "siae_detail_cocontracting_click_count_annotated", 0)}</a>'
)

siae_detail_cocontracting_click_count_annotated_with_link.short_description = "S. ouvert à la co-traitance"
siae_detail_cocontracting_click_count_annotated_with_link.admin_order_field = (
"siae_detail_cocontracting_click_count_annotated"
)

def logs_display(self, tender=None):
if tender:
return pretty_print_readonly_jsonfield(tender.logs)
Expand Down
7 changes: 7 additions & 0 deletions lemarche/tenders/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@ def with_siae_stats(self):
output_field=IntegerField(),
)
),
siae_detail_cocontracting_click_count_annotated=Sum(
Case(
When(tendersiae__detail_cocontracting_click_date__isnull=False, then=1),
default=0,
output_field=IntegerField(),
)
),
siae_detail_contact_click_since_last_seen_date_count_annotated=Sum(
Case(
When(
Expand Down
15 changes: 13 additions & 2 deletions lemarche/tenders/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ def setUpTestData(cls):
siae_with_tender_4 = SiaeFactory()
siae_with_tender_5 = SiaeFactory()
siae_with_tender_6 = SiaeFactory()
siae_with_tender_7 = 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
Expand Down Expand Up @@ -317,6 +318,13 @@ def setUpTestData(cls):
detail_display_date=timezone.now(),
detail_contact_click_date=timezone.now(),
)
TenderSiae.objects.create(
tender=cls.tender_with_siae_1,
siae=siae_with_tender_7,
email_send_date=timezone.now(),
email_link_click_date=timezone.now(),
detail_cocontracting_click_date=timezone.now(),
)
cls.tender_with_siae_2 = TenderFactory()
TenderSiae.objects.create(
tender=cls.tender_with_siae_2,
Expand All @@ -339,13 +347,14 @@ def test_filter_with_siaes(self):
def test_with_siae_stats(self):
self.assertEqual(Tender.objects.count(), 2 + 1)
tender_with_siae_1 = Tender.objects.with_siae_stats().filter(id=self.tender_with_siae_1.id).first()
self.assertEqual(tender_with_siae_1.siaes.count(), 6)
self.assertEqual(tender_with_siae_1.siae_count_annotated, 6)
self.assertEqual(tender_with_siae_1.siaes.count(), 7)
self.assertEqual(tender_with_siae_1.siae_count_annotated, 7)
self.assertEqual(tender_with_siae_1.siae_email_send_count_annotated, 4)
self.assertEqual(tender_with_siae_1.siae_email_link_click_count_annotated, 3)
self.assertEqual(tender_with_siae_1.siae_detail_display_count_annotated, 2)
self.assertEqual(tender_with_siae_1.siae_email_link_click_or_detail_display_count_annotated, 2)
self.assertEqual(tender_with_siae_1.siae_detail_contact_click_count_annotated, 1)
self.assertEqual(tender_with_siae_1.siae_detail_cocontracting_click_count_annotated, 1)
self.assertEqual(tender_with_siae_1.siae_detail_contact_click_since_last_seen_date_count_annotated, 1)
tender_with_siae_2 = Tender.objects.with_siae_stats().filter(id=self.tender_with_siae_2.id).first()
self.assertEqual(tender_with_siae_2.siaes.count(), 1)
Expand All @@ -354,6 +363,7 @@ def test_with_siae_stats(self):
self.assertEqual(tender_with_siae_2.siae_detail_display_count_annotated, 1)
self.assertEqual(tender_with_siae_2.siae_email_link_click_or_detail_display_count_annotated, 1)
self.assertEqual(tender_with_siae_2.siae_detail_contact_click_count_annotated, 1)
self.assertEqual(tender_with_siae_2.siae_detail_cocontracting_click_count_annotated, 0)
self.assertEqual(tender_with_siae_2.siae_detail_contact_click_since_last_seen_date_count_annotated, 1)
tender_without_siae = Tender.objects.with_siae_stats().filter(id=self.tender_without_siae.id).first()
self.assertEqual(tender_without_siae.siaes.count(), 0)
Expand All @@ -362,6 +372,7 @@ def test_with_siae_stats(self):
self.assertEqual(tender_without_siae.siae_detail_display_count_annotated, 0)
self.assertEqual(tender_without_siae.siae_email_link_click_or_detail_display_count_annotated, 0)
self.assertEqual(tender_without_siae.siae_detail_contact_click_count_annotated, 0)
self.assertEqual(tender_without_siae.siae_detail_cocontracting_click_count_annotated, 0)
self.assertEqual(tender_without_siae.siae_detail_contact_click_since_last_seen_date_count_annotated, 0)

def test_siae_with_tender_stats(self):
Expand Down

0 comments on commit 0b91b83

Please sign in to comment.