From f7f26b8816486f426899b52ebd0a54bda85a8c28 Mon Sep 17 00:00:00 2001 From: Vincent Porte Date: Mon, 1 Jul 2024 19:06:34 +0200 Subject: [PATCH] feat(stats): page hebdo partenaires fiches pratiques (#695) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Description 🎸 Ajout d'une `WeekArchiveView` basée sur le modèle `ForumStat` 🎸 Ajout en contexte des stats de visiteurs à la période consultée 🎸 Ajout de la liste des `Forum` les plus notés (au moins 2 notes) sur la période consultée ## Type de changement 🎢 Nouvelle fonctionnalité (changement non cassant qui ajoute une fonctionnalité). ### Points d'attention 🦺 factorisation de la génération des stats des visiteurs : utilisés dans 2 vues 🦺 pagination vers les semaines existantes, pas d'accès aux semaines futures 🦺 affichage des forums les plus vus, limités à 15 ### Captures d'écran (optionnel) ![image](https://github.com/gip-inclusion/itou-communaute-django/assets/11419273/18de6014-0e16-4f2a-b88e-eb41582cb194) --- lacommunaute/forum/factories.py | 6 + lacommunaute/forum/models.py | 3 + lacommunaute/forum/tests/tests_model.py | 7 + lacommunaute/stats/factories.py | 3 + .../tests/__snapshots__/tests_views.ambr | 96 +++++++++ lacommunaute/stats/tests/tests_views.py | 186 ++++++++++++++--- lacommunaute/stats/urls.py | 3 +- lacommunaute/stats/views.py | 77 +++++-- .../stats/forum_stat_week_archive.html | 188 ++++++++++++++++++ 9 files changed, 525 insertions(+), 44 deletions(-) create mode 100644 lacommunaute/templates/stats/forum_stat_week_archive.html diff --git a/lacommunaute/forum/factories.py b/lacommunaute/forum/factories.py index 72acfc3bc..a0705584d 100644 --- a/lacommunaute/forum/factories.py +++ b/lacommunaute/forum/factories.py @@ -54,3 +54,9 @@ class ForumRatingFactory(factory.django.DjangoModelFactory): class Meta: model = ForumRating + + @factory.post_generation + def set_created(self, create, extracted, **kwargs): + if extracted: + self.created = extracted + self.save() diff --git a/lacommunaute/forum/models.py b/lacommunaute/forum/models.py index 518302313..d13c21f5c 100644 --- a/lacommunaute/forum/models.py +++ b/lacommunaute/forum/models.py @@ -77,6 +77,9 @@ def is_newsfeed(self): def get_session_rating(self, session_key): return getattr(ForumRating.objects.filter(forum=self, session_id=session_key).first(), "rating", None) + def get_average_rating(self): + return ForumRating.objects.filter(forum=self).aggregate(models.Avg("rating"))["rating__avg"] + class ForumRating(DatedModel): session_id = models.CharField(max_length=40) diff --git a/lacommunaute/forum/tests/tests_model.py b/lacommunaute/forum/tests/tests_model.py index 8e6d8a8a0..fe0fb7f4e 100644 --- a/lacommunaute/forum/tests/tests_model.py +++ b/lacommunaute/forum/tests/tests_model.py @@ -117,3 +117,10 @@ def test_get_session_rating(self): ForumRatingFactory(forum=forum, session_id=forum_rating.session_id, rating=forum_rating.rating + 1) self.assertEqual(forum.get_session_rating(forum_rating.session_id), forum_rating.rating + 1) + + def test_get_average_rating(self): + forum = ForumFactory() + ForumRatingFactory(forum=forum, rating=1) + ForumRatingFactory(forum=forum, rating=5) + + self.assertEqual(forum.get_average_rating(), 3) diff --git a/lacommunaute/stats/factories.py b/lacommunaute/stats/factories.py index b4221b5c1..2beb7fed7 100644 --- a/lacommunaute/stats/factories.py +++ b/lacommunaute/stats/factories.py @@ -36,3 +36,6 @@ class ForumStatFactory(factory.django.DjangoModelFactory): class Meta: model = ForumStat + + class Params: + for_snapshot = factory.Trait(period="week", date=datetime.date(2024, 5, 20)) diff --git a/lacommunaute/stats/tests/__snapshots__/tests_views.ambr b/lacommunaute/stats/tests/__snapshots__/tests_views.ambr index 21d0150f3..81384b099 100644 --- a/lacommunaute/stats/tests/__snapshots__/tests_views.ambr +++ b/lacommunaute/stats/tests/__snapshots__/tests_views.ambr @@ -11,6 +11,102 @@ ''' # --- +# name: TestForumStatWeekArchiveView.test_header_and_breadcrumb[breadcrumb] + ''' + + ''' +# --- +# name: TestForumStatWeekArchiveView.test_header_and_breadcrumb[title-01] + ''' +
+
+
+
+

+ Partenariat Academie France Travail x La communauté de l'inclusion +
+ Statistiques de la semaine du 20 mai 2024 au 26 mai 2024 +

+
+
+
+
+ ''' +# --- +# name: TestForumStatWeekArchiveView.test_most_rated_forums[most_rated_forums] + ''' +
+
+
+

Les 1 fiches pratiques les plus notées sur la période

+ + + + + + + + + + + + + + + + + +
Fiche PratiqueNombres de notations de la semaineNotation moyenne totale
Forum A23,00
+
+
+
+ ''' +# --- +# name: TestForumStatWeekArchiveView.test_most_viewed_forums[most_viewed_forums] + ''' +
+
+
+

Les 2 fiches pratiques les plus lues sur la période

+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Fiche PratiqueVisiteurs uniquesVisiteurs uniques entrantsTemps de lecture total
Forum B1751978 secondes
Forum A1081000 secondes
+
+
+
+ ''' +# --- # name: TestMonthlyVisitorsView.test_navigation[breadcrumb] '''