Add management commands

This commit is contained in:
Nyymix 2025-04-14 18:52:27 +03:00
parent f4a0edd091
commit ec6acaf8ea
2 changed files with 54 additions and 0 deletions

View file

@ -0,0 +1,21 @@
from django.core.cache import cache
from django.core.management.base import BaseCommand
from gallery.models import Album
class Command(BaseCommand):
help = "Clears photo count and view count caches for all albums"
def handle(self, *args, **options):
albums = Album.objects.only("pk", "name")
cleared = 0
for album in albums:
cache.delete(f'album_{album.pk}_photo_count')
cache.delete(f'album_{album.pk}_photo_views')
cleared += 1
self.stdout.write(self.style.SUCCESS(
f"Cleared cache for {cleared} albums."
))

View file

@ -0,0 +1,33 @@
import json
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)
# Redis-avaimet istuntoja varten (django-redis käyttää 'session:' prefixiä)
keys = redis_conn.keys('session:*')
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:
session_data = redis_conn.get(key)
try:
decoded = session_data.decode("utf-8")
except Exception:
decoded = str(session_data)
self.stdout.write(f"{key.decode('utf-8')}: {decoded}\n")