Skip to content

Commit

Permalink
New Siae method set_super_label
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Dec 7, 2023
1 parent 92d0f51 commit dbd49eb
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
17 changes: 16 additions & 1 deletion lemarche/siaes/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
32 changes: 32 additions & 0 deletions lemarche/siaes/tests.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit dbd49eb

Please sign in to comment.