diff --git a/lacommunaute/pages/tests/tests.py b/lacommunaute/pages/tests/tests.py index 18784374e..b064a1913 100644 --- a/lacommunaute/pages/tests/tests.py +++ b/lacommunaute/pages/tests/tests.py @@ -1,13 +1,18 @@ from django.test import TestCase from django.urls import reverse from django.utils import timezone +from django.utils.dateformat import format +from django.utils.timezone import localdate +from faker import Faker from machina.core.loading import get_class from lacommunaute.forum_stats.enums import Period from lacommunaute.forum_stats.factories import StatFactory from lacommunaute.users.factories import UserFactory +from lacommunaute.utils.math import percent +faker = Faker() assign_perm = get_class("forum_permission.shortcuts", "assign_perm") @@ -41,6 +46,48 @@ def test_context_data(self): self.assertNotIn(undesired_period_stat.date.strftime("%Y-%m-%d"), response.context["stats"]["date"]) self.assertNotIn(undesired_date_stat.date.strftime("%Y-%m-%d"), response.context["stats"]["date"]) + def test_month_datas_in_context(self): + today = localdate() + url = reverse("pages:statistiques") + + # no data + response = self.client.get(url) + self.assertEqual(response.status_code, 200) + self.assertEqual(response.context["period"], None) + self.assertEqual(response.context["nb_uniq_visitors"], 0) + self.assertEqual(response.context["nb_uniq_active_visitors"], 0) + self.assertEqual(response.context["nb_uniq_engaged_visitors"], 0) + self.assertEqual(response.context["activation_percent"], 0) + self.assertEqual(response.context["engagement_percent"], 0) + + # undesired data + StatFactory(name="nb_uniq_engaged_visitors", period=Period.DAY, date=today) + StatFactory(name=faker.word(), period=Period.MONTH, date=today) + response = self.client.get(url) + self.assertEqual(response.status_code, 200) + self.assertEqual(response.context["period"], None) + self.assertEqual(response.context["nb_uniq_visitors"], 0) + self.assertEqual(response.context["nb_uniq_active_visitors"], 0) + self.assertEqual(response.context["nb_uniq_engaged_visitors"], 0) + self.assertEqual(response.context["activation_percent"], 0) + self.assertEqual(response.context["engagement_percent"], 0) + + uniq_visitors = StatFactory(name="nb_uniq_visitors", period=Period.MONTH, date=today) + uniq_active_visitors = StatFactory(name="nb_uniq_active_visitors", period=Period.MONTH, date=today) + uniq_engaged_visitors = StatFactory(name="nb_uniq_engaged_visitors", period=Period.MONTH, date=today) + response = self.client.get(url) + self.assertEqual(response.status_code, 200) + self.assertEqual(response.context["period"], format(today, "F Y")) + self.assertEqual(response.context["nb_uniq_visitors"], uniq_visitors.value) + self.assertEqual(response.context["nb_uniq_active_visitors"], uniq_active_visitors.value) + self.assertEqual(response.context["nb_uniq_engaged_visitors"], uniq_engaged_visitors.value) + self.assertEqual( + response.context["activation_percent"], percent(uniq_active_visitors.value, uniq_visitors.value) + ) + self.assertEqual( + response.context["engagement_percent"], percent(uniq_engaged_visitors.value, uniq_active_visitors.value) + ) + class LandingPagesListViewTest(TestCase): def test_context_data(self): diff --git a/lacommunaute/pages/views.py b/lacommunaute/pages/views.py index 2f66ecc06..6d51590d8 100644 --- a/lacommunaute/pages/views.py +++ b/lacommunaute/pages/views.py @@ -6,6 +6,7 @@ from django.db.models.functions import Cast from django.shortcuts import render from django.utils import timezone +from django.utils.dateformat import format from django.views.generic.base import TemplateView from lacommunaute.forum.enums import Kind as ForumKind @@ -13,6 +14,7 @@ from lacommunaute.forum_conversation.models import Topic from lacommunaute.forum_stats.models import Stat from lacommunaute.utils.json import extract_values_in_list +from lacommunaute.utils.math import percent logger = logging.getLogger(__name__) @@ -25,6 +27,30 @@ def contact(request): class StatistiquesPageView(TemplateView): template_name = "pages/statistiques.html" + def get_funnel_data(self): + qs = Stat.objects.current_month_datas() + + stats = { + "period": None, + "nb_uniq_visitors": 0, + "nb_uniq_active_visitors": 0, + "nb_uniq_engaged_visitors": 0, + } + + if qs.filter(name="nb_uniq_visitors").exists(): + stats["period"] = format(qs.get(name="nb_uniq_visitors")["date"], "F Y") + stats["nb_uniq_visitors"] = qs.get(name="nb_uniq_visitors")["value"] + + if qs.filter(name="nb_uniq_active_visitors").exists(): + stats["nb_uniq_active_visitors"] = qs.get(name="nb_uniq_active_visitors")["value"] + + if qs.filter(name="nb_uniq_engaged_visitors").exists(): + stats["nb_uniq_engaged_visitors"] = qs.get(name="nb_uniq_engaged_visitors")["value"] + + stats["activation_percent"] = percent(stats["nb_uniq_active_visitors"], stats["nb_uniq_visitors"]) + stats["engagement_percent"] = percent(stats["nb_uniq_engaged_visitors"], stats["nb_uniq_active_visitors"]) + return stats + def get_context_data(self, **kwargs): indicator_names = [ "nb_uniq_engaged_visitors", @@ -40,6 +66,7 @@ def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context["stats"] = extract_values_in_list(datas, indicator_names) + context = {**context, **self.get_funnel_data()} return context diff --git a/lacommunaute/templates/pages/statistiques.html b/lacommunaute/templates/pages/statistiques.html index a598386ce..0fca79a9d 100644 --- a/lacommunaute/templates/pages/statistiques.html +++ b/lacommunaute/templates/pages/statistiques.html @@ -27,36 +27,21 @@