- copy codes from Sonic.add_route
- modify comment by r0fls
This commit is contained in:
parent
b66a6bddbc
commit
07aa0ee7ad
|
@ -1,6 +1,7 @@
|
||||||
from collections import defaultdict, namedtuple
|
from collections import defaultdict, namedtuple
|
||||||
|
|
||||||
from sanic.constants import HTTP_METHODS
|
from sanic.constants import HTTP_METHODS
|
||||||
|
from sanic.views import CompositionView
|
||||||
|
|
||||||
FutureRoute = namedtuple('Route', ['handler', 'uri', 'methods', 'host'])
|
FutureRoute = namedtuple('Route', ['handler', 'uri', 'methods', 'host'])
|
||||||
FutureListener = namedtuple('Listener', ['handler', 'uri', 'methods', 'host'])
|
FutureListener = namedtuple('Listener', ['handler', 'uri', 'methods', 'host'])
|
||||||
|
@ -86,14 +87,23 @@ class Blueprint:
|
||||||
def add_route(self, handler, uri, methods=frozenset({'GET'}), host=None):
|
def add_route(self, handler, uri, methods=frozenset({'GET'}), host=None):
|
||||||
"""
|
"""
|
||||||
Creates a blueprint route from a function.
|
Creates a blueprint route from a function.
|
||||||
:param handler: Function or class instance to handle uri request.
|
:param handler: Function for handling uri requests. Accepts function, or class instance with a view_class method.
|
||||||
:param uri: Endpoint at which the route will be accessible.
|
:param uri: Endpoint at which the route will be accessible.
|
||||||
:param methods: List of acceptable HTTP methods.
|
:param methods: List of acceptable HTTP methods.
|
||||||
:return: function or class instance
|
:return: function or class instance
|
||||||
"""
|
"""
|
||||||
# Handle HTTPMethodView differently
|
# Handle HTTPMethodView differently
|
||||||
if hasattr(handler, 'view_class'):
|
if hasattr(handler, 'view_class'):
|
||||||
methods = frozenset(HTTP_METHODS)
|
methods = set()
|
||||||
|
|
||||||
|
for method in HTTP_METHODS:
|
||||||
|
if getattr(handler.view_class, method.lower(), None):
|
||||||
|
methods.add(method)
|
||||||
|
|
||||||
|
# handle composition view differently
|
||||||
|
if isinstance(handler, CompositionView):
|
||||||
|
methods = handler.handlers.keys()
|
||||||
|
|
||||||
self.route(uri=uri, methods=methods, host=host)(handler)
|
self.route(uri=uri, methods=methods, host=host)(handler)
|
||||||
return handler
|
return handler
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user