Fix list_sessions command
This commit is contained in:
parent
97ca2964cb
commit
e1cbc3a7fb
1 changed files with 14 additions and 7 deletions
|
@ -1,4 +1,5 @@
|
||||||
import json
|
import pickle
|
||||||
|
import zlib
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core.management.base import BaseCommand
|
from django.core.management.base import BaseCommand
|
||||||
|
@ -15,8 +16,8 @@ class Command(BaseCommand):
|
||||||
cache_prefix = getattr(settings, 'SESSION_CACHE_ALIAS', 'default')
|
cache_prefix = getattr(settings, 'SESSION_CACHE_ALIAS', 'default')
|
||||||
redis_conn = get_redis_connection(cache_prefix)
|
redis_conn = get_redis_connection(cache_prefix)
|
||||||
|
|
||||||
# Redis-avaimet istuntoja varten (django-redis käyttää 'session:' prefixiä)
|
pattern = "*django.contrib.sessions.cache*"
|
||||||
keys = redis_conn.keys('session:*')
|
keys = redis_conn.keys(pattern)
|
||||||
count = len(keys)
|
count = len(keys)
|
||||||
|
|
||||||
self.stdout.write(f"Aktiivisia sessioita Redisissä: {count}")
|
self.stdout.write(f"Aktiivisia sessioita Redisissä: {count}")
|
||||||
|
@ -24,10 +25,16 @@ class Command(BaseCommand):
|
||||||
if options['details'] and count > 0:
|
if options['details'] and count > 0:
|
||||||
self.stdout.write("\nSession ID:t ja sisällöt:\n" + "-"*40)
|
self.stdout.write("\nSession ID:t ja sisällöt:\n" + "-"*40)
|
||||||
for key in keys:
|
for key in keys:
|
||||||
session_data = redis_conn.get(key)
|
raw = redis_conn.get(key)
|
||||||
try:
|
try:
|
||||||
decoded = session_data.decode("utf-8")
|
# Purkaa mahdollisesti zlib-pakatun ja pickle-muotoisen session
|
||||||
|
session_data = pickle.loads(zlib.decompress(raw))
|
||||||
except Exception:
|
except Exception:
|
||||||
decoded = str(session_data)
|
session_data = "<ei voitu purkaa>"
|
||||||
|
|
||||||
self.stdout.write(f"{key.decode('utf-8')}: {decoded}\n")
|
try:
|
||||||
|
key_str = key.decode("utf-8")
|
||||||
|
except Exception:
|
||||||
|
key_str = str(key)
|
||||||
|
|
||||||
|
self.stdout.write(f"{key_str}: {session_data}\n")
|
Loading…
Add table
Reference in a new issue