Update web/core/views.py

This commit is contained in:
Joshua Laymon 2025-08-31 04:04:54 +00:00
parent 03af17b61b
commit 225eb35d96

View File

@ -1021,25 +1021,22 @@ def login_attempts(request):
attempts = LoginAttempt.objects.filter(timestamp__gte=cutoff).order_by("-timestamp") attempts = LoginAttempt.objects.filter(timestamp__gte=cutoff).order_by("-timestamp")
return render(request, "tools/login_attempts.html", {"attempts": attempts}) return render(request, "tools/login_attempts.html", {"attempts": attempts})
@login_required @login_required
@require_POST @require_POST
def clear_history(request): def clear_history(request):
""" """
Clear all 'recents' for the CURRENT user. Clear the current user's history shown on the Search page:
We clear the two app-specific keys AND a few common variants in case - SearchHistory (last 10 searches)
your templates or JS ever used alternates. - ViewedIllustration (recently viewed entries)
Also clears any session keys that might be used as UI caches.
""" """
KEYS = [ # Delete DB-backed recents for this user
"recent_searches", # your apps key for searches SearchHistory.objects.filter(user=request.user).delete()
"recent_entries", # your apps key for viewed entries ViewedIllustration.objects.filter(user=request.user).delete()
# Defensive extras (ignored if unused):
"recent_viewed", "recently_viewed", "recent_results", # (Harmless) clear possible session caches if present
"recentSearches", "recentEntries", for k in ["recent_searches", "recent_entries", "recent_viewed", "recently_viewed"]:
]
cleared = {}
for k in KEYS:
existed = k in request.session
request.session.pop(k, None) request.session.pop(k, None)
cleared[k] = bool(existed)
request.session.modified = True request.session.modified = True
return JsonResponse({"ok": True, "cleared": cleared})
return JsonResponse({"ok": True})