Optimize user log

This commit is contained in:
cuom1999 2024-01-13 18:52:11 -06:00
parent ee4a947385
commit e09008bcb7

View file

@ -1,5 +1,6 @@
from django.utils.timezone import now
from django.conf import settings
from django.core.cache import cache
from judge.models import Profile
@ -15,11 +16,14 @@ class LogUserAccessMiddleware(object):
hasattr(request, "user")
and request.user.is_authenticated
and not getattr(request, "no_profile_update", False)
and not cache.get(f"user_log_update_{request.user.id}")
):
updates = {"last_access": now()}
# Decided on using REMOTE_ADDR as nginx will translate it to the external IP that hits it.
if request.META.get(settings.META_REMOTE_ADDRESS_KEY):
updates["ip"] = request.META.get(settings.META_REMOTE_ADDRESS_KEY)
Profile.objects.filter(user_id=request.user.pk).update(**updates)
cache.set(f"user_log_update_{request.user.id}", True, 120)
print("UPDATE", updates)
return response