add blueprint name to request.endpoint

This commit is contained in:
Raphael Deem 2017-10-16 23:11:56 -07:00
parent 75f2180cb1
commit 9150767574
2 changed files with 33 additions and 2 deletions

View File

@ -136,3 +136,24 @@ app = Sanic()
def hello(request): def hello(request):
return text(request.endpoint) 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)
```

View File

@ -247,7 +247,12 @@ class Sanic:
def response(handler): def response(handler):
async def websocket_handler(request, *args, **kwargs): async def websocket_handler(request, *args, **kwargs):
request.app = self request.app = self
if not getattr(handler, '__blueprintname__', False):
request.endpoint = handler.__name__ request.endpoint = handler.__name__
else:
request.endpoint = getattr(handler,
'__blueprintname__',
'') + handler.__name__
try: try:
protocol = request.transport.get_protocol() protocol = request.transport.get_protocol()
except AttributeError: except AttributeError:
@ -541,7 +546,12 @@ class Sanic:
# Fetch handler from router # Fetch handler from router
handler, args, kwargs, uri = self.router.get(request) handler, args, kwargs, uri = self.router.get(request)
if not getattr(handler, '__blueprintname__', False):
request.endpoint = handler.__name__ request.endpoint = handler.__name__
else:
request.endpoint = getattr(handler,
'__blueprintname__',
'') + handler.__name__
request.uri_template = uri request.uri_template = uri
if handler is None: if handler is None:
raise ServerError( raise ServerError(