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
Provenance
- Commit
f7d50d4- Tree
d8ad442- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent 1
afb7e4e- Parent 2
15c079f
10 files changed+43 added−85 removed
modified:docs/patterns/caching.rst+4 added-55 removed
8 unmodified lines9 still be good enough if they were 5 minutes old. So then the idea is that10 you actually put the result of that calculation into a cache for some11 time.12 13-Flask itself does not provide caching for you, but Werkzeug, one of the14-libraries it is based on, has some very basic cache support. It supports15-multiple cache backends, normally you want to use a memcached server.13+Flask itself does not provide caching for you, but `Flask-Caching`_, an14+extentions for Flask does. Flask-Caching supports various backends, and it is15+even possible to develop your own caching backend.16 17-Setting up a Cache18-------------------17 20-You create a cache object once and keep it around, similar to how21-:class:`~flask.Flask` objects are created. If you are using the22-development server you can create a23-:class:`~werkzeug.contrib.cache.SimpleCache` object, that one is a simple24-cache that keeps the item stored in the memory of the Python interpreter::25-26- from werkzeug.contrib.cache import SimpleCache27- cache = SimpleCache()28-29-If you want to use memcached, make sure to have one of the memcache modules30-supported (you get them from `PyPI <https://pypi.org/>`_) and a31-memcached server running somewhere. This is how you connect to such an32-memcached server then::33-34- from werkzeug.contrib.cache import MemcachedCache35- cache = MemcachedCache(['127.0.0.1:11211'])36-37-If you are using App Engine, you can connect to the App Engine memcache38-server easily::39-40- from werkzeug.contrib.cache import GAEMemcachedCache41- cache = GAEMemcachedCache()42-43-Using a Cache44--------------45-46-Now how can one use such a cache? There are two very important47-operations: :meth:`~werkzeug.contrib.cache.BaseCache.get` and48-:meth:`~werkzeug.contrib.cache.BaseCache.set`. This is how to use them:49-50-To get an item from the cache call51-:meth:`~werkzeug.contrib.cache.BaseCache.get` with a string as key name.52-If something is in the cache, it is returned. Otherwise that function53-will return ``None``::54-55- rv = cache.get('my-item')56-57-To add items to the cache, use the :meth:`~werkzeug.contrib.cache.BaseCache.set`58-method instead. The first argument is the key and the second the value59-that should be set. Also a timeout can be provided after which the cache60-will automatically remove item.61-62-Here a full example how this looks like normally::63-64- 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 lines40 # 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 response45 46 g.language = languagemodified:docs/conf.py+4 added-4 removed
20 unmodified lines21 ]22 intersphinx_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 }32 33 # HTML -----------------------------------------------------------------72 unmodified linesmodified:docs/index.rst+1 added-1 removed
1-:orphan:1+.. rst-class:: hide-header2 3 .. rst-class:: hide-header4 5 Welcome to Flask24 unmodified linesadded:docs/requirements.txt+3 added
1+Sphinx~=1.8.02+Pallets-Sphinx-Themes~=1.1.03+sphinxcontrib-log-cabinet~=1.0.0modified:flask/app.py+11 added-10 removed
1696 unmodified lines1697 # 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.1700 1701- # 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()1706 1707+ 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 = ()1712+1713 if isinstance(e, HTTPException) and not self.trap_http_exception(e):1714 return self.handle_http_exception(e)1715 1716 handler = self._find_error_handler(e)599 unmodified linesmodified:flask/cli.py+1 added-1 removed
368 unmodified lines369 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 linesmodified:tests/test_basic.py+1 added-1 removed
1043 unmodified lines1044 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 == 4001051 1052 app.debug = False910 unmodified linesmodified:tests/test_cli.py+16 added-3 removed
259 unmodified lines260 261 def 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 app267 268+ # 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 app279+280 def create_app(info):281 return Flask("createapp")282 283 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 app287 288 obj = ScriptInfo()289 pytest.raises(NoAppException, obj.load_app)290 302 unmodified linesmodified:tox.ini+1 added-10 removed
37 unmodified lines38 coverage run -p -m pytest tests examples39 40 [testenv:docs-html]41 deps =42- sphinx43- pallets-sphinx-themes44- sphinxcontrib-log-cabinet42+ -r docs/requirements.txt43 commands = sphinx-build -W -b html -d {envtmpdir}/doctrees docs {envtmpdir}/html44 47-[testenv:docs-linkcheck]48-deps =49- sphinx50- pallets-sphinx-themes51- sphinxcontrib-log-cabinet52-commands = sphinx-build -W -b linkcheck -d {envtmpdir}/doctrees docs {envtmpdir}/linkcheck53-45 [testenv:coverage-report]46 deps = coverage47 skip_install = true48 commands =12 unmodified lines