mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-27 08:29:59 +03:00
Refactored FBV code
This commit is contained in:
parent
7b18380321
commit
f65e859e2f
|
@ -47,20 +47,17 @@ def api_view(http_method_names=None):
|
||||||
allowed_methods = set(http_method_names) | {'options'}
|
allowed_methods = set(http_method_names) | {'options'}
|
||||||
WrappedAPIView.http_method_names = [method.lower() for method in allowed_methods]
|
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)
|
view_is_async = asyncio.iscoroutinefunction(func)
|
||||||
|
|
||||||
if view_is_async:
|
if view_is_async:
|
||||||
for method in http_method_names:
|
async def handler(self, *args, **kwargs):
|
||||||
setattr(WrappedAPIView, method.lower(), async_handler)
|
return await func(*args, **kwargs)
|
||||||
else:
|
else:
|
||||||
|
def handler(self, *args, **kwargs):
|
||||||
|
return func(*args, **kwargs)
|
||||||
|
|
||||||
for method in http_method_names:
|
for method in http_method_names:
|
||||||
setattr(WrappedAPIView, method.lower(), sync_handler)
|
setattr(WrappedAPIView, method.lower(), handler)
|
||||||
|
|
||||||
WrappedAPIView.__name__ = func.__name__
|
WrappedAPIView.__name__ = func.__name__
|
||||||
WrappedAPIView.__module__ = func.__module__
|
WrappedAPIView.__module__ = func.__module__
|
||||||
|
|
Loading…
Reference in New Issue
Block a user