Skip to content

Commit

Permalink
Yep
Browse files Browse the repository at this point in the history
  • Loading branch information
baseplate-admin committed Dec 10, 2024
1 parent 32f6fee commit 17206fd
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 35 deletions.
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,6 @@ addopts = '--nomigrations'

[tool.mypy]
plugins = ["mypy_django_plugin.main"]

[tool.django-stubs]
django_settings_module = "tests.demo_project.demo.settings"
64 changes: 37 additions & 27 deletions src/ninja_put_patch_file_upload_middleware/middlewares.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from collections.abc import Awaitable, Callable
from typing import Any, TypeAlias
from typing import Any, TypeAlias, Final

from asgiref.sync import iscoroutinefunction, sync_to_async
from django.http import HttpRequest, HttpResponse
Expand All @@ -9,8 +9,8 @@
RequestHandler: TypeAlias = Callable[[HttpRequest], ResponseType]
AsyncRequestHandler: TypeAlias = Callable[[HttpRequest], Awaitable[ResponseType]]

TARGET_METHODS = {"PUT", "PATCH"}
CONTENT_TYPE = "application/json"
TARGET_METHODS: Final[set[str]] = {"PUT", "PATCH"}
CONTENT_TYPE: Final[str] = "application/json"


@sync_and_async_middleware
Expand All @@ -27,27 +27,37 @@ def process_put_patch(
Returns:
An async or sync middleware function based on the input handler type.
"""

async def async_middleware(request: HttpRequest) -> ResponseType:
if request.method in TARGET_METHODS and request.content_type != CONTENT_TYPE:
initial_method = request.method
request.method = "POST"
request.META["REQUEST_METHOD"] = "POST"
await sync_to_async(request._load_post_and_files)()
request.META["REQUEST_METHOD"] = initial_method
request.method = initial_method

return await get_response(request)

def sync_middleware(request: HttpRequest) -> ResponseType:
if request.method in TARGET_METHODS and request.content_type != CONTENT_TYPE:
initial_method = request.method
request.method = "POST"
request.META["REQUEST_METHOD"] = "POST"
request._load_post_and_files()
request.META["REQUEST_METHOD"] = initial_method
request.method = initial_method

return get_response(request)

return async_middleware if iscoroutinefunction(get_response) else sync_middleware
if iscoroutinefunction(get_response):

async def async_middleware(request: HttpRequest) -> ResponseType:
if (
request.method in TARGET_METHODS
and request.content_type != CONTENT_TYPE
):
initial_method = request.method
request.method = "POST"
request.META["REQUEST_METHOD"] = "POST"
await sync_to_async(request._load_post_and_files)()
request.META["REQUEST_METHOD"] = initial_method
request.method = initial_method

return get_response(request)

return async_middleware
else:

def sync_middleware(request: HttpRequest) -> ResponseType:
if (
request.method in TARGET_METHODS
and request.content_type != CONTENT_TYPE
):
initial_method = request.method
request.method = "POST"
request.META["REQUEST_METHOD"] = "POST"
request._load_post_and_files()
request.META["REQUEST_METHOD"] = initial_method
request.method = initial_method

return get_response(request)

return sync_middleware
2 changes: 0 additions & 2 deletions tests/demo_project/demo/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
"django.contrib.messages",
"django.contrib.staticfiles",
"ninja",
"someapp",
"multi_param",
]

MIDDLEWARE = [
Expand Down
6 changes: 0 additions & 6 deletions tests/pytest.ini

This file was deleted.

0 comments on commit 17206fd

Please sign in to comment.