Register most stuff only once
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 7a08331 · Browse files · 2 files·+26 added−17 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+26 added−17 removed
- other:+26 added−17 removed
Provenance
- Commit
f7e71b5- Tree
ded6c3e- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
7a08331
2 files changed+26 added−17 removed
modified:flask/app.py+3 added-1 removed
581 unmodified lines582 """Registers a blueprint on the application.583 584 .. versionadded:: 0.7585 """586+ first_registration = False587 if blueprint.name in self.blueprints:588 assert self.blueprints[blueprint.name] is blueprint, \589 'A blueprint\'s name collision ocurred between %r and ' \590 '%r. Both share the same name "%s"' % \591 (blueprint, self.blueprints[blueprint.name], blueprint.name)592 else:593 self.blueprints[blueprint.name] = blueprint594+ first_registration = True593- blueprint.register(self, options)595+ blueprint.register(self, options, first_registration)596 597 def add_url_rule(self, rule, endpoint=None, view_func=None, **options):598 """Connects a URL rule. Works exactly like the :meth:`route`599 decorator. If a view_func is provided it will be registered with the529 unmodified linesmodified:flask/blueprints.py+23 added-16 removed
8 unmodified lines9 :copyright: (c) 2011 by Armin Ronacher.10 :license: BSD, see LICENSE for more details.11 """12 import os13+from functools import update_wrapper14 15 from .helpers import _PackageBoundObject, _endpoint_from_view_func16 17 1 unmodified line19 """Temporary holder object for registering a blueprint with the20 application.21 """22 22- def __init__(self, blueprint, app, options):23+ def __init__(self, blueprint, app, options, first_registration):24 self.app = app25 self.blueprint = blueprint26 self.options = options27+ self.first_registration = first_registration28 29 subdomain = self.options.get('subdomain')30 if subdomain is None:31 subdomain = self.blueprint.subdomain33 unmodified lines65 66 def _record(self, func):67 self.deferred_functions.append(func)68 67- def make_setup_state(self, app, options):68- return BlueprintSetupState(self, app, options)69+ def _record_once(self, func):70+ def wrapper(state):71+ if state.first_registration:72+ func(state)73+ return self._record(update_wrapper(wrapper, func))74 70- def register(self, app, options):75+ def make_setup_state(self, app, options, first_registration=False):76+ return BlueprintSetupState(self, app, options, first_registration)77+78+ def register(self, app, options, first_registration=False):79 """Called by :meth:`Flask.register_blueprint` to register a blueprint80 on the application. This can be overridden to customize the register81 behavior. Keyword arguments from82 :func:`~flask.Flask.register_blueprint` are directly forwarded to this83 method in the `options` dictionary.84 """77- state = self.make_setup_state(app, options)85+ state = self.make_setup_state(app, options, first_registration)86 if self.has_static_folder:87 state.add_url_rule(self.static_url_path + '/<path:filename>',88 view_func=self.send_static_file,89 endpoint='static')13 unmodified lines103 def add_url_rule(self, rule, endpoint=None, view_func=None, **options):104 """Like :meth:`Flask.add_url_rule` but for a module. The endpoint for105 the :func:`url_for` function is prefixed with the name of the module.106 """99- def register_rule(state):100- state.add_url_rule(rule, endpoint, view_func, **options)101- self._record(register_rule)107+ self._record(lambda s:108+ s.add_url_rule(rule, endpoint, view_func, **options))109 110 def endpoint(self, endpoint):111 """Like :meth:`Flask.endpoint` but for a module. This does not112 prefix the endpoint with the module name, this has to be done1 unmodified line114 """115 def decorator(f):116 def register_endpoint(state):117 state.app.view_functions[endpoint] = f111- self._record(register_endpoint)118+ self._record_once(register_endpoint)119 return f120 return decorator121 122 def before_request(self, f):123 """Like :meth:`Flask.before_request` but for a module. This function124 is only executed before each request that is handled by a function of125 that module.126 """120- self._record(lambda s: s.app.before_request_funcs127+ self._record_once(lambda s: s.app.before_request_funcs128 .setdefault(self.name, []).append(f))129 return f130 131 def before_app_request(self, f):132 """Like :meth:`Flask.before_request`. Such a function is executed133 before each request, even if outside of a module.134 """128- self._record(lambda s: s.app.before_request_funcs135+ self._record_once(lambda s: s.app.before_request_funcs136 .setdefault(None, []).append(f))137 return f138 139 def after_request(self, f):140 """Like :meth:`Flask.after_request` but for a module. This function141 is only executed after each request that is handled by a function of142 that module.143 """137- self._record(lambda s: s.app.after_request_funcs144+ self._record_once(lambda s: s.app.after_request_funcs145 .setdefault(self.name, []).append(f))146 return f147 148 def after_app_request(self, f):149 """Like :meth:`Flask.after_request` but for a module. Such a function150 is executed after each request, even if outside of the module.151 """145- self._record(lambda s: s.app.after_request_funcs152+ self._record_once(lambda s: s.app.after_request_funcs153 .setdefault(None, []).append(f))154 return f155 156 def context_processor(self, f):157 """Like :meth:`Flask.context_processor` but for a module. This158 function is only executed for requests handled by a module.159 """153- self._record(lambda s: s.app.template_context_processors160+ self._record_once(lambda s: s.app.template_context_processors161 .setdefault(self.name, []).append(f))162 return f163 164 def app_context_processor(self, f):165 """Like :meth:`Flask.context_processor` but for a module. Such a166 function is executed each request, even if outside of the module.167 """161- self._record(lambda s: s.app.template_context_processors168+ self._record_once(lambda s: s.app.template_context_processors169 .setdefault(None, []).append(f))170 return f171 172 def app_errorhandler(self, code):173 """Like :meth:`Flask.errorhandler` but for a module. This174 handler is used for all requests, even if outside of the module.175 """176 def decorator(f):170- self._record(lambda s: s.app.errorhandler(code)(f))177+ self._record_once(lambda s: s.app.errorhandler(code)(f))178 return f179 return decorator