Skip to content

Commit

Permalink
fix: use MiddlewareMixin consistently. Fixes #2019
Browse files Browse the repository at this point in the history
  • Loading branch information
jerivas committed Feb 22, 2022
1 parent 75e84b9 commit aa91d40
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 35 deletions.
13 changes: 5 additions & 8 deletions mezzanine/core/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 _

Expand All @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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:
Expand Down
8 changes: 1 addition & 7 deletions mezzanine/core/request.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import threading

from mezzanine.utils.deprecation import MiddlewareMixin
from django.utils.deprecation import MiddlewareMixin

_thread_local = threading.local()

Expand All @@ -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
3 changes: 2 additions & 1 deletion mezzanine/pages/middleware.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down
17 changes: 0 additions & 17 deletions mezzanine/utils/deprecation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand All @@ -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.
Expand Down
3 changes: 1 addition & 2 deletions tests/test_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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, [], {}
)

Expand Down

1 comment on commit aa91d40

@henri-hulski
Copy link
Collaborator

@henri-hulski henri-hulski commented on aa91d40 Feb 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW This was a breaking change, as you cannot do from mezzanine.utils.deprecation import MiddlewareMixin anymore.
I think it should be mentioned in the release notes.
(Cartridge was suddenly broken and it took me a while to find out why ;)

Please sign in to comment.