From b1d07525bb56dbae0124768a08ac744d0596d18e Mon Sep 17 00:00:00 2001 From: "madjid.asa" Date: Mon, 2 Oct 2023 12:30:14 +0200 Subject: [PATCH 1/4] update date of last activities siae display --- lemarche/siaes/models.py | 26 ++++++++++++++++++++++ lemarche/templates/siaes/_card_detail.html | 2 +- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lemarche/siaes/models.py b/lemarche/siaes/models.py index 05a478b21..1d6c4a74f 100644 --- a/lemarche/siaes/models.py +++ b/lemarche/siaes/models.py @@ -1040,6 +1040,32 @@ def completion_percent(self): score_percent = round(score / total, 2) * 100 return round_by_base(score_percent, base=5) + @property + def last_activity_at(self): + last_activity_at = None + if self.users.exists(): + users_last_activities = self.users.values_list( + "last_login", "updated_at", "dashboard_last_seen_date", "tender_list_last_seen_date" + ) + for index, user_last_activities in enumerate(users_last_activities): + if index == 0: + # set the first date + last_activity_at = user_last_activities[0] + for index1, activity_at in enumerate(user_last_activities): + if index1 > 0: + last_activity_at = ( + activity_at if activity_at and activity_at > last_activity_at else last_activity_at + ) + else: + for activity_at in user_last_activities: + last_activity_at = ( + activity_at if activity_at and activity_at > last_activity_at else last_activity_at + ) + last_activity_at = last_activity_at if last_activity_at > self.updated_at else self.updated_at + else: + last_activity_at = self.updated_at + return last_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/templates/siaes/_card_detail.html b/lemarche/templates/siaes/_card_detail.html index ad27a9b79..3f8bb21d6 100644 --- a/lemarche/templates/siaes/_card_detail.html +++ b/lemarche/templates/siaes/_card_detail.html @@ -16,7 +16,7 @@

{{ siae.name_display }}
- (profil mis à jour il y a {{ siae.updated_at|timesince }}) + (Dernière activité il y a {{ siae.last_activity_at|timesince }})

{% if user.is_authenticated %} {% if siae.in_user_favorite_list_count %} From 617df36235933a5067262e5ab3a390e48261affe Mon Sep 17 00:00:00 2001 From: "madjid.asa" Date: Mon, 2 Oct 2023 12:38:27 +0200 Subject: [PATCH 2/4] improve method of last-activity --- lemarche/siaes/models.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/lemarche/siaes/models.py b/lemarche/siaes/models.py index 1d6c4a74f..c4250d06e 100644 --- a/lemarche/siaes/models.py +++ b/lemarche/siaes/models.py @@ -1045,19 +1045,14 @@ def last_activity_at(self): last_activity_at = None if self.users.exists(): users_last_activities = self.users.values_list( - "last_login", "updated_at", "dashboard_last_seen_date", "tender_list_last_seen_date" + "updated_at", "last_login", "dashboard_last_seen_date", "tender_list_last_seen_date" ) for index, user_last_activities in enumerate(users_last_activities): if index == 0: # set the first date last_activity_at = user_last_activities[0] - for index1, activity_at in enumerate(user_last_activities): - if index1 > 0: - last_activity_at = ( - activity_at if activity_at and activity_at > last_activity_at else last_activity_at - ) - else: - for activity_at in user_last_activities: + start_index = 1 if index == 0 else 0 + for _, activity_at in enumerate(user_last_activities, start=start_index): last_activity_at = ( activity_at if activity_at and activity_at > last_activity_at else last_activity_at ) From 25139787938a702c265b044033fde183dab19870 Mon Sep 17 00:00:00 2001 From: "madjid.asa" Date: Tue, 3 Oct 2023 12:25:53 +0200 Subject: [PATCH 3/4] improve the way to calculate the latest activity and improve wording --- lemarche/siaes/models.py | 26 +++++++++------------- lemarche/templates/siaes/_card_detail.html | 2 +- lemarche/users/models.py | 9 +++++++- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/lemarche/siaes/models.py b/lemarche/siaes/models.py index c4250d06e..224f7fa2a 100644 --- a/lemarche/siaes/models.py +++ b/lemarche/siaes/models.py @@ -1041,25 +1041,19 @@ def completion_percent(self): return round_by_base(score_percent, base=5) @property - def last_activity_at(self): - last_activity_at = None - if self.users.exists(): - users_last_activities = self.users.values_list( + 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" ) - for index, user_last_activities in enumerate(users_last_activities): - if index == 0: - # set the first date - last_activity_at = user_last_activities[0] - start_index = 1 if index == 0 else 0 - for _, activity_at in enumerate(user_last_activities, start=start_index): - last_activity_at = ( - activity_at if activity_at and activity_at > last_activity_at else last_activity_at - ) - last_activity_at = last_activity_at if last_activity_at > self.updated_at else self.updated_at + ).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: - last_activity_at = self.updated_at - return last_activity_at + 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) diff --git a/lemarche/templates/siaes/_card_detail.html b/lemarche/templates/siaes/_card_detail.html index 3f8bb21d6..27705bfc7 100644 --- a/lemarche/templates/siaes/_card_detail.html +++ b/lemarche/templates/siaes/_card_detail.html @@ -16,7 +16,7 @@

{{ siae.name_display }}
- (Dernière activité il y a {{ siae.last_activity_at|timesince }}) + (Dernière activité il y a {{ siae.latest_activity_at|timesince }})

{% if user.is_authenticated %} {% if siae.in_user_favorite_list_count %} diff --git a/lemarche/users/models.py b/lemarche/users/models.py index ce623b56b..4018127b1 100644 --- a/lemarche/users/models.py +++ b/lemarche/users/models.py @@ -4,7 +4,7 @@ from django.contrib.contenttypes.fields import GenericRelation from django.db import models from django.db.models import Count -from django.db.models.functions import Lower +from django.db.models.functions import Greatest, Lower from django.db.models.signals import post_save from django.dispatch import receiver from django.forms.models import model_to_dict @@ -44,6 +44,13 @@ def with_siae_stats(self): def with_tender_stats(self): return self.prefetch_related("tenders").annotate(tender_count=Count("tenders", distinct=True)) + def with_latest_activities(self): + return self.annotate( + latest_activity_at=Greatest( + "updated_at", "last_login", "dashboard_last_seen_date", "tender_list_last_seen_date" + ) + ) + class UserManager(BaseUserManager): """ From 86b03f18ad419d1b3782bc9d224be80e6288e367 Mon Sep 17 00:00:00 2001 From: "madjid.asa" Date: Tue, 17 Oct 2023 14:40:01 +0200 Subject: [PATCH 4/4] add tests --- lemarche/siaes/tests.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) 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)