Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mise à jours de la date de dernière activité des structures #930

Merged
merged 4 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lemarche/siaes/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
19 changes: 19 additions & 0 deletions lemarche/siaes/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Un petit commentaire qui est resté là.

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)
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>(profil mis à jour il y a {{ siae.updated_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