From 9150767574fc2f213b1e8aca6d61d9e2f70e7e40 Mon Sep 17 00:00:00 2001 From: Raphael Deem Date: Mon, 16 Oct 2017 23:11:56 -0700 Subject: [PATCH] add blueprint name to request.endpoint --- docs/sanic/request_data.md | 21 +++++++++++++++++++++ sanic/app.py | 14 ++++++++++++-- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/docs/sanic/request_data.md b/docs/sanic/request_data.md index 5423aa13..82a89823 100644 --- a/docs/sanic/request_data.md +++ b/docs/sanic/request_data.md @@ -136,3 +136,24 @@ app = Sanic() def hello(request): return text(request.endpoint) ``` + +Or, with a blueprint it will be include both, separated by a period. For example, + the below route would return foo.bar: + +```python +from sanic import Sanic +from sanic import Blueprint +from sanic.response import text + + +app = Sanic(__name__) +blueprint = Blueprint('foo') + +@blueprint.get('/') +async def bar(request): + return text(request.endpoint) + +app.blueprint(blueprint) + +app.run(host="0.0.0.0", port=8000, debug=True) +``` diff --git a/sanic/app.py b/sanic/app.py index 8f621599..f1790a3e 100644 --- a/sanic/app.py +++ b/sanic/app.py @@ -247,7 +247,12 @@ class Sanic: def response(handler): async def websocket_handler(request, *args, **kwargs): request.app = self - request.endpoint = handler.__name__ + if not getattr(handler, '__blueprintname__', False): + request.endpoint = handler.__name__ + else: + request.endpoint = getattr(handler, + '__blueprintname__', + '') + handler.__name__ try: protocol = request.transport.get_protocol() except AttributeError: @@ -541,7 +546,12 @@ class Sanic: # Fetch handler from router handler, args, kwargs, uri = self.router.get(request) - request.endpoint = handler.__name__ + if not getattr(handler, '__blueprintname__', False): + request.endpoint = handler.__name__ + else: + request.endpoint = getattr(handler, + '__blueprintname__', + '') + handler.__name__ request.uri_template = uri if handler is None: raise ServerError(