Started work on URL value preprocessors
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent f7e71b5 · Browse files · 2 files·+81 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+81 added−1 removed
- other:+81 added−1 removed
Provenance
- Commit
5da1fc2- Tree
5bfbeea- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
f7e71b5
2 files changed+81 added−1 removed
modified:flask/app.py+49 added-1 removed
267 unmodified lines268 #:269 #: .. versionadded:: 0.7270 self.teardown_request_funcs = {}271 272+ #: A dictionary with lists of functions that can be used as URL273+ #: value processor functions. Whenever a URL is built these functions274+ #: are called to modify the dictionary of values in place. The key275+ #: `None` here is used for application wide276+ #: callbacks, otherwise the key is the name of the blueprint.277+ #: Each of these functions has the chance to modify the dictionary278+ #:279+ #: .. versionadded:: 0.7280+ self.url_value_preprocessors = {}281+282+ #: A dictionary with lists of functions that can be used as URL value283+ #: preprocessors. The key `None` here is used for application wide284+ #: callbacks, otherwise the key is the name of the blueprint.285+ #: Each of these functions has the chance to modify the dictionary286+ #: of URL values before they are used as the keyword arguments of the287+ #: view function. For each function registered this one should also288+ #: provide a :meth:`url_defaults` function that adds the parameters289+ #: automatically again that were removed that way.290+ #:291+ #: .. versionadded:: 0.7292+ self.url_default_functions = {}293+294 #: A dictionary with list of functions that are called without argument295 #: to populate the template context. The key of the dictionary is the296 #: name of the blueprint this function is active for, `None` for all297 #: requests. Each returns a dictionary that the template context is549 unmodified lines847 """Registers a template context processor function."""848 self.template_context_processors[None].append(f)849 return f850 851+ def url_value_preprocessor(self, f):852+ """Registers a function as URL value preprocessor for all view853+ functions of the application. It's called before the view functions854+ are called and can modify the url values provided.855+ """856+ self.url_value_preprocessors.setdefault(None, []).append(f)857+ return f858+859+ def url_defaults(self, f):860+ """Callback function for URL defaults for all view functions of the861+ application. It's called with the endpoint and values and should862+ update the values passed in place.863+ """864+ self.url_default_functions.setdefault(None, []).append(f)865+ return f866+867 def handle_http_exception(self, e):868 """Handles an HTTP exception. By default this will invoke the869 registered error handlers and fall back to returning the870 exception as response.147 unmodified lines1018 call every as :meth:`before_request` decorated function.1019 If any of these function returns a value it's handled as1020 if it was the return value from the view and further1021 request handling is stopped.1022+1023+ This also triggers the :meth:`url_value_processor` functions before1024+ the actualy :meth:`before_request` functions are called.1025 """985- funcs = self.before_request_funcs.get(None, ())1026 bp = request.blueprint1027+1028+ funcs = self.url_value_preprocessors.get(None, ())1029+ if bp is not None and bp in self.url_value_preprocessors:1030+ funcs = chain(funcs, self.url_value_preprocessors[bp])1031+ for func in funcs:1032+ func(request.endpoint, request.values)1033+1034+ funcs = self.before_request_funcs.get(None, ())1035 if bp is not None and bp in self.before_request_funcs:1036 funcs = chain(funcs, self.before_request_funcs[bp])1037 for func in funcs:1038 rv = func()138 unmodified linesmodified:flask/blueprints.py+32 added
175 unmodified lines176 def decorator(f):177 self._record_once(lambda s: s.app.errorhandler(code)(f))178 return f179 return decorator180+181+ def url_value_preprocessor(self, f):182+ """Registers a function as URL value preprocessor for this183+ blueprint. It's called before the view functions are called and184+ can modify the url values provided.185+ """186+ self._record_once(lambda s: s.app.url_value_preprocessors187+ .setdefault(self.name, []).append(f))188+ return f189+190+ def url_defaults(self, f):191+ """Callback function for URL defaults for this module. It's called192+ with the endpoint and values and should update the values passed193+ in place.194+ """195+ self._record_once(lambda s: s.app.url_default_functions196+ .setdefault(self.name, []).append(f))197+ return f198+199+ def app_url_value_preprocessor(self, f):200+ """Same as :meth:`url_value_preprocessor` but application wide.201+ """202+ self._record_once(lambda s: s.app.url_value_preprocessor203+ .setdefault(self.name, []).append(f))204+ return f205+206+ def app_url_defaults(self, f):207+ """Same as :meth:`url_defaults` but application wide.208+ """209+ self._record_once(lambda s: s.app.url_default_functions210+ .setdefault(None, []).append(f))211+ return f