@@ -415,17 +415,16 @@ def __init__(self, import_name, static_path=None, static_url_path=None,
415
415
#: .. versionadded:: 0.9
416
416
self .url_build_error_handlers = []
417
417
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.
424
423
self .before_request_funcs = {}
425
424
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.
429
428
#:
430
429
#: .. versionadded:: 0.8
431
430
self .before_first_request_funcs = []
@@ -457,12 +456,11 @@ def __init__(self, import_name, static_path=None, static_url_path=None,
457
456
#: .. versionadded:: 0.9
458
457
self .teardown_appcontext_funcs = []
459
458
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`.
466
464
#:
467
465
#: .. versionadded:: 0.7
468
466
self .url_value_preprocessors = {}
@@ -1314,11 +1312,13 @@ def add_template_global(self, f, name=None):
1314
1312
@setupmethod
1315
1313
def before_request (self , f ):
1316
1314
"""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.
1317
1318
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.
1322
1322
"""
1323
1323
self .before_request_funcs .setdefault (None , []).append (f )
1324
1324
return f
@@ -1437,9 +1437,17 @@ def shell_context_processor(self, f):
1437
1437
1438
1438
@setupmethod
1439
1439
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.
1443
1451
"""
1444
1452
self .url_value_preprocessors .setdefault (None , []).append (f )
1445
1453
return f
@@ -1877,16 +1885,16 @@ def handle_url_build_error(self, error, endpoint, values):
1877
1885
raise error
1878
1886
1879
1887
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.
1889
1896
"""
1897
+
1890
1898
bp = _request_ctx_stack .top .request .blueprint
1891
1899
1892
1900
funcs = self .url_value_preprocessors .get (None , ())
0 commit comments