40 lines
No EOL
1.4 KiB
Python
40 lines
No EOL
1.4 KiB
Python
import pickle
|
|
import zlib
|
|
|
|
from django.conf import settings
|
|
from django.core.management.base import BaseCommand
|
|
from django_redis import get_redis_connection
|
|
|
|
|
|
class Command(BaseCommand):
|
|
help = "List active Django sessions from Redis"
|
|
|
|
def add_arguments(self, parser):
|
|
parser.add_argument('--details', action='store_true', help='Print session data')
|
|
|
|
def handle(self, *args, **options):
|
|
cache_prefix = getattr(settings, 'SESSION_CACHE_ALIAS', 'default')
|
|
redis_conn = get_redis_connection(cache_prefix)
|
|
|
|
pattern = "*django.contrib.sessions.cache*"
|
|
keys = redis_conn.keys(pattern)
|
|
count = len(keys)
|
|
|
|
self.stdout.write(f"Aktiivisia sessioita Redisissä: {count}")
|
|
|
|
if options['details'] and count > 0:
|
|
self.stdout.write("\nSession ID:t ja sisällöt:\n" + "-"*40)
|
|
for key in keys:
|
|
raw = redis_conn.get(key)
|
|
try:
|
|
# Purkaa mahdollisesti zlib-pakatun ja pickle-muotoisen session
|
|
session_data = pickle.loads(zlib.decompress(raw))
|
|
except Exception:
|
|
session_data = "<ei voitu purkaa>"
|
|
|
|
try:
|
|
key_str = key.decode("utf-8")
|
|
except Exception:
|
|
key_str = str(key)
|
|
|
|
self.stdout.write(f"{key_str}: {session_data}\n") |