diff --git a/lacommunaute/stats/tests/tests_views.py b/lacommunaute/stats/tests/tests_views.py index ee6b0f1d..26bed60e 100644 --- a/lacommunaute/stats/tests/tests_views.py +++ b/lacommunaute/stats/tests/tests_views.py @@ -10,7 +10,7 @@ from faker import Faker from freezegun import freeze_time from machina.core.loading import get_class -from pytest_django.asserts import assertContains, assertNotContains +from pytest_django.asserts import assertContains from lacommunaute.forum.factories import CategoryForumFactory, ForumFactory, ForumRatingFactory from lacommunaute.stats.enums import Period @@ -306,121 +306,3 @@ def test_num_queries(self, client, db, document_stats_setup, django_assert_num_q expected_queries_in_view = 1 with django_assert_num_queries(expected_queries_in_view + django_session_num_of_queries): client.get(reverse("stats:document_stats")) - - -class TestForumStatWeekArchiveView: - def get_url_from_date(self, date): - return reverse( - "stats:forum_stat_week_archive", kwargs={"year": date.strftime("%Y"), "week": date.strftime("%W")} - ) - - def test_header_and_breadcrumb(self, client, db, snapshot): - response = client.get(self.get_url_from_date(ForumStatFactory(for_snapshot=True).date)) - assert response.status_code == 200 - assert str(parse_response_to_soup(response, selector=".s-title-01")) == snapshot(name="title-01") - assert str(parse_response_to_soup(response, selector=".c-breadcrumb")) == snapshot(name="breadcrumb") - - def test_navigation(self, client, db): - weeks = [date.today() - relativedelta(weeks=i) for i in range(15, 10, -1)] - for week in weeks[1:4]: - ForumStatFactory(date=week, for_snapshot=True) - - test_cases = [ - {"test_week": weeks[1], "not_contains": [weeks[0]], "contains": [weeks[2]]}, - {"test_week": weeks[2], "not_contains": [], "contains": [weeks[1], weeks[3]]}, - {"test_week": weeks[3], "not_contains": [weeks[4]], "contains": [weeks[2]]}, - ] - - for test_case in test_cases: - response = client.get(self.get_url_from_date(test_case["test_week"])) - for week in test_case["not_contains"]: - assertNotContains(response, self.get_url_from_date(week)) - for week in test_case["contains"]: - assertContains(response, self.get_url_from_date(week)) - - # out of bound - for week in [weeks[0], weeks[4]]: - response = client.get(self.get_url_from_date(week)) - assert response.status_code == 404 - - def test_most_viewed_forums(self, client, db, snapshot): - forums_stats = [ - ForumStatFactory(for_snapshot=True, visits=10, entry_visits=8, time_spent=1000, forum__name="Forum A"), - ForumStatFactory(for_snapshot=True, visits=17, entry_visits=5, time_spent=1978, forum__name="Forum B"), - ] - - response = client.get(self.get_url_from_date(forums_stats[0].date)) - assert response.status_code == 200 - assert str( - parse_response_to_soup( - response, selector="#most_viewed", replace_in_href=[fs.forum for fs in forums_stats] - ) - ) == snapshot(name="most_viewed_forums") - - def test_paginated_most_viewed_forums(self, client, db): - ForumStatFactory.create_batch(16, for_snapshot=True) - response = client.get(reverse("stats:forum_stat_week_archive", kwargs={"year": 2024, "week": 21})) - assert response.status_code == 200 - assert len(response.context_data["forum_stats"]) == 15 - - def test_most_rated_forums(self, client, db, snapshot): - fs = ForumStatFactory(for_snapshot=True, forum__name="Forum A") - - # rating within range - ForumRatingFactory.create_batch(2, rating=5, forum=fs.forum, set_created=fs.date) - # rating out of range - ForumRatingFactory.create_batch(2, rating=1, forum=fs.forum) - - # undesired forum - ForumFactory() - - # undesired rating - ForumRatingFactory(rating=4) - - response = client.get(self.get_url_from_date(fs.date)) - assert response.status_code == 200 - assert str(parse_response_to_soup(response, selector="#most_rated")) == snapshot(name="most_rated_forums") - - def test_visitors(self, client, db, snapshot): - fs = ForumStatFactory(for_snapshot=True) - - # relevant - relevant_dates = [ - fs.date, - fs.date + relativedelta(days=6), - fs.date + relativedelta(days=6) - relativedelta(days=89), - ] - visitors = [10, 11, 12] - for stat_date, visitor_count in zip(relevant_dates, visitors): - StatFactory(date=stat_date, name="nb_uniq_visitors", value=visitor_count) - - # undesired - for stat_date in [fs.date + relativedelta(weeks=1), fs.date + relativedelta(days=6) - relativedelta(days=90)]: - StatFactory(date=stat_date, name="nb_uniq_visitors", value=99) - - response = client.get(self.get_url_from_date(fs.date)) - assert response.status_code == 200 - expected_stats = { - "date": ["2024-02-27", "2024-05-20", "2024-05-26"], - "nb_uniq_visitors": [12, 10, 11], - "nb_uniq_engaged_visitors": [], - } - assert response.context_data["stats"] == expected_stats - - -@pytest.mark.parametrize( - "forum_stats,status_code", - [ - (lambda: None, 404), - (lambda: [ForumStatFactory(for_snapshot=True), ForumStatFactory(for_snapshot_older=True)], 302), - ], -) -def test_redirect_to_latest_weekly_stats(client, db, forum_stats, status_code): - forum_stats = forum_stats() - response = client.get(reverse("stats:redirect_to_latest_weekly_stats")) - assert response.status_code == status_code - if forum_stats: - assert response.url == reverse( - "stats:forum_stat_week_archive", - kwargs={"year": forum_stats[0].date.year, "week": forum_stats[0].date.strftime("%W")}, - ) diff --git a/lacommunaute/stats/urls.py b/lacommunaute/stats/urls.py index a377ddbd..e73bc6df 100644 --- a/lacommunaute/stats/urls.py +++ b/lacommunaute/stats/urls.py @@ -3,10 +3,8 @@ from lacommunaute.stats.views import ( DailyDSPView, DocumentStatsView, - ForumStatWeekArchiveView, MonthlyVisitorsView, StatistiquesPageView, - redirect_to_latest_weekly_stats, ) @@ -16,7 +14,5 @@ path("", StatistiquesPageView.as_view(), name="statistiques"), path("monthly-visitors/", MonthlyVisitorsView.as_view(), name="monthly_visitors"), path("dsp/", DailyDSPView.as_view(), name="dsp"), - path("weekly///", ForumStatWeekArchiveView.as_view(), name="forum_stat_week_archive"), - path("weekly/", redirect_to_latest_weekly_stats, name="redirect_to_latest_weekly_stats"), path("documents/", DocumentStatsView.as_view(), name="document_stats"), ] diff --git a/lacommunaute/stats/views.py b/lacommunaute/stats/views.py index 7b11682e..3b1fc532 100644 --- a/lacommunaute/stats/views.py +++ b/lacommunaute/stats/views.py @@ -1,19 +1,16 @@ -import datetime import logging from dateutil.relativedelta import relativedelta -from django.db.models import Avg, CharField, Count, OuterRef, Q, Subquery, Sum +from django.db.models import Avg, CharField, Count, OuterRef, Subquery, Sum from django.db.models.functions import Cast -from django.shortcuts import redirect, render -from django.urls import reverse +from django.shortcuts import render from django.utils.dateformat import format from django.utils.timezone import localdate from django.views import View from django.views.generic.base import TemplateView -from django.views.generic.dates import WeekArchiveView from lacommunaute.forum.models import Forum, ForumRating -from lacommunaute.stats.models import ForumStat, Stat +from lacommunaute.stats.models import Stat from lacommunaute.surveys.models import DSP from lacommunaute.utils.json import extract_values_in_list from lacommunaute.utils.math import percent @@ -126,45 +123,6 @@ class DailyDSPView(BaseDetailStatsView): months = 3 -class ForumStatWeekArchiveView(WeekArchiveView): - template_name = "stats/forum_stat_week_archive.html" - date_field = "date" - queryset = ForumStat.objects.filter(period="week").select_related("forum") - week_format = "%W" - make_object_list = True - context_object_name = "forum_stats" - ordering = ["date", "-visits"] - paginate_by = 15 - - def get_dates_of_the_week(self): - start_date = datetime.date(self.get_year(), 1, 1) + datetime.timedelta(weeks=self.get_week() - 1) - return start_date, start_date + datetime.timedelta(days=6) - - def get_most_rated_forums(self, start_date, end_date): - return ( - Forum.objects.annotate(avg_rating=Avg("forumrating__rating")) - .filter(avg_rating__isnull=False) - .annotate( - rating_count=Count( - "forumrating", - filter=Q( - forumrating__created__gte=start_date, forumrating__created__lt=end_date + relativedelta(days=1) - ), - ) - ) - .filter(rating_count__gt=1) - .order_by("-rating_count", "avg_rating", "id") - ) - - def get_context_data(self, **kwargs): - context = super().get_context_data(**kwargs) - start_date, end_date = self.get_dates_of_the_week() - context["end_date"] = end_date - context["stats"] = get_daily_visits_stats(from_date=end_date - relativedelta(days=89), to_date=end_date) - context["rated_forums"] = self.get_most_rated_forums(start_date, end_date) - return context - - class DocumentStatsView(View): def get_objects_with_stats_and_ratings(self): objects = ( @@ -207,17 +165,3 @@ def get(self, request, *args, **kwargs): "sort_fields": self.get_sort_fields(), }, ) - - -def redirect_to_latest_weekly_stats(request): - latest_weekly_stat = ForumStat.objects.filter(period="week").order_by("-date").first() - - if latest_weekly_stat: - return redirect( - reverse( - "stats:forum_stat_week_archive", - kwargs={"year": latest_weekly_stat.date.year, "week": latest_weekly_stat.date.strftime("%W")}, - ) - ) - - return render(request, "404.html", status=404)