Provide an extendable merge blueprint funcs method
This allows a Blueprint implementation that has additional funcs, such as Quart with its before_websocket_funcs (as example), to extend the merge method to also merge these functions.
This commit is contained in:
parent
0ec7f713d6
commit
72c85e80c8
1 changed files with 31 additions and 31 deletions
|
|
@ -329,37 +329,7 @@ class Blueprint(Scaffold):
|
||||||
|
|
||||||
# Merge blueprint data into parent.
|
# Merge blueprint data into parent.
|
||||||
if first_bp_registration or first_name_registration:
|
if first_bp_registration or first_name_registration:
|
||||||
|
self._merge_blueprint_funcs(app, name)
|
||||||
def extend(bp_dict, parent_dict):
|
|
||||||
for key, values in bp_dict.items():
|
|
||||||
key = name if key is None else f"{name}.{key}"
|
|
||||||
parent_dict[key].extend(values)
|
|
||||||
|
|
||||||
for key, value in self.error_handler_spec.items():
|
|
||||||
key = name if key is None else f"{name}.{key}"
|
|
||||||
value = defaultdict(
|
|
||||||
dict,
|
|
||||||
{
|
|
||||||
code: {
|
|
||||||
exc_class: func for exc_class, func in code_values.items()
|
|
||||||
}
|
|
||||||
for code, code_values in value.items()
|
|
||||||
},
|
|
||||||
)
|
|
||||||
app.error_handler_spec[key] = value
|
|
||||||
|
|
||||||
for endpoint, func in self.view_functions.items():
|
|
||||||
app.view_functions[endpoint] = func
|
|
||||||
|
|
||||||
extend(self.before_request_funcs, app.before_request_funcs)
|
|
||||||
extend(self.after_request_funcs, app.after_request_funcs)
|
|
||||||
extend(
|
|
||||||
self.teardown_request_funcs,
|
|
||||||
app.teardown_request_funcs,
|
|
||||||
)
|
|
||||||
extend(self.url_default_functions, app.url_default_functions)
|
|
||||||
extend(self.url_value_preprocessors, app.url_value_preprocessors)
|
|
||||||
extend(self.template_context_processors, app.template_context_processors)
|
|
||||||
|
|
||||||
for deferred in self.deferred_functions:
|
for deferred in self.deferred_functions:
|
||||||
deferred(state)
|
deferred(state)
|
||||||
|
|
@ -406,6 +376,36 @@ class Blueprint(Scaffold):
|
||||||
bp_options["name_prefix"] = name
|
bp_options["name_prefix"] = name
|
||||||
blueprint.register(app, bp_options)
|
blueprint.register(app, bp_options)
|
||||||
|
|
||||||
|
def _merge_blueprint_funcs(self, app: App, name: str) -> None:
|
||||||
|
def extend(bp_dict, parent_dict):
|
||||||
|
for key, values in bp_dict.items():
|
||||||
|
key = name if key is None else f"{name}.{key}"
|
||||||
|
parent_dict[key].extend(values)
|
||||||
|
|
||||||
|
for key, value in self.error_handler_spec.items():
|
||||||
|
key = name if key is None else f"{name}.{key}"
|
||||||
|
value = defaultdict(
|
||||||
|
dict,
|
||||||
|
{
|
||||||
|
code: {exc_class: func for exc_class, func in code_values.items()}
|
||||||
|
for code, code_values in value.items()
|
||||||
|
},
|
||||||
|
)
|
||||||
|
app.error_handler_spec[key] = value
|
||||||
|
|
||||||
|
for endpoint, func in self.view_functions.items():
|
||||||
|
app.view_functions[endpoint] = func
|
||||||
|
|
||||||
|
extend(self.before_request_funcs, app.before_request_funcs)
|
||||||
|
extend(self.after_request_funcs, app.after_request_funcs)
|
||||||
|
extend(
|
||||||
|
self.teardown_request_funcs,
|
||||||
|
app.teardown_request_funcs,
|
||||||
|
)
|
||||||
|
extend(self.url_default_functions, app.url_default_functions)
|
||||||
|
extend(self.url_value_preprocessors, app.url_value_preprocessors)
|
||||||
|
extend(self.template_context_processors, app.template_context_processors)
|
||||||
|
|
||||||
@setupmethod
|
@setupmethod
|
||||||
def add_url_rule(
|
def add_url_rule(
|
||||||
self,
|
self,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue