From dcacd5e033bf8414b556c058250d17d0cbf30a84 Mon Sep 17 00:00:00 2001 From: "madjid.asa" Date: Tue, 3 Oct 2023 12:25:53 +0200 Subject: [PATCH] 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 4293c6b65..bf9b284d1 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=5): 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): """