Skip to content

Commit

Permalink
improve the way to calculate the latest activity and improve wording
Browse files Browse the repository at this point in the history
  • Loading branch information
madjid-asa committed Oct 17, 2023
1 parent d916b93 commit dcacd5e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
26 changes: 10 additions & 16 deletions lemarche/siaes/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lemarche/templates/siaes/_card_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<h1 class="h2 mb-2">
{{ siae.name_display }}
<br />
<small>(Dernière activité il y a {{ siae.last_activity_at|timesince }})</small>
<small>(Dernière activité il y a {{ siae.latest_activity_at|timesince }})</small>
</h1>
{% if user.is_authenticated %}
{% if siae.in_user_favorite_list_count %}
Expand Down
9 changes: 8 additions & 1 deletion lemarche/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
"""
Expand Down

0 comments on commit dcacd5e

Please sign in to comment.