From aa91d40fc67c0d5d992eab0f9f5603abefd6231d Mon Sep 17 00:00:00 2001 From: Ed Rivas Date: Tue, 22 Feb 2022 08:47:32 -0600 Subject: [PATCH] fix: use MiddlewareMixin consistently. Fixes #2019 --- mezzanine/core/middleware.py | 13 +++++-------- mezzanine/core/request.py | 8 +------- mezzanine/pages/middleware.py | 3 ++- mezzanine/utils/deprecation.py | 17 ----------------- tests/test_pages.py | 3 +-- 5 files changed, 9 insertions(+), 35 deletions(-) diff --git a/mezzanine/core/middleware.py b/mezzanine/core/middleware.py index 224488ec36..f0a2ebb3b3 100755 --- a/mezzanine/core/middleware.py +++ b/mezzanine/core/middleware.py @@ -16,6 +16,7 @@ from django.template import RequestContext, Template from django.urls import resolve, reverse from django.utils.cache import get_max_age +from django.utils.deprecation import MiddlewareMixin from django.utils.safestring import mark_safe from django.utils.translation import gettext as _ @@ -33,11 +34,7 @@ nevercache_token, ) from mezzanine.utils.conf import middlewares_or_subclasses_installed -from mezzanine.utils.deprecation import ( - MiddlewareMixin, - get_middleware_request, - is_authenticated, -) +from mezzanine.utils.deprecation import is_authenticated from mezzanine.utils.sites import current_site_id from mezzanine.utils.urls import next_url @@ -211,7 +208,7 @@ def process_response(self, request, response): # the cookie will be correctly set for the the response if csrf_middleware_installed(): response.csrf_processing_done = False - csrf_mw = CsrfViewMiddleware(get_middleware_request) + csrf_mw = CsrfViewMiddleware(self.get_response) csrf_mw.process_response(request, response) return response @@ -238,10 +235,10 @@ def process_request(self, request): cache_key = cache_key_prefix(request) + request.get_full_path() response = cache_get(cache_key) # We need to force a csrf token here, as new sessions - # won't receieve one on their first request, with cache + # won't receive one on their first request, with cache # middleware running. if csrf_middleware_installed(): - csrf_mw = CsrfViewMiddleware() + csrf_mw = CsrfViewMiddleware(self.get_response) csrf_mw.process_view(request, lambda x: None, None, None) get_token(request) if response is None: diff --git a/mezzanine/core/request.py b/mezzanine/core/request.py index 58a960ca45..456ace7931 100644 --- a/mezzanine/core/request.py +++ b/mezzanine/core/request.py @@ -1,6 +1,6 @@ import threading -from mezzanine.utils.deprecation import MiddlewareMixin +from django.utils.deprecation import MiddlewareMixin _thread_local = threading.local() @@ -19,9 +19,3 @@ class CurrentRequestMiddleware(MiddlewareMixin): def process_request(self, request): _thread_local.request = request - - # def process_response(self, request, response): - # try: - # return response - # finally: - # _thread_local.request = None diff --git a/mezzanine/pages/middleware.py b/mezzanine/pages/middleware.py index db2568a972..bda0ab5629 100644 --- a/mezzanine/pages/middleware.py +++ b/mezzanine/pages/middleware.py @@ -1,13 +1,14 @@ from django.contrib.auth.views import redirect_to_login from django.core.exceptions import MiddlewareNotUsed from django.http import Http404, HttpResponse +from django.utils.deprecation import MiddlewareMixin from mezzanine.conf import settings from mezzanine.pages import context_processors, page_processors from mezzanine.pages.models import Page from mezzanine.pages.views import page as page_view from mezzanine.utils.conf import middlewares_or_subclasses_installed -from mezzanine.utils.deprecation import MiddlewareMixin, is_authenticated +from mezzanine.utils.deprecation import is_authenticated from mezzanine.utils.urls import path_to_slug diff --git a/mezzanine/utils/deprecation.py b/mezzanine/utils/deprecation.py index ccbdefa0bf..8c8f6f9b1d 100644 --- a/mezzanine/utils/deprecation.py +++ b/mezzanine/utils/deprecation.py @@ -7,14 +7,6 @@ import django from django.conf import settings -# Middleware mixin for Django 1.10 -try: - from django.utils.deprecation import MiddlewareMixin -except ImportError: - - class MiddlewareMixin: - pass - def request_is_ajax(request): """ @@ -25,15 +17,6 @@ def request_is_ajax(request): return request.META.get("CONTENT_TYPE") == "application/json" -def get_middleware_request(request): - """ - Middlewares require get_request in after django4.0 - - Returns the passed request object - """ - return request - - def get_middleware_setting_name(): """ Returns the name of the middleware setting. diff --git a/tests/test_pages.py b/tests/test_pages.py index 3408ea6af0..c73a3dd103 100644 --- a/tests/test_pages.py +++ b/tests/test_pages.py @@ -21,7 +21,6 @@ from mezzanine.pages.fields import MenusField from mezzanine.pages.models import Page, RichTextPage from mezzanine.urls import PAGES_SLUG -from mezzanine.utils.deprecation import get_middleware_request from mezzanine.utils.sites import override_current_site_id from mezzanine.utils.tests import TestCase @@ -415,7 +414,7 @@ def test_page_processor(request, page): request = self._request_factory.get("/foo/bar/") request.user = self._user - response = PageMiddleware(get_middleware_request).process_view( + response = PageMiddleware(lambda resp: resp).process_view( request, page_view, [], {} )