Simplified AuthenticationMiddleware a bit.

SimpleLazyObject already caches value in _wrapped.
This commit is contained in:
Sergey Fedoseev 2019-10-22 16:32:37 +05:00 committed by Mariusz Felisiak
parent 7552de7866
commit 2f010795e6
1 changed files with 3 additions and 7 deletions

View File

@ -1,3 +1,5 @@
from functools import partial
from django.contrib import auth from django.contrib import auth
from django.contrib.auth import load_backend from django.contrib.auth import load_backend
from django.contrib.auth.backends import RemoteUserBackend from django.contrib.auth.backends import RemoteUserBackend
@ -6,12 +8,6 @@ from django.utils.deprecation import MiddlewareMixin
from django.utils.functional import SimpleLazyObject from django.utils.functional import SimpleLazyObject
def get_user(request):
if not hasattr(request, '_cached_user'):
request._cached_user = auth.get_user(request)
return request._cached_user
class AuthenticationMiddleware(MiddlewareMixin): class AuthenticationMiddleware(MiddlewareMixin):
def process_request(self, request): def process_request(self, request):
assert hasattr(request, 'session'), ( assert hasattr(request, 'session'), (
@ -20,7 +16,7 @@ class AuthenticationMiddleware(MiddlewareMixin):
"'django.contrib.sessions.middleware.SessionMiddleware' before " "'django.contrib.sessions.middleware.SessionMiddleware' before "
"'django.contrib.auth.middleware.AuthenticationMiddleware'." "'django.contrib.auth.middleware.AuthenticationMiddleware'."
) )
request.user = SimpleLazyObject(lambda: get_user(request)) request.user = SimpleLazyObject(partial(auth.get_user, request))
class RemoteUserMiddleware(MiddlewareMixin): class RemoteUserMiddleware(MiddlewareMixin):