From 6e99585c19290fb9bec502cac8210041fdb28484 Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Wed, 13 Nov 2019 16:33:25 +0100 Subject: [PATCH] Fixed #30941 -- Reverted "Simplified AuthenticationMiddleware a bit." This reverts commit 2f010795e690550c8c6f56b3924c0f629cacb33b. --- django/contrib/auth/middleware.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/django/contrib/auth/middleware.py b/django/contrib/auth/middleware.py index 89c7ca6205b..5bd176ef69f 100644 --- a/django/contrib/auth/middleware.py +++ b/django/contrib/auth/middleware.py @@ -1,5 +1,3 @@ -from functools import partial - from django.contrib import auth from django.contrib.auth import load_backend from django.contrib.auth.backends import RemoteUserBackend @@ -8,6 +6,12 @@ from django.utils.deprecation import MiddlewareMixin 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): def process_request(self, request): assert hasattr(request, 'session'), ( @@ -16,7 +20,7 @@ class AuthenticationMiddleware(MiddlewareMixin): "'django.contrib.sessions.middleware.SessionMiddleware' before " "'django.contrib.auth.middleware.AuthenticationMiddleware'." ) - request.user = SimpleLazyObject(partial(auth.get_user, request)) + request.user = SimpleLazyObject(lambda: get_user(request)) class RemoteUserMiddleware(MiddlewareMixin):