diff --git a/lemarche/siaes/models.py b/lemarche/siaes/models.py index 05a478b21..224f7fa2a 100644 --- a/lemarche/siaes/models.py +++ b/lemarche/siaes/models.py @@ -1040,6 +1040,21 @@ def completion_percent(self): score_percent = round(score / total, 2) * 100 return round_by_base(score_percent, base=5) + @property + def latest_activity_at(self): + latest_activity_at = None + users_activity = self.users.annotate( + latest_activity_at=Greatest( + "updated_at", "last_login", "dashboard_last_seen_date", "tender_list_last_seen_date" + ) + ).order_by("-latest_activity_at") + if users_activity: + latest_activity_at = users_activity.first().latest_activity_at + latest_activity_at = self.updated_at if self.updated_at > latest_activity_at else latest_activity_at + else: + latest_activity_at = self.updated_at + return latest_activity_at + def sectors_list_string(self, display_max=3): sectors_name_list = self.sectors.form_filter_queryset().values_list("name", flat=True) if display_max and len(sectors_name_list) > display_max: diff --git a/lemarche/siaes/tests.py b/lemarche/siaes/tests.py index f1c2194e2..8259bdb6f 100644 --- a/lemarche/siaes/tests.py +++ b/lemarche/siaes/tests.py @@ -480,3 +480,22 @@ def setUpTestData(cls): def test_calculate_etablissement_count(self): self.assertEqual(siae_utils.calculate_etablissement_count(self.siae_with_siret_1), 2) + + +class SiaeActivitiesTest(TestCase): + @classmethod + def setUpTestData(cls) -> None: + # cls.users = [UserFactory() for i in range(3)] + cls.siae: Siae = SiaeFactory(siret="12312312312345", is_active=True) + + def test_last_activity_is_updated_at(self): + self.assertEqual(self.siae.updated_at, self.siae.latest_activity_at) + users = [UserFactory() for i in range(3)] + self.siae.users.set(users) + users[0].first_name = "test" + users[0].save() + self.assertNotEqual(self.siae.updated_at, self.siae.latest_activity_at) + self.assertTrue(self.siae.updated_at < self.siae.latest_activity_at) + self.siae.name = "test_siae" + self.siae.save() + self.assertTrue(self.siae.updated_at == self.siae.latest_activity_at) diff --git a/lemarche/templates/siaes/_card_detail.html b/lemarche/templates/siaes/_card_detail.html index ad27a9b79..27705bfc7 100644 --- a/lemarche/templates/siaes/_card_detail.html +++ b/lemarche/templates/siaes/_card_detail.html @@ -16,7 +16,7 @@