From 0eb9ec451e94f160072d8e7503a7afcf2585d7f3 Mon Sep 17 00:00:00 2001 From: Filipp Lepalaan Date: Wed, 12 May 2021 20:53:36 +0300 Subject: Fix custom middleware --- servo/lib/middleware.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'servo') diff --git a/servo/lib/middleware.py b/servo/lib/middleware.py index ec50188..0ee054a 100644 --- a/servo/lib/middleware.py +++ b/servo/lib/middleware.py @@ -11,7 +11,7 @@ if hasattr(settings, 'LOGIN_EXEMPT_URLS'): EXEMPT_URLS = [compile(expr) for expr in settings.LOGIN_EXEMPT_URLS] -class LoginRequiredMiddleware(object): +class LoginRequiredMiddleware: """ Middleware that requires a user to be authenticated to view any page other than LOGIN_URL. Exemptions to this requirement can optionally be specified @@ -21,7 +21,12 @@ class LoginRequiredMiddleware(object): Requires authentication middleware and template context processors to be loaded. You'll get an error if they aren't. """ - def process_request(self, request): + def __init__(self, get_response): + self.get_response = get_response + + def __call__(self, request): + response = self.get_response(request) + assert hasattr(request, 'user'), "The Login Required middleware\ requires authentication middleware to be installed. Edit your\ MIDDLEWARE_CLASSES setting to insert\ @@ -33,11 +38,19 @@ class LoginRequiredMiddleware(object): if not any(m.match(path) for m in EXEMPT_URLS): return HttpResponseRedirect(settings.LOGIN_URL) + return response + -class TimezoneMiddleware(object): - def process_request(self, request): +class TimezoneMiddleware: + def __init__(self, get_response): + self.get_response = get_response + + def __call__(self, request): + response = self.get_response(request) tzname = request.session.get('django_timezone') if tzname: timezone.activate(pytz.timezone(tzname)) else: timezone.deactivate() + + return response -- cgit v1.2.3