Skip to content
Myr

Added the ability to trigger functions before the first request to the application

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 5500986 · Browse files · 4 files+74 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+74 added−2 removed

  • tests:+15 added−0 removed
  • other:+59 added−2 removed

4 files+74 added−2 removed

modified:flask/app.py+50 added-2 removed
289 unmodified lines
290 #: getting hold of the currently logged in user. To register a291 #: function here, use the :meth:`before_request` decorator.292 self.before_request_funcs = {}293294 #: A lists of functions that should be called at the beginning of the295 #: first request to this instance. To register a function here, use296 #: the :meth:`before_first_request` decorator.297 #:298 #: .. versionadded:: 0.8299 self.before_first_request_funcs = []300301 #: A dictionary with lists of functions that should be called after302 #: each request. The key of the dictionary is the name of the blueprint303 #: this function is active for, `None` for all requests. This can for304 #: example be used to open database connections or getting hold of the
87 unmodified lines
392393 # tracks internally if the application already handled at least one394 # request.395 self._got_first_request = False396 self._before_request_lock = Lock()397398 # register the static folder for the application. Do that even399 # if the folder does not exist. First of all it might be created400 # while the server is running (usually happens during development)
80 unmodified lines
481 self.init_jinja_globals()482483 return rv484485 @property486 def got_first_request(self):487 """This attribute is set to `True` if the application started488 handling the first request.489490 .. versionadded:: 0.8491 """492 return self._got_first_request493494 def create_jinja_environment(self):495 """Creates the Jinja2 environment based on :attr:`jinja_options`496 and :meth:`select_jinja_autoescape`. Since 0.7 this also adds497 the Jinja2 globals and filters after initialization. Override
99 unmodified lines
597 if 'debug' in options:598 self.debug = options.pop('debug')599 options.setdefault('use_reloader', self.debug)600 options.setdefault('use_debugger', self.debug)601 try:584 return run_simple(host, port, self, **options)602 run_simple(host, port, self, **options)603 finally:604 # reset the first request information if the development server605 # resetted normally. This makes it possible to restart the server606 # without reloader and that stuff from an interactive shell.607 self._got_first_request = False608609 def test_client(self, use_cookies=True):610 """Creates a test client for this application. For information611 about unit testing head over to :ref:`testing`.
351 unmodified lines
963 self.before_request_funcs.setdefault(None, []).append(f)964 return f965966 @setupmethod967 def before_first_request(self, f):968 """Registers a function to be run before the first request to this969 instance of the application.970971 .. versionadded:: 0.8972 """973 self.before_first_request_funcs.append(f)974975 @setupmethod976 def after_request(self, f):977 """Register a function to be run after each request. Your function978 must take one parameter, a :attr:`response_class` object and return979 a new response object or the same (see :meth:`process_response`).
182 unmodified lines
1162 error handling.11631164 .. versionadded:: 0.71165 """1134 self._got_first_request = True1166 self.try_trigger_before_first_request_functions()1167 try:1168 request_started.send(self)1169 rv = self.preprocess_request()1170 if rv is None:
4 unmodified lines
1175 response = self.process_response(response)1176 request_finished.send(self, response=response)1177 return response11781179 def try_trigger_before_first_request_functions(self):1180 """Called before each request and will ensure that it triggers1181 the :attr:`before_first_request_funcs` and only exactly once per1182 application instance (which means process usually).11831184 .. versionadded:: 0.81185 """1186 if self._got_first_request:1187 return1188 with self._before_request_lock:1189 if self._got_first_request:1190 return1191 self._got_first_request = True1192 for func in self.before_first_request_funcs:1193 func()11941195 def make_default_options_response(self):1196 """This method is called to create the default `OPTIONS` response.1197 This can be changed through subclassing to change the default1198 behaviour of `OPTIONS` responses.
235 unmodified lines
modified:flask/blueprints.py+7 added
197 unmodified lines
198 self.record_once(lambda s: s.app.before_request_funcs199 .setdefault(None, []).append(f))200 return f201202 def before_app_first_request(self, f):203 """Like :meth:`Flask.before_first_request`. Such a function is204 executed before the first request to the application.205 """206 self.record_once(lambda s: s.app.before_first_request_funcs.append(f))207 return f208209 def after_request(self, f):210 """Like :meth:`Flask.after_request` but for a blueprint. This function211 is only executed after each request that is handled by a function of212 that blueprint.
106 unmodified lines
modified:tests/flask_tests.py+15 added
948 unmodified lines
949 app.debug = True950 @app.route('/')951 def index():952 return 'Awesome'953 self.assert_(not app.got_first_request)954 self.assertEqual(app.test_client().get('/').data, 'Awesome')955 try:956 @app.route('/foo')957 def broken():
7 unmodified lines
965 @app.route('/foo')966 def working():967 return 'Meh'968 self.assertEqual(app.test_client().get('/foo').data, 'Meh')969 self.assert_(app.got_first_request)970971 def test_before_first_request_functions(self):972 got = []973 app = flask.Flask(__name__)974 @app.before_first_request975 def foo():976 got.append(42)977 c = app.test_client()978 c.get('/')979 self.assertEqual(got, [42])980 c.get('/')981 self.assertEqual(got, [42])982 self.assert_(app.got_first_request)983984985class JSONTestCase(unittest.TestCase):986987 def test_json_body_encoding(self):988 app = flask.Flask(__name__)
1088 unmodified lines
modified:CHANGES+2 added
19 unmodified lines
20 warn you about them.21- Flask in debug mode will now complain with an assertion error if a view22 was attached after the first request was handled. This gives earlier23 feedback when users forget to import view code ahead of time.24- Added the ability to register callbacks that are only triggered once at25 the beginning of the first request. (:meth:`Flask.before_first_request`)2627Version 0.7.328-------------29
238 unmodified lines