Optimize user log
This commit is contained in:
parent
ee4a947385
commit
e09008bcb7
1 changed files with 4 additions and 0 deletions
|
@ -1,5 +1,6 @@
|
||||||
from django.utils.timezone import now
|
from django.utils.timezone import now
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
from django.core.cache import cache
|
||||||
|
|
||||||
from judge.models import Profile
|
from judge.models import Profile
|
||||||
|
|
||||||
|
@ -15,11 +16,14 @@ class LogUserAccessMiddleware(object):
|
||||||
hasattr(request, "user")
|
hasattr(request, "user")
|
||||||
and request.user.is_authenticated
|
and request.user.is_authenticated
|
||||||
and not getattr(request, "no_profile_update", False)
|
and not getattr(request, "no_profile_update", False)
|
||||||
|
and not cache.get(f"user_log_update_{request.user.id}")
|
||||||
):
|
):
|
||||||
updates = {"last_access": now()}
|
updates = {"last_access": now()}
|
||||||
# Decided on using REMOTE_ADDR as nginx will translate it to the external IP that hits it.
|
# 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):
|
if request.META.get(settings.META_REMOTE_ADDRESS_KEY):
|
||||||
updates["ip"] = 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)
|
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
|
return response
|
||||||
|
|
Loading…
Reference in a new issue