Skip to content
Myr

Merge branch '1.0-maintenance'

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

authored by David Lord · committed by David Lord · parents afb7e4e, 15c079f · Browse files · 10 files+43 added−85 removed
This commit

State

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

Diff stats

10 files+43 added−85 removed

  • tests:+17 added−4 removed
  • other:+26 added−81 removed

10 files+43 added−85 removed

modified:docs/patterns/caching.rst+4 added-55 removed
8 unmodified lines
9still be good enough if they were 5 minutes old. So then the idea is that10you actually put the result of that calculation into a cache for some11time.1213Flask itself does not provide caching for you, but Werkzeug, one of the14libraries it is based on, has some very basic cache support. It supports15multiple cache backends, normally you want to use a memcached server.13Flask itself does not provide caching for you, but `Flask-Caching`_, an14extentions for Flask does. Flask-Caching supports various backends, and it is15even possible to develop your own caching backend.1617Setting up a Cache18------------------1720You create a cache object once and keep it around, similar to how21:class:`~flask.Flask` objects are created. If you are using the22development server you can create a23:class:`~werkzeug.contrib.cache.SimpleCache` object, that one is a simple24cache that keeps the item stored in the memory of the Python interpreter::2526 from werkzeug.contrib.cache import SimpleCache27 cache = SimpleCache()2829If you want to use memcached, make sure to have one of the memcache modules30supported (you get them from `PyPI <https://pypi.org/>`_) and a31memcached server running somewhere. This is how you connect to such an32memcached server then::3334 from werkzeug.contrib.cache import MemcachedCache35 cache = MemcachedCache(['127.0.0.1:11211'])3637If you are using App Engine, you can connect to the App Engine memcache38server easily::3940 from werkzeug.contrib.cache import GAEMemcachedCache41 cache = GAEMemcachedCache()4243Using a Cache44-------------4546Now how can one use such a cache? There are two very important47operations: :meth:`~werkzeug.contrib.cache.BaseCache.get` and48:meth:`~werkzeug.contrib.cache.BaseCache.set`. This is how to use them:4950To get an item from the cache call51:meth:`~werkzeug.contrib.cache.BaseCache.get` with a string as key name.52If something is in the cache, it is returned. Otherwise that function53will return ``None``::5455 rv = cache.get('my-item')5657To add items to the cache, use the :meth:`~werkzeug.contrib.cache.BaseCache.set`58method instead. The first argument is the key and the second the value59that should be set. Also a timeout can be provided after which the cache60will automatically remove item.6162Here a full example how this looks like normally::6364 def get_my_item():65 rv = cache.get('my-item')66 if rv is None:67 rv = calculate_value()68 cache.set('my-item', rv, timeout=5 * 60)69 return rv18.. _Flask-Caching: https://flask-caching.readthedocs.io/en/latest/
modified:docs/patterns/deferredcallbacks.rst+1 added
39 unmodified lines
40 # when the response exists, set a cookie with the language41 @after_this_request42 def remember_language(response):43 response.set_cookie('user_lang', language)44 return response4546 g.language = language
modified:docs/conf.py+4 added-4 removed
20 unmodified lines
21]22intersphinx_mapping = {23 "python": ("https://docs.python.org/3/", None),24 "werkzeug": ("http://werkzeug.pocoo.org/docs/", None),25 "click": ("http://click.pocoo.org/", None),25 "click": ("https://click.palletsprojects.com/", None),26 "jinja": ("http://jinja.pocoo.org/docs/", None),27 "itsdangerous": ("https://pythonhosted.org/itsdangerous", None),28 "sqlalchemy": ("https://docs.sqlalchemy.org/en/latest/", None),29 "wtforms": ("https://wtforms.readthedocs.io/en/latest/", None),27 "itsdangerous": ("https://itsdangerous.palletsprojects.com/", None),28 "sqlalchemy": ("https://docs.sqlalchemy.org/", None),29 "wtforms": ("https://wtforms.readthedocs.io/en/stable/", None),30 "blinker": ("https://pythonhosted.org/blinker/", None),31}3233# HTML -----------------------------------------------------------------
72 unmodified lines
modified:docs/index.rst+1 added-1 removed
1:orphan:1.. rst-class:: hide-header23.. rst-class:: hide-header45Welcome to Flask
24 unmodified lines
added:docs/requirements.txt+3 added
1Sphinx~=1.8.02Pallets-Sphinx-Themes~=1.1.03sphinxcontrib-log-cabinet~=1.0.0
modified:flask/app.py+11 added-10 removed
1696 unmodified lines
1697 # wants the traceback preserved in handle_http_exception. Of course1698 # we cannot prevent users from trashing it themselves in a custom1699 # trap_http_exception method so that's their fault then.17001701 # MultiDict passes the key to the exception, but that's ignored1702 # when generating the response message. Set an informative1703 # description for key errors in debug mode or when trapping errors.1704 if (1705 (self.debug or self.config['TRAP_BAD_REQUEST_ERRORS'])1706 and isinstance(e, BadRequestKeyError)1707 # only set it if it's still the default description1708 and e.description is BadRequestKeyError.description1709 ):1710 e.description = "KeyError: '{0}'".format(*e.args)1701 if isinstance(e, BadRequestKeyError):1702 if self.debug or self.config["TRAP_BAD_REQUEST_ERRORS"]:1703 # Werkzeug < 0.15 doesn't add the KeyError to the 4001704 # message, add it in manually.1705 description = e.get_description()17061707 if e.args[0] not in description:1708 e.description = "KeyError: '{}'".format(*e.args)1709 else:1710 # Werkzeug >= 0.15 does add it, remove it in production1711 e.args = ()17121713 if isinstance(e, HTTPException) and not self.trap_http_exception(e):1714 return self.handle_http_exception(e)17151716 handler = self._find_error_handler(e)
599 unmodified lines
modified:flask/cli.py+1 added-1 removed
368 unmodified lines
369 if self.create_app is not None:370 app = call_factory(self, self.create_app)371 else:372 if self.app_import_path:373 path, name = (self.app_import_path.split(':', 1) + [None])[:2]373 path, name = (re.split(r':(?![\\/])', self.app_import_path, 1) + [None])[:2]374 import_name = prepare_import(path)375 app = locate_app(self, import_name, name)376 else:377 for path in ('wsgi.py', 'app.py'):
527 unmodified lines
modified:tests/test_basic.py+1 added-1 removed
1043 unmodified lines
1044 app.debug = True1045 with pytest.raises(KeyError) as e:1046 client.get("/key")1047 assert e.errisinstance(BadRequest)1048 assert 'missing_key' in e.value.description1048 assert 'missing_key' in e.value.get_description()1049 rv = client.get('/abort')1050 assert rv.status_code == 40010511052 app.debug = False
910 unmodified lines
modified:tests/test_cli.py+16 added-3 removed
259 unmodified lines
260261def test_scriptinfo(test_apps, monkeypatch):262 """Test of ScriptInfo."""263 obj = ScriptInfo(app_import_path="cliapp.app:testapp")264 assert obj.load_app().name == "testapp"265 assert obj.load_app().name == "testapp"264 app = obj.load_app()265 assert app.name == "testapp"266 assert obj.load_app() is app267268 # import app with module's absolute path269 cli_app_path = os.path.abspath(os.path.join(270 os.path.dirname(__file__), 'test_apps', 'cliapp', 'app.py'))271 obj = ScriptInfo(app_import_path=cli_app_path)272 app = obj.load_app()273 assert app.name == 'testapp'274 assert obj.load_app() is app275 obj = ScriptInfo(app_import_path=cli_app_path + ':testapp')276 app = obj.load_app()277 assert app.name == 'testapp'278 assert obj.load_app() is app279280 def create_app(info):281 return Flask("createapp")282283 obj = ScriptInfo(create_app=create_app)284 app = obj.load_app()285 assert app.name == "createapp"273 assert obj.load_app() == app286 assert obj.load_app() is app287288 obj = ScriptInfo()289 pytest.raises(NoAppException, obj.load_app)290
302 unmodified lines
modified:tox.ini+1 added-10 removed
37 unmodified lines
38 coverage run -p -m pytest tests examples3940[testenv:docs-html]41deps =42 sphinx43 pallets-sphinx-themes44 sphinxcontrib-log-cabinet42 -r docs/requirements.txt43commands = sphinx-build -W -b html -d {envtmpdir}/doctrees docs {envtmpdir}/html4447[testenv:docs-linkcheck]48deps =49 sphinx50 pallets-sphinx-themes51 sphinxcontrib-log-cabinet52commands = sphinx-build -W -b linkcheck -d {envtmpdir}/doctrees docs {envtmpdir}/linkcheck5345[testenv:coverage-report]46deps = coverage47skip_install = true48commands =
12 unmodified lines