diff --git a/lemarche/siaes/models.py b/lemarche/siaes/models.py index 98c7c3660..bd6fa5b7f 100644 --- a/lemarche/siaes/models.py +++ b/lemarche/siaes/models.py @@ -1083,7 +1083,11 @@ def source_display(self): @property def super_badge_calculated(self): - if (self.user_count >= 1) and (self.completion_rate >= 80) and (self.tender_email_send_count >= 1): + if ( + (self.user_count >= 1) + and (self.completion_rate and self.completion_rate >= 80) + and (self.tender_email_send_count >= 1) + ): tender_view_rate = round(100 * self.tender_email_link_click_count / self.tender_email_send_count) tender_interested_rate = round(100 * self.tender_detail_contact_click_count / self.tender_email_send_count) if (tender_view_rate >= 40) or (tender_interested_rate >= 20): @@ -1159,6 +1163,17 @@ def siae_user_requests_pending_count(self): def get_absolute_url(self): return reverse("siae:detail", kwargs={"slug": self.slug}) + def set_super_badge(self): + UPDATE_FIELDS = ["super_badge"] + siae_super_badge_current_value = self.super_badge + self.super_badge = self.super_badge_calculated + + if self.super_badge != siae_super_badge_current_value: + self.super_badge_last_updated = timezone.now() + UPDATE_FIELDS.append("super_badge_last_updated") + + self.save(update_fields=UPDATE_FIELDS) + @receiver(post_save, sender=Siae) def siae_post_save(sender, instance, **kwargs): diff --git a/lemarche/siaes/tests.py b/lemarche/siaes/tests.py index e28a96250..95f6e0051 100644 --- a/lemarche/siaes/tests.py +++ b/lemarche/siaes/tests.py @@ -1,4 +1,5 @@ from django.test import TestCase +from django.utils import timezone from lemarche.labels.factories import LabelFactory from lemarche.networks.factories import NetworkFactory @@ -365,6 +366,37 @@ def test_update_last_updated_fields(self): # siae = Siae.objects.get(id=siae.id) # we need to fetch it again to make sure # self.assertNotEqual(siae.coords, None) + def test_set_super_badge(self): + # None -> True + siae_super_badge_1 = SiaeFactory( + user_count=1, + completion_rate=80, + tender_email_send_count=10, + tender_email_link_click_count=4, + tender_detail_contact_click_count=1, + ) + self.assertIsNone(siae_super_badge_1.super_badge) + self.assertIsNone(siae_super_badge_1.super_badge_last_updated) + siae_super_badge_1.set_super_badge() + self.assertTrue(siae_super_badge_1.super_badge) + self.assertIsNotNone(siae_super_badge_1.super_badge_last_updated) + # True -> True + siae_super_badge_2 = SiaeFactory( + user_count=1, + completion_rate=80, + tender_email_send_count=10, + tender_email_link_click_count=3, + tender_detail_contact_click_count=3, + super_badge=True, + super_badge_last_updated=timezone.now(), + ) + self.assertTrue(siae_super_badge_2.super_badge) + self.assertIsNotNone(siae_super_badge_2.super_badge_last_updated) + siae_super_badge_last_updated_current_value = siae_super_badge_2.super_badge_last_updated + siae_super_badge_2.set_super_badge() + self.assertTrue(siae_super_badge_2.super_badge) + self.assertEqual(siae_super_badge_2.super_badge_last_updated, siae_super_badge_last_updated_current_value) + class SiaeModelQuerysetTest(TestCase): @classmethod