Coverage for /Users/davegaeddert/Development/dropseed/plain/plain-auth/plain/auth/middleware.py: 94%

1 from plain import auth

2 from plain.exceptions import ImproperlyConfigured

3 from plain.utils.functional import SimpleLazyObject

4

5

6 def get_user(request):

7 if not hasattr(request, "_cached_user"):

8 request._cached_user = auth.get_user(request)

9 return request._cached_user

10

11

12 class AuthenticationMiddleware:

13 def __init__(self, get_response):

14 self.get_response = get_response

15

16 def __call__(self, request):

17 if not hasattr(request, "session"):

18 raise ImproperlyConfigured(

19 "The Plain authentication middleware requires session "

20 "middleware to be installed. Edit your MIDDLEWARE setting to "

21 "insert "

22 "'plain.sessions.middleware.SessionMiddleware' before "

23 "'plain.auth.middleware.AuthenticationMiddleware'."

24 )

25 request.user = SimpleLazyObject(lambda: get_user(request))

26 response = self.get_response(request)

27 return response