Skip to content

Commit 71b7c4f

Browse files
authored
Merge pull request pallets#2258 from davidism/preprocess_request-docs
Clean up preprocess_request docs
2 parents 970a800 + 501f043 commit 71b7c4f

File tree

1 file changed

+39
-31
lines changed

1 file changed

+39
-31
lines changed

flask/app.py

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -415,17 +415,16 @@ def __init__(self, import_name, static_path=None, static_url_path=None,
415415
#: .. versionadded:: 0.9
416416
self.url_build_error_handlers = []
417417

418-
#: A dictionary with lists of functions that should be called at the
419-
#: beginning of the request. The key of the dictionary is the name of
420-
#: the blueprint this function is active for, ``None`` for all requests.
421-
#: This can for example be used to open database connections or
422-
#: getting hold of the currently logged in user. To register a
423-
#: function here, use the :meth:`before_request` decorator.
418+
#: A dictionary with lists of functions that will be called at the
419+
#: beginning of each request. The key of the dictionary is the name of
420+
#: the blueprint this function is active for, or ``None`` for all
421+
#: requests. To register a function, use the :meth:`before_request`
422+
#: decorator.
424423
self.before_request_funcs = {}
425424

426-
#: A lists of functions that should be called at the beginning of the
427-
#: first request to this instance. To register a function here, use
428-
#: the :meth:`before_first_request` decorator.
425+
#: A list of functions that will be called at the beginning of the
426+
#: first request to this instance. To register a function, use the
427+
#: :meth:`before_first_request` decorator.
429428
#:
430429
#: .. versionadded:: 0.8
431430
self.before_first_request_funcs = []
@@ -457,12 +456,11 @@ def __init__(self, import_name, static_path=None, static_url_path=None,
457456
#: .. versionadded:: 0.9
458457
self.teardown_appcontext_funcs = []
459458

460-
#: A dictionary with lists of functions that can be used as URL
461-
#: value processor functions. Whenever a URL is built these functions
462-
#: are called to modify the dictionary of values in place. The key
463-
#: ``None`` here is used for application wide
464-
#: callbacks, otherwise the key is the name of the blueprint.
465-
#: Each of these functions has the chance to modify the dictionary
459+
#: A dictionary with lists of functions that are called before the
460+
#: :attr:`before_request_funcs` functions. The key of the dictionary is
461+
#: the name of the blueprint this function is active for, or ``None``
462+
#: for all requests. To register a function, use
463+
#: :meth:`url_value_preprocessor`.
466464
#:
467465
#: .. versionadded:: 0.7
468466
self.url_value_preprocessors = {}
@@ -1314,11 +1312,13 @@ def add_template_global(self, f, name=None):
13141312
@setupmethod
13151313
def before_request(self, f):
13161314
"""Registers a function to run before each request.
1315+
1316+
For example, this can be used to open a database connection, or to load
1317+
the logged in user from the session.
13171318
1318-
The function will be called without any arguments.
1319-
If the function returns a non-None value, it's handled as
1320-
if it was the return value from the view and further
1321-
request handling is stopped.
1319+
The function will be called without any arguments. If it returns a
1320+
non-None value, the value is handled as if it was the return value from
1321+
the view, and further request handling is stopped.
13221322
"""
13231323
self.before_request_funcs.setdefault(None, []).append(f)
13241324
return f
@@ -1437,9 +1437,17 @@ def shell_context_processor(self, f):
14371437

14381438
@setupmethod
14391439
def url_value_preprocessor(self, f):
1440-
"""Registers a function as URL value preprocessor for all view
1441-
functions of the application. It's called before the view functions
1442-
are called and can modify the url values provided.
1440+
"""Register a URL value preprocessor function for all view
1441+
functions in the application. These functions will be called before the
1442+
:meth:`before_request` functions.
1443+
1444+
The function can modify the values captured from the matched url before
1445+
they are passed to the view. For example, this can be used to pop a
1446+
common language code value and place it in ``g`` rather than pass it to
1447+
every view.
1448+
1449+
The function is passed the endpoint name and values dict. The return
1450+
value is ignored.
14431451
"""
14441452
self.url_value_preprocessors.setdefault(None, []).append(f)
14451453
return f
@@ -1877,16 +1885,16 @@ def handle_url_build_error(self, error, endpoint, values):
18771885
raise error
18781886

18791887
def preprocess_request(self):
1880-
"""Called before the actual request dispatching and will
1881-
call each :meth:`before_request` decorated function, passing no
1882-
arguments.
1883-
If any of these functions returns a value, it's handled as
1884-
if it was the return value from the view and further
1885-
request handling is stopped.
1886-
1887-
This also triggers the :meth:`url_value_preprocessor` functions before
1888-
the actual :meth:`before_request` functions are called.
1888+
"""Called before the request is dispatched. Calls
1889+
:attr:`url_value_preprocessors` registered with the app and the
1890+
current blueprint (if any). Then calls :attr:`before_request_funcs`
1891+
registered with the app and the blueprint.
1892+
1893+
If any :meth:`before_request` handler returns a non-None value, the
1894+
value is handled as if it was the return value from the view, and
1895+
further request handling is stopped.
18891896
"""
1897+
18901898
bp = _request_ctx_stack.top.request.blueprint
18911899

18921900
funcs = self.url_value_preprocessors.get(None, ())

0 commit comments

Comments
 (0)