Skip to content

Commit

Permalink
Corrige le nombre de sessions rapporté par Munin (#6636)
Browse files Browse the repository at this point in the history
Bug introduit par 2b2215e (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
  • Loading branch information
philippemilink authored Sep 1, 2024
1 parent 23c78ec commit 30d805b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
8 changes: 5 additions & 3 deletions django_munin/munin/views.py
Original file line number Diff line number Diff line change
@@ -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(
Expand Down
12 changes: 9 additions & 3 deletions zds/member/views/sessions.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
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
from django.utils.translation import gettext_lazy as _
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,
Expand Down
19 changes: 19 additions & 0 deletions zds/munin/tests.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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")
4 changes: 2 additions & 2 deletions zds/utils/custom_cached_db_backend.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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"
Expand Down

0 comments on commit 30d805b

Please sign in to comment.