Deduplicate signals docs
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Triggered by #1390
authored by Markus Unterwaditzer · committed by Markus Unterwaditzer · parent 3e07b9c · Browse files · 2 files·+109 added−181 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+109 added−181 removed
- other:+109 added−181 removed
Provenance
- Commit
beec47a- Tree
07ebcd8- Author
Markus Unterwaditzer <markus@unterwaditzer.net>- Committer
Markus Unterwaditzer <markus@unterwaditzer.net>- Parent
3e07b9c
2 files changed+109 added−181 removed
modified:docs/api.rst+108 added-18 removed
506 unmodified lines507 508 .. autoclass:: flask.blueprints.BlueprintSetupState509 :members:510 511+.. _core-signals-list:512+513 Signals514 -------515 514-.. when modifying this list, also update the one in signals.rst515-516 .. versionadded:: 0.6517 518-.. data:: signals_available518+.. data:: signals.signals_available519 520 ``True`` if the signaling system is available. This is the case521 when `blinker`_ is installed.522 523+The following signals exist in Flask:524+525 .. data:: template_rendered526 527 This signal is sent when a template was successfully rendered. The528 signal is invoked with the instance of the template as `template`529 and the context as dictionary (named `context`).530 531+ Example subscriber::532+533+ def log_template_renders(sender, template, context, **extra):534+ sender.logger.debug('Rendering template "%s" with context %s',535+ template.name or 'string template',536+ context)537+538+ from flask import template_rendered539+ template_rendered.connect(log_template_renders, app)540+541 .. data:: request_started542 531- This signal is sent before any request processing started but when the532- request context was set up. Because the request context is already543+ This signal is sent when the request context is set up, before544+ any request processing happens. Because the request context is already545 bound, the subscriber can access the request with the standard global546 proxies such as :class:`~flask.request`.547 548+ Example subscriber::549+550+ def log_request(sender, **extra):551+ sender.logger.debug('Request context is set up')552+553+ from flask import request_started554+ request_started.connect(log_request, app)555+556 .. data:: request_finished557 558 This signal is sent right before the response is sent to the client.559 It is passed the response to be sent named `response`.560 561+ Example subscriber::562+563+ def log_response(sender, response, **extra):564+ sender.logger.debug('Request context is about to close down. '565+ 'Response: %s', response)566+567+ from flask import request_finished568+ request_finished.connect(log_response, app)569+570 .. data:: got_request_exception571 572 This signal is sent when an exception happens during request processing.573 It is sent *before* the standard exception handling kicks in and even574 in debug mode, where no exception handling happens. The exception575 itself is passed to the subscriber as `exception`.576 577+ Example subscriber::578+579+ def log_exception(sender, exception, **extra):580+ sender.logger.debug('Got exception during processing: %s', exception)581+582+ from flask import got_request_exception583+ got_request_exception.connect(log_exception, app)584+585 .. data:: request_tearing_down586 550- This signal is sent when the application is tearing down the request.551- This is always called, even if an error happened. An `exc` keyword552- argument is passed with the exception that caused the teardown.587+ This signal is sent when the request is tearing down. This is always588+ called, even if an exception is caused. Currently functions listening589+ to this signal are called after the regular teardown handlers, but this590+ is not something you can rely on.591 554- .. versionchanged:: 0.9555- The `exc` parameter was added.592+ Example subscriber::593 594+ def close_db_connection(sender, **extra):595+ session.close()596+597+ from flask import request_tearing_down598+ request_tearing_down.connect(close_db_connection, app)599+600+ As of Flask 0.9, this will also be passed an `exc` keyword argument601+ that has a reference to the exception that caused the teardown if602+ there was one.603+604 .. data:: appcontext_tearing_down605 559- This signal is sent when the application is tearing down the560- application context. This is always called, even if an error happened.561- An `exc` keyword argument is passed with the exception that caused the562- teardown. The sender is the application.606+ This signal is sent when the app context is tearing down. This is always607+ called, even if an exception is caused. Currently functions listening608+ to this signal are called after the regular teardown handlers, but this609+ is not something you can rely on.610 611+ Example subscriber::612+613+ def close_db_connection(sender, **extra):614+ session.close()615+616+ from flask import appcontext_tearing_down617+ appcontext_tearing_down.connect(close_db_connection, app)618+619+ This will also be passed an `exc` keyword argument that has a reference620+ to the exception that caused the teardown if there was one.621+622 .. data:: appcontext_pushed623 624 This signal is sent when an application context is pushed. The sender567- is the application.625+ is the application. This is usually useful for unittests in order to626+ temporarily hook in information. For instance it can be used to627+ set a resource early onto the `g` object.628 629+ Example usage::630+631+ from contextlib import contextmanager632+ from flask import appcontext_pushed633+634+ @contextmanager635+ def user_set(app, user):636+ def handler(sender, **kwargs):637+ g.user = user638+ with appcontext_pushed.connected_to(handler, app):639+ yield640+641+ And in the testcode::642+643+ def test_user_me(self):644+ with user_set(app, 'john'):645+ c = app.test_client()646+ resp = c.get('/users/me')647+ assert resp.data == 'username=john'648+649 .. versionadded:: 0.10650 651 .. data:: appcontext_popped652 2 unmodified lines655 :data:`appcontext_tearing_down` signal.656 657 .. versionadded:: 0.10658 659+660 .. data:: message_flashed661 662 This signal is sent when the application is flashing a message. The663 messages is sent as `message` keyword argument and the category as664 `category`.665 585- .. versionadded:: 0.10666+ Example subscriber::667 587-.. currentmodule:: None668+ recorded = []669+ def record(sender, message, category, **extra):670+ recorded.append((message, category))671 589-.. class:: flask.signals.Namespace672+ from flask import message_flashed673+ message_flashed.connect(record, app)674 675+ .. versionadded:: 0.10676+677+.. class:: signals.Namespace678+679 An alias for :class:`blinker.base.Namespace` if blinker is available,680 otherwise a dummy class that creates fake signals. This class is681 available for Flask extensions that want to provide the same fallback682 system as Flask itself.4 unmodified lines687 otherwise returns a fake signal that has a send method that will688 do nothing but will fail with a :exc:`RuntimeError` for all other689 operations, including connecting.690 691+692 .. _blinker: https://pypi.python.org/pypi/blinker693 694+695 Class-Based Views696 -----------------697 698 .. versionadded:: 0.7184 unmodified linesmodified:docs/signals.rst+1 added-163 removed
182 unmodified lines183 184 Core Signals185 ------------186 187-.. when modifying this list, also update the one in api.rst187+Take a look at :ref:`core-signals-list` for a list of all builtin signals.188 189-The following signals exist in Flask:189 191-.. data:: flask.template_rendered192- :noindex:193-194- This signal is sent when a template was successfully rendered. The195- signal is invoked with the instance of the template as `template`196- and the context as dictionary (named `context`).197-198- Example subscriber::199-200- def log_template_renders(sender, template, context, **extra):201- sender.logger.debug('Rendering template "%s" with context %s',202- template.name or 'string template',203- context)204-205- from flask import template_rendered206- template_rendered.connect(log_template_renders, app)207-208-.. data:: flask.request_started209- :noindex:210-211- This signal is sent when the request context is set up, before212- any request processing happens. Because the request context is already213- bound, the subscriber can access the request with the standard global214- proxies such as :class:`~flask.request`.215-216- Example subscriber::217-218- def log_request(sender, **extra):219- sender.logger.debug('Request context is set up')220-221- from flask import request_started222- request_started.connect(log_request, app)223-224-.. data:: flask.request_finished225- :noindex:226-227- This signal is sent right before the response is sent to the client.228- It is passed the response to be sent named `response`.229-230- Example subscriber::231-232- def log_response(sender, response, **extra):233- sender.logger.debug('Request context is about to close down. '234- 'Response: %s', response)235-236- from flask import request_finished237- request_finished.connect(log_response, app)238-239-.. data:: flask.got_request_exception240- :noindex:241-242- This signal is sent when an exception happens during request processing.243- It is sent *before* the standard exception handling kicks in and even244- in debug mode, where no exception handling happens. The exception245- itself is passed to the subscriber as `exception`.246-247- Example subscriber::248-249- def log_exception(sender, exception, **extra):250- sender.logger.debug('Got exception during processing: %s', exception)251-252- from flask import got_request_exception253- got_request_exception.connect(log_exception, app)254-255-.. data:: flask.request_tearing_down256- :noindex:257-258- This signal is sent when the request is tearing down. This is always259- called, even if an exception is caused. Currently functions listening260- to this signal are called after the regular teardown handlers, but this261- is not something you can rely on.262-263- Example subscriber::264-265- def close_db_connection(sender, **extra):266- session.close()267-268- from flask import request_tearing_down269- request_tearing_down.connect(close_db_connection, app)270-271- As of Flask 0.9, this will also be passed an `exc` keyword argument272- that has a reference to the exception that caused the teardown if273- there was one.274-275-.. data:: flask.appcontext_tearing_down276- :noindex:277-278- This signal is sent when the app context is tearing down. This is always279- called, even if an exception is caused. Currently functions listening280- to this signal are called after the regular teardown handlers, but this281- is not something you can rely on.282-283- Example subscriber::284-285- def close_db_connection(sender, **extra):286- session.close()287-288- from flask import appcontext_tearing_down289- appcontext_tearing_down.connect(close_db_connection, app)290-291- This will also be passed an `exc` keyword argument that has a reference292- to the exception that caused the teardown if there was one.293-294-.. data:: flask.appcontext_pushed295- :noindex:296-297- This signal is sent when an application context is pushed. The sender298- is the application. This is usually useful for unittests in order to299- temporarily hook in information. For instance it can be used to300- set a resource early onto the `g` object.301-302- Example usage::303-304- from contextlib import contextmanager305- from flask import appcontext_pushed306-307- @contextmanager308- def user_set(app, user):309- def handler(sender, **kwargs):310- g.user = user311- with appcontext_pushed.connected_to(handler, app):312- yield313-314- And in the testcode::315-316- def test_user_me(self):317- with user_set(app, 'john'):318- c = app.test_client()319- resp = c.get('/users/me')320- assert resp.data == 'username=john'321-322- .. versionadded:: 0.10323-324-.. data:: flask.appcontext_popped325- :noindex:326-327- This signal is sent when an application context is popped. The sender328- is the application. This usually falls in line with the329- :data:`appcontext_tearing_down` signal.330-331- .. versionadded:: 0.10332-333-334-.. data:: flask.message_flashed335- :noindex:336-337- This signal is sent when the application is flashing a message. The338- messages is sent as `message` keyword argument and the category as339- `category`.340-341- Example subscriber::342-343- recorded = []344- def record(sender, message, category, **extra):345- recorded.append((message, category))346-347- from flask import message_flashed348- message_flashed.connect(record, app)349-350- .. versionadded:: 0.10351-190 .. _blinker: https://pypi.python.org/pypi/blinker