From 30d805bdb450b908165d588ba536085e4a48d14c Mon Sep 17 00:00:00 2001 From: "Ph. SW." Date: Sun, 1 Sep 2024 12:30:28 +0200 Subject: [PATCH] =?UTF-8?q?Corrige=20le=20nombre=20de=20sessions=20rapport?= =?UTF-8?q?=C3=A9=20par=20Munin=20(#6636)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bug introduit par 2b2215e02db3233d2360060e1ab1243d5801f16c (PR #6021), où le backend pour gérer les sessions passe de django.contrib.sessions.backends.cached_db à zds.utils.custom_cached_db_backend, ce qui change la table utilisée pour stocker les sessions et le modèle à utiliser pour les manipuler. La vue qui compte le nombre de sessions pour Munin n'a pas été mise à jour pour utiliser le nouveau modèle. - Utilise les classes de sessions définies dans les paramètres, plutôt que forcément django.contrib.sessions - Ajoute un test pour vérifier que le nombre de sessions rapporté par Munin est correct - Renomme l'import DBStore en CachedDBStore pour éviter une confusion sur quel backend est utilisé pour stocker les sessions --- django_munin/munin/views.py | 8 +++++--- zds/member/views/sessions.py | 12 +++++++++--- zds/munin/tests.py | 19 +++++++++++++++++++ zds/utils/custom_cached_db_backend.py | 4 ++-- 4 files changed, 35 insertions(+), 8 deletions(-) diff --git a/django_munin/munin/views.py b/django_munin/munin/views.py index e5982aac7b..e92895db08 100644 --- a/django_munin/munin/views.py +++ b/django_munin/munin/views.py @@ -1,13 +1,15 @@ -from datetime import datetime -from datetime import timedelta +from datetime import datetime, timedelta +from importlib import import_module import time -from django.contrib.sessions.models import Session + +from django.conf import settings from django.contrib.auth import get_user_model from .helpers import muninview from .models import Test User = get_user_model() +Session = import_module(settings.SESSION_ENGINE).CustomSession @muninview( diff --git a/zds/member/views/sessions.py b/zds/member/views/sessions.py index 3059ef370e..0f6c50c32f 100644 --- a/zds/member/views/sessions.py +++ b/zds/member/views/sessions.py @@ -1,3 +1,6 @@ +from importlib import import_module + +from django.conf import settings from django.contrib.auth.mixins import LoginRequiredMixin from django.shortcuts import redirect from django.urls import reverse @@ -5,21 +8,24 @@ from django.views.generic import View from zds.member.utils import get_geo_location_from_ip, get_info_from_user_agent -from zds.utils.custom_cached_db_backend import CustomSession, SessionStore from zds.utils.paginator import ZdSPagingListView +Session = import_module(settings.SESSION_ENGINE).CustomSession +SessionStore = import_module(settings.SESSION_ENGINE).SessionStore + + class ListSessions(LoginRequiredMixin, ZdSPagingListView): """List the user's sessions with useful information (user agent, IP address, geolocation and last visit).""" - model = CustomSession + model = Session context_object_name = "sessions" template_name = "member/settings/sessions.html" paginate_by = 10 def get_context_data(self, **kwargs): self.object_list = [] - for session in CustomSession.objects.filter(account_id=self.request.user.pk).iterator(): + for session in Session.objects.filter(account_id=self.request.user.pk).iterator(): data = session.get_decoded() session_context = { "session_key": session.session_key, diff --git a/zds/munin/tests.py b/zds/munin/tests.py index 4c8b67bf77..8c73a2f2ec 100644 --- a/zds/munin/tests.py +++ b/zds/munin/tests.py @@ -1,6 +1,8 @@ from django.test import TestCase from django.urls import reverse +from zds.member.tests.factories import ProfileFactory + class Munin(TestCase): def setUp(self): @@ -25,3 +27,20 @@ def test_routes(self): url = reverse(route) response = self.client.get(url) self.assertEqual(response.status_code, 200) + + def test_sessions(self): + """Makes sure the sessions are correctly counted in Munin data.""" + response = self.client.get(reverse("munin:base:active-sessions")) + self.assertEqual(response.content.decode(), "sessions 0") + + response = self.client.get(reverse("munin:base:total-sessions")) + self.assertEqual(response.content.decode(), "sessions 0") + + profile = ProfileFactory() + self.client.force_login(profile.user) + + response = self.client.get(reverse("munin:base:active-sessions")) + self.assertEqual(response.content.decode(), "sessions 1") + + response = self.client.get(reverse("munin:base:total-sessions")) + self.assertEqual(response.content.decode(), "sessions 1") diff --git a/zds/utils/custom_cached_db_backend.py b/zds/utils/custom_cached_db_backend.py index cc61cf3e75..e3f1c5b174 100644 --- a/zds/utils/custom_cached_db_backend.py +++ b/zds/utils/custom_cached_db_backend.py @@ -1,4 +1,4 @@ -from django.contrib.sessions.backends.cached_db import SessionStore as DBStore +from django.contrib.sessions.backends.cached_db import SessionStore as CachedDBStore from django.contrib.sessions.base_session import AbstractBaseSession from django.db import models @@ -15,7 +15,7 @@ def get_session_store_class(cls): return SessionStore -class SessionStore(DBStore): +class SessionStore(CachedDBStore): """Custom session store for the custom session model.""" cache_key_prefix = "zds.utils.custom_cached_db_backend"