is_request_stream for CompositionView and HTTPMethodView

This commit is contained in:
38elements
2017-05-08 23:04:45 +09:00
parent 15ad07f03d
commit 4d4f38fb35
7 changed files with 228 additions and 60 deletions

View File

@@ -131,7 +131,8 @@ class Sanic:
self.is_request_stream = True
def response(handler):
handler.is_stream = stream
if stream:
handler.is_stream = stream
self.router.add(uri=uri, methods=methods, handler=handler,
host=host, strict_slashes=strict_slashes)
return handler
@@ -187,20 +188,28 @@ class Sanic:
:param host:
:return: function or class instance
"""
stream = False
# Handle HTTPMethodView differently
if hasattr(handler, 'view_class'):
methods = set()
for method in HTTP_METHODS:
if getattr(handler.view_class, method.lower(), None):
_handler = getattr(handler.view_class, method.lower(), None)
if _handler:
methods.add(method)
if hasattr(_handler, 'is_stream'):
stream = True
# handle composition view differently
if isinstance(handler, CompositionView):
methods = handler.handlers.keys()
for _handler in handler.handlers.values():
if hasattr(_handler, 'is_stream'):
stream = True
break
self.route(uri=uri, methods=methods, host=host,
strict_slashes=strict_slashes)(handler)
strict_slashes=strict_slashes, stream=stream)(handler)
return handler
# Decorator

View File

@@ -355,4 +355,4 @@ class Router:
if (hasattr(handler, 'view_class') and
hasattr(handler.view_class, request.method.lower())):
handler = getattr(handler.view_class, request.method.lower())
return hasattr(handler, 'is_stream') and handler.is_stream
return hasattr(handler, 'is_stream')

View File

@@ -89,7 +89,8 @@ class CompositionView:
self.handlers = {}
def add(self, methods, handler, stream=False):
handler.is_stream = stream
if stream:
handler.is_stream = stream
for method in methods:
if method not in HTTP_METHODS:
raise InvalidUsage(