From d57d90fe6b6fa4d103a82e8704ff3ec1202756f3 Mon Sep 17 00:00:00 2001 From: growingdever Date: Tue, 14 Feb 2017 14:23:22 +0900 Subject: [PATCH 1/4] - make blueprint add_route method support view instance - update documentation that doesn't specify url_prefix parameter --- docs/sanic/blueprints.md | 4 ++-- sanic/blueprints.py | 12 ++++++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/docs/sanic/blueprints.md b/docs/sanic/blueprints.md index 120aa1dd..02d387b2 100644 --- a/docs/sanic/blueprints.md +++ b/docs/sanic/blueprints.md @@ -131,8 +131,8 @@ can be used to implement our API versioning scheme. from sanic.response import text from sanic import Blueprint -blueprint_v1 = Blueprint('v1') -blueprint_v2 = Blueprint('v2') +blueprint_v1 = Blueprint('v1', url_prefix='/v1') +blueprint_v2 = Blueprint('v2', url_prefix='/v2') @blueprint_v1.route('/') async def api_v1_root(request): diff --git a/sanic/blueprints.py b/sanic/blueprints.py index 64a37da0..c07cc398 100644 --- a/sanic/blueprints.py +++ b/sanic/blueprints.py @@ -1,5 +1,6 @@ from collections import defaultdict, namedtuple +from sanic.constants import HTTP_METHODS FutureRoute = namedtuple('Route', ['handler', 'uri', 'methods', 'host']) FutureListener = namedtuple('Listener', ['handler', 'uri', 'methods', 'host']) @@ -82,15 +83,18 @@ class Blueprint: return handler return decorator - def add_route(self, handler, uri, methods=None, host=None): + def add_route(self, handler, uri, methods=frozenset({'GET'}), host=None): """ Creates a blueprint route from a function. - :param handler: Function to handle uri request. + :param handler: function or class instance to handle uri request. :param uri: Endpoint at which the route will be accessible. :param methods: List of acceptable HTTP methods. + :return: function or class instance """ - route = FutureRoute(handler, uri, methods, host) - self.routes.append(route) + # Handle HTTPMethodView differently + if hasattr(handler, 'view_class'): + methods = frozenset(HTTP_METHODS) + self.route(uri=uri, methods=methods, host=host)(handler) return handler def listener(self, event): From b66a6bddbc49583cd353017c17fcd423aa7828fb Mon Sep 17 00:00:00 2001 From: growingdever Date: Tue, 14 Feb 2017 14:30:07 +0900 Subject: [PATCH 2/4] fix typo --- sanic/blueprints.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sanic/blueprints.py b/sanic/blueprints.py index c07cc398..237f1b51 100644 --- a/sanic/blueprints.py +++ b/sanic/blueprints.py @@ -86,7 +86,7 @@ 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 or class instance to handle uri request. :param uri: Endpoint at which the route will be accessible. :param methods: List of acceptable HTTP methods. :return: function or class instance From 07aa0ee7ad535db4e79ec3fbc40748459d66ac6d Mon Sep 17 00:00:00 2001 From: growingdever Date: Tue, 14 Feb 2017 17:15:38 +0900 Subject: [PATCH 3/4] - 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 From 81a8a99b6e7fd0865ab87e70b8c3af2a782d3047 Mon Sep 17 00:00:00 2001 From: growingdever Date: Tue, 14 Feb 2017 17:20:39 +0900 Subject: [PATCH 4/4] wrap over width comment --- sanic/blueprints.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sanic/blueprints.py b/sanic/blueprints.py index a123a7a3..155d187b 100644 --- a/sanic/blueprints.py +++ b/sanic/blueprints.py @@ -87,7 +87,8 @@ class Blueprint: def add_route(self, handler, uri, methods=frozenset({'GET'}), host=None): """ Creates a blueprint route from a function. - :param handler: Function for handling uri requests. Accepts function, or class instance with a view_class method. + :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