python: Reformat with Black, except quotes.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg
2021-02-11 23:19:30 -08:00
committed by Tim Abbott
parent 5028c081cb
commit 11741543da
817 changed files with 44952 additions and 24860 deletions

View File

@@ -18,7 +18,8 @@ from zerver.lib.response import json_method_not_allowed
from zerver.lib.types import ViewFuncT
METHODS = ('GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'PATCH')
FLAGS = ('override_api_url_scheme')
FLAGS = 'override_api_url_scheme'
def default_never_cache_responses(view_func: ViewFuncT) -> ViewFuncT:
"""Patched version of the standard Django never_cache_responses
@@ -26,6 +27,7 @@ def default_never_cache_responses(view_func: ViewFuncT) -> ViewFuncT:
cached, unless the view code has already set a Cache-Control
header.
"""
@wraps(view_func)
def _wrapped_view_func(request: HttpRequest, *args: object, **kwargs: object) -> HttpResponse:
response = view_func(request, *args, **kwargs)
@@ -34,8 +36,10 @@ def default_never_cache_responses(view_func: ViewFuncT) -> ViewFuncT:
add_never_cache_headers(response)
return response
return cast(ViewFuncT, _wrapped_view_func) # https://github.com/python/mypy/issues/1927
@default_never_cache_responses
@csrf_exempt
def rest_dispatch(request: HttpRequest, **kwargs: Any) -> HttpResponse:
@@ -109,21 +113,24 @@ def rest_dispatch(request: HttpRequest, **kwargs: Any) -> HttpResponse:
# for some special views (e.g. serving a file that has been
# uploaded), we support using the same URL for web and API clients.
if ('override_api_url_scheme' in view_flags and
request.META.get('HTTP_AUTHORIZATION', None) is not None):
if (
'override_api_url_scheme' in view_flags
and request.META.get('HTTP_AUTHORIZATION', None) is not None
):
# This request uses standard API based authentication.
# For override_api_url_scheme views, we skip our normal
# rate limiting, because there are good reasons clients
# might need to (e.g.) request a large number of uploaded
# files or avatars in quick succession.
target_function = authenticated_rest_api_view(skip_rate_limiting=True)(target_function)
elif ('override_api_url_scheme' in view_flags and
request.GET.get('api_key') is not None):
elif 'override_api_url_scheme' in view_flags and request.GET.get('api_key') is not None:
# This request uses legacy API authentication. We
# unfortunately need that in the React Native mobile apps,
# because there's no way to set HTTP_AUTHORIZATION in
# React Native. See last block for rate limiting notes.
target_function = authenticated_uploads_api_view(skip_rate_limiting=True)(target_function)
target_function = authenticated_uploads_api_view(skip_rate_limiting=True)(
target_function
)
# /json views (web client) validate with a session token (cookie)
elif not request.path.startswith("/api") and request.user.is_authenticated:
# Authenticated via sessions framework, only CSRF check needed
@@ -144,8 +151,7 @@ def rest_dispatch(request: HttpRequest, **kwargs: Any) -> HttpResponse:
# For endpoints that support anonymous web access, we do that.
# TODO: Allow /api calls when this is stable enough.
auth_kwargs = dict(allow_unauthenticated=True)
target_function = csrf_protect(authenticated_json_view(
target_function, **auth_kwargs))
target_function = csrf_protect(authenticated_json_view(target_function, **auth_kwargs))
else:
# Otherwise, throw an authentication error; our middleware
# will generate the appropriate HTTP response.
@@ -161,6 +167,7 @@ def rest_dispatch(request: HttpRequest, **kwargs: Any) -> HttpResponse:
return json_method_not_allowed(list(supported_methods.keys()))
def rest_path(
route: str,
kwargs: Mapping[str, object] = {},