35955-vm/core/middleware.py
Flatlogic Bot 7be9310a96 v1.1
2025-11-23 01:09:28 +00:00

26 lines
1.0 KiB
Python

from datetime import datetime
from django.conf import settings
from django.utils import timezone
class UndoSessionCleanup:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
undo_data = request.session.get('undo_data')
if undo_data and 'timestamp' in undo_data:
try:
undo_timestamp = datetime.fromisoformat(undo_data['timestamp'])
# Ensure timestamp is timezone-aware for comparison
if timezone.is_naive(undo_timestamp):
undo_timestamp = timezone.make_aware(undo_timestamp, timezone.get_default_timezone())
if (timezone.now() - undo_timestamp).total_seconds() > settings.UNDO_TIMEOUT:
del request.session['undo_data']
except (ValueError, TypeError):
# If timestamp is invalid, remove it
del request.session['undo_data']
response = self.get_response(request)
return response