Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions django/contrib/admin/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
from django.template.response import SimpleTemplateResponse, TemplateResponse
from django.urls import reverse
from django.utils.decorators import method_decorator
from django.utils.deprecation import RemovedInDjango70Warning
from django.utils.deprecation import RemovedInDjango70Warning, warn_about_implementation
from django.utils.html import format_html
from django.utils.http import urlencode
from django.utils.inspect import get_func_args
Expand Down Expand Up @@ -928,11 +928,12 @@ def get_changelist_instance(self, request):
# RemovedInDjango70Warning: when the deprecation ends, remove the
# below 'if' clause and raise a ValueError here.
if self.list_select_related is not True:
warnings.warn(
warn_about_implementation(
"Returning True from ModelAdmin.get_list_select_related() is "
"deprecated. Return False or a list or tuple of fields to "
"fetch instead.",
RemovedInDjango70Warning,
self.get_list_select_related,
)
return ChangeList(
request,
Expand Down Expand Up @@ -1132,12 +1133,12 @@ def _get_actions_with_action_location(
if "action_location" in get_func_args(self.get_actions):
return self.get_actions(request, action_location=action_location)
else:
warnings.warn(
warn_about_implementation(
"Overriding get_actions() without the 'action_location' parameter is "
"deprecated. Update the signature to get_actions(self, request, "
"action_location=ActionLocation.CHANGE_LIST).",
RemovedInDjango70Warning,
skip_file_prefixes=django_file_prefixes(),
self.get_actions,
)
if action_location == ActionLocation.CHANGE_FORM:
# Disable adding actions on change form when get_actions is
Expand Down Expand Up @@ -1172,13 +1173,13 @@ def _get_action_choices_with_action_location(
action_location=action_location,
)
else:
warnings.warn(
warn_about_implementation(
"Overriding get_action_choices() without the 'action_location' "
"parameter is deprecated. Update the signature to "
"get_action_choices(self, request, default_choices=None, "
"action_location=ActionLocation.CHANGE_LIST).",
RemovedInDjango70Warning,
skip_file_prefixes=django_file_prefixes(),
self.get_action_choices,
)
return self.get_action_choices(request, default_choices=default_choices)

Expand Down
2 changes: 1 addition & 1 deletion django/contrib/admindocs/middleware.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.http import HttpResponse
from django.utils.deprecation import MiddlewareMixin
from django.middleware import MiddlewareMixin

from .utils import get_view_name

Expand Down
2 changes: 1 addition & 1 deletion django/contrib/auth/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
from django.contrib.auth.views import redirect_to_login
from django.core.exceptions import ImproperlyConfigured
from django.core.handlers.asgi import ASGIRequest
from django.middleware import MiddlewareMixin
from django.shortcuts import resolve_url
from django.utils.deprecation import MiddlewareMixin
from django.utils.functional import SimpleLazyObject


Expand Down
2 changes: 1 addition & 1 deletion django/contrib/flatpages/middleware.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.conf import settings
from django.contrib.flatpages.views import flatpage
from django.http import Http404
from django.utils.deprecation import MiddlewareMixin
from django.middleware import MiddlewareMixin


class FlatpageFallbackMiddleware(MiddlewareMixin):
Expand Down
2 changes: 1 addition & 1 deletion django/contrib/messages/middleware.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.conf import settings
from django.contrib.messages.storage import default_storage
from django.utils.deprecation import MiddlewareMixin
from django.middleware import MiddlewareMixin


class MessageMiddleware(MiddlewareMixin):
Expand Down
2 changes: 1 addition & 1 deletion django/contrib/redirects/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.contrib.sites.shortcuts import get_current_site
from django.core.exceptions import ImproperlyConfigured
from django.http import HttpResponseGone, HttpResponsePermanentRedirect
from django.utils.deprecation import MiddlewareMixin
from django.middleware import MiddlewareMixin


class RedirectFallbackMiddleware(MiddlewareMixin):
Expand Down
2 changes: 1 addition & 1 deletion django/contrib/sessions/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from django.conf import settings
from django.contrib.sessions.backends.base import UpdateError
from django.contrib.sessions.exceptions import SessionInterrupted
from django.middleware import MiddlewareMixin
from django.utils.cache import patch_vary_headers
from django.utils.deprecation import MiddlewareMixin
from django.utils.http import http_date


Expand Down
2 changes: 1 addition & 1 deletion django/contrib/sites/middleware.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.utils.deprecation import MiddlewareMixin
from django.middleware import MiddlewareMixin

from .shortcuts import get_current_site

Expand Down
64 changes: 64 additions & 0 deletions django/middleware/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from inspect import iscoroutinefunction, markcoroutinefunction

from asgiref.sync import sync_to_async

__all__ = ["MiddlewareMixin"]


class MiddlewareMixin:
sync_capable = True
async_capable = True

def __init__(self, get_response):
if get_response is None:
raise ValueError("get_response must be provided.")
self.get_response = get_response
# If get_response is a coroutine function, turns us into async mode so
# a thread is not consumed during a whole request.
self.async_mode = iscoroutinefunction(self.get_response)
if self.async_mode:
# Mark the class as async-capable, but do the actual switch inside
# __call__ to avoid swapping out dunder methods.
markcoroutinefunction(self)
super().__init__()

def __repr__(self):
return "<%s get_response=%s>" % (
self.__class__.__qualname__,
getattr(
self.get_response,
"__qualname__",
self.get_response.__class__.__name__,
),
)

def __call__(self, request):
# Exit out to async mode, if needed
if self.async_mode:
return self.__acall__(request)
response = None
if hasattr(self, "process_request"):
response = self.process_request(request)
response = response or self.get_response(request)
if hasattr(self, "process_response"):
response = self.process_response(request, response)
return response

async def __acall__(self, request):
"""
Async version of __call__ that is swapped in when an async request
is running.
"""
response = None
if hasattr(self, "process_request"):
response = await sync_to_async(
self.process_request,
thread_sensitive=True,
)(request)
response = response or await self.get_response(request)
if hasattr(self, "process_response"):
response = await sync_to_async(
self.process_response,
thread_sensitive=True,
)(request, response)
return response
2 changes: 1 addition & 1 deletion django/middleware/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@

from django.conf import settings
from django.core.cache import DEFAULT_CACHE_ALIAS, caches
from django.middleware import MiddlewareMixin
from django.utils.cache import (
get_cache_key,
get_max_age,
Expand All @@ -57,7 +58,6 @@
patch_response_headers,
patch_vary_headers,
)
from django.utils.deprecation import MiddlewareMixin
from django.utils.http import parse_http_date_safe, split_directive_names


Expand Down
2 changes: 1 addition & 1 deletion django/middleware/clickjacking.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"""

from django.conf import settings
from django.utils.deprecation import MiddlewareMixin
from django.middleware import MiddlewareMixin


class XFrameOptionsMiddleware(MiddlewareMixin):
Expand Down
2 changes: 1 addition & 1 deletion django/middleware/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from django.core.exceptions import PermissionDenied
from django.core.mail import MailerDoesNotExist, mail_managers, mailers
from django.http import HttpResponsePermanentRedirect
from django.middleware import MiddlewareMixin
from django.urls import is_valid_path
from django.utils.deprecation import MiddlewareMixin
from django.utils.http import escape_leading_slashes


Expand Down
2 changes: 1 addition & 1 deletion django/middleware/csp.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.conf import settings
from django.middleware import MiddlewareMixin
from django.utils.csp import CSP, LazyNonce, build_policy
from django.utils.deprecation import MiddlewareMixin


def get_nonce(request):
Expand Down
2 changes: 1 addition & 1 deletion django/middleware/csrf.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
from django.conf import settings
from django.core.exceptions import DisallowedHost, ImproperlyConfigured
from django.http import HttpHeaders, UnreadablePostError
from django.middleware import MiddlewareMixin
from django.urls import get_callable
from django.utils.cache import patch_vary_headers
from django.utils.crypto import constant_time_compare, get_random_string
from django.utils.deprecation import MiddlewareMixin
from django.utils.functional import cached_property
from django.utils.http import is_same_domain
from django.utils.log import log_response
Expand Down
2 changes: 1 addition & 1 deletion django/middleware/gzip.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.middleware import MiddlewareMixin
from django.utils.cache import patch_vary_headers
from django.utils.deprecation import MiddlewareMixin
from django.utils.regex_helper import _lazy_re_compile
from django.utils.text import acompress_sequence, compress_sequence, compress_string

Expand Down
2 changes: 1 addition & 1 deletion django/middleware/http.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.middleware import MiddlewareMixin
from django.utils.cache import get_conditional_response, set_response_etag
from django.utils.deprecation import MiddlewareMixin
from django.utils.http import parse_http_date_safe, split_directive_names


Expand Down
2 changes: 1 addition & 1 deletion django/middleware/locale.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from django.conf import settings
from django.conf.urls.i18n import is_language_prefix_patterns_used
from django.http import HttpResponseRedirect
from django.middleware import MiddlewareMixin
from django.urls import get_script_prefix, is_valid_path
from django.utils import translation
from django.utils.cache import patch_vary_headers
from django.utils.deprecation import MiddlewareMixin


class LocaleMiddleware(MiddlewareMixin):
Expand Down
2 changes: 1 addition & 1 deletion django/middleware/security.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from django.conf import settings
from django.http import HttpResponsePermanentRedirect
from django.utils.deprecation import MiddlewareMixin
from django.middleware import MiddlewareMixin


class SecurityMiddleware(MiddlewareMixin):
Expand Down
Loading
Loading