From f65e859e2f90c91bad4de33c86233d9bad93c4d5 Mon Sep 17 00:00:00 2001 From: enrico Date: Mon, 5 Sep 2022 18:50:15 +0800 Subject: [PATCH] Refactored FBV code --- rest_framework/decorators.py | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/rest_framework/decorators.py b/rest_framework/decorators.py index 9c526959b..1a56f7fa2 100644 --- a/rest_framework/decorators.py +++ b/rest_framework/decorators.py @@ -47,20 +47,17 @@ def api_view(http_method_names=None): allowed_methods = set(http_method_names) | {'options'} WrappedAPIView.http_method_names = [method.lower() for method in allowed_methods] - def sync_handler(self, *args, **kwargs): - return func(*args, **kwargs) - - async def async_handler(self, *args, **kwargs): - return await func(*args, **kwargs) - view_is_async = asyncio.iscoroutinefunction(func) if view_is_async: - for method in http_method_names: - setattr(WrappedAPIView, method.lower(), async_handler) + async def handler(self, *args, **kwargs): + return await func(*args, **kwargs) else: - for method in http_method_names: - setattr(WrappedAPIView, method.lower(), sync_handler) + def handler(self, *args, **kwargs): + return func(*args, **kwargs) + + for method in http_method_names: + setattr(WrappedAPIView, method.lower(), handler) WrappedAPIView.__name__ = func.__name__ WrappedAPIView.__module__ = func.__module__