From 07aa0ee7ad535db4e79ec3fbc40748459d66ac6d Mon Sep 17 00:00:00 2001 From: growingdever Date: Tue, 14 Feb 2017 17:15:38 +0900 Subject: [PATCH] - copy codes from Sonic.add_route - modify comment by r0fls --- sanic/blueprints.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/sanic/blueprints.py b/sanic/blueprints.py index 237f1b51..a123a7a3 100644 --- a/sanic/blueprints.py +++ b/sanic/blueprints.py @@ -1,6 +1,7 @@ from collections import defaultdict, namedtuple from sanic.constants import HTTP_METHODS +from sanic.views import CompositionView FutureRoute = namedtuple('Route', ['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): """ 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 methods: List of acceptable HTTP methods. :return: function or class instance """ # Handle HTTPMethodView differently 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) return handler