Skip to content

Commit b0ca7e5

Browse files
mvantellingenmitsuhiko
authored andcommitted
Implement the endpoint decorator. This allows you to easily map views to endpoints when using the werkzeug routing.
Signed-off-by: Armin Ronacher <armin.ronacher@active-4.com>
1 parent 8a73097 commit b0ca7e5

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

docs/patterns/viewdecorators.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,24 @@ Here the code for that decorator::
145145
return render_template(template_name, **ctx)
146146
return decorated_function
147147
return decorator
148+
149+
150+
Endpoint Decorator
151+
------------------
152+
153+
When you want to use the werkzeug routing system for more flexibility you
154+
need to map the endpoint as defined in the :class:`~werkzeug.routing.Rule`
155+
to a view function. This is possible with this decorator. For example::
156+
157+
from flask import Flask
158+
from werkzeug.routing import Rule
159+
160+
app = Flask(__name__)
161+
app.url_map.add(Rule('/', endpoint='index'))
162+
163+
@app.endpoint('index')
164+
def my_index():
165+
return "Hello world"
166+
167+
168+

flask/app.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,7 @@ def register_module(self, module, **options):
496496
"""
497497
options.setdefault('url_prefix', module.url_prefix)
498498
options.setdefault('subdomain', module.subdomain)
499+
self.view_functions.update(module.view_functions)
499500
state = _ModuleSetupState(self, **options)
500501
for func in module._register_events:
501502
func(state)
@@ -629,6 +630,22 @@ def decorator(f):
629630
return f
630631
return decorator
631632

633+
634+
def endpoint(self, endpoint):
635+
"""A decorator to register a function as an endpoint.
636+
Example::
637+
638+
@app.endpoint('example.endpoint')
639+
def example():
640+
return "example"
641+
642+
:param endpoint: the name of the endpoint
643+
"""
644+
def decorator(f):
645+
self.view_functions[endpoint] = f
646+
return f
647+
return decorator
648+
632649
def errorhandler(self, code):
633650
"""A decorator that is used to register a function give a given
634651
error code. Example::

flask/module.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ def __init__(self, import_name, name=None, url_prefix=None,
124124
self.name = name
125125
self.url_prefix = url_prefix
126126
self.subdomain = subdomain
127+
self.view_functions = {}
127128
self._register_events = [_register_module(self, static_path)]
128129

129130
def route(self, rule, **options):
@@ -157,6 +158,13 @@ def register_rule(state):
157158
view_func, **options)
158159
self._record(register_rule)
159160

161+
def endpoint(self, endpoint):
162+
"""Like :meth:`Flask.endpoint` but for a module."""
163+
def decorator(f):
164+
self.view_functions[endpoint] = f
165+
return f
166+
return decorator
167+
160168
def before_request(self, f):
161169
"""Like :meth:`Flask.before_request` but for a module. This function
162170
is only executed before each request that is handled by a function of

0 commit comments

Comments
 (0)