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 ea316d2, 4f155fb · Browse files · 12 files·+153 added−37 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
12 files+153 added−37 removed
- tests:+39 added−13 removed
- other:+114 added−24 removed
Provenance
- Commit
1fa9185- Tree
005fca4- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent 1
ea316d2- Parent 2
4f155fb
12 files changed+153 added−37 removed
modified:docs/patterns/fileuploads.rst+1 added-1 removed
63 unmodified lines64 return redirect(request.url)65 if file and allowed_file(file.filename):66 filename = secure_filename(file.filename)67 file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))68- return redirect(url_for('upload_file',68+ return redirect(url_for('uploaded_file',69 filename=filename))70 return '''71 <!doctype html>72 <title>Upload new File</title>118 unmodified linesmodified:docs/api.rst+12 added-1 removed
715 unmodified lines716 def show_users(page):717 pass718 719 This specifies that ``/users/`` will be the URL for page one and720-``/users/page/N`` will be the URL for page `N`.720+``/users/page/N`` will be the URL for page ``N``.721 722+If a URL contains a default value, it will be redirected to its simpler723+form with a 301 redirect. In the above example, ``/users/page/1`` will724+be redirected to ``/users/``. If your route handles ``GET`` and ``POST``725+requests, make sure the default route only handles ``GET``, as redirects726+can't preserve form data. ::727+728+ @app.route('/region/', defaults={'id': 1})729+ @app.route('/region/<id>', methods=['GET', 'POST'])730+ def region(id):731+ pass732+733 Here are the parameters that :meth:`~flask.Flask.route` and734 :meth:`~flask.Flask.add_url_rule` accept. The only difference is that735 with the route parameter the view function is defined with the decorator736 instead of the `view_func` parameter.97 unmodified linesmodified:docs/cli.rst+24 added
199 unmodified lines200 These can be added to the ``.flaskenv`` file just like ``FLASK_APP`` to201 control default command options.202 203 204+Disable dotenv205+~~~~~~~~~~~~~~206+207+The ``flask`` command will show a message if it detects dotenv files but208+python-dotenv is not installed.209+210+.. code-block:: none211+212+ flask run213+ * Tip: There are .env files present. Do "pip install python-dotenv" to use them.214+215+You can tell Flask not to load dotenv files even when python-dotenv is216+installed by setting the ``FLASK_SKIP_DOTENV`` environment variable.217+This can be useful if you want to load them manually, or if you're using218+a project runner that loads them already. Keep in mind that the219+environment variables must be set before the app loads or it won't220+configure as expected.221+222+.. code-block:: none223+224+ export FLASK_SKIP_DOTENV=1225+ flask run226+227+228 Environment Variables From virtualenv229 -------------------------------------230 231 If you do not want to install dotenv support, you can still set environment235 unmodified linesmodified:docs/quickstart.rst+4 added-2 removed
291 unmodified lines292 return 'login'293 294 @app.route('/user/<username>')295 def profile(username):296- return '{}'s profile'.format(username)296+ return '{}\'s profile'.format(username)297 298 with app.test_request_context():299 print(url_for('index'))300 print(url_for('login'))13 unmodified lines314 a route only answers to ``GET`` requests. You can use the ``methods`` argument315 of the :meth:`~flask.Flask.route` decorator to handle different HTTP methods.316 ::317 318+ from flask import request319+320 @app.route('/login', methods=['GET', 'POST'])321 def login():322 if request.method == 'POST':323 return do_the_login()324 else:325 return show_the_login_form()326 327 If ``GET`` is present, Flask automatically adds support for the ``HEAD`` method326-and handles ``HEAD`` requests according to the the `HTTP RFC`_. Likewise,328+and handles ``HEAD`` requests according to the `HTTP RFC`_. Likewise,329 ``OPTIONS`` is automatically implemented for you.330 331 .. _HTTP RFC: https://www.ietf.org/rfc/rfc2068.txt332 519 unmodified linesmodified:flask/app.py+15 added-7 removed
25 unmodified lines26 from ._compat import integer_types, reraise, string_types, text_type27 from .config import Config, ConfigAttribute28 from .ctx import AppContext, RequestContext, _AppCtxGlobals29 from .globals import _request_ctx_stack, g, request, session30-from .helpers import _PackageBoundObject, \31- _endpoint_from_view_func, find_package, get_env, get_debug_flag, \32- get_flashed_messages, locked_cached_property, url_for30+from .helpers import (31+ _PackageBoundObject,32+ _endpoint_from_view_func, find_package, get_env, get_debug_flag,33+ get_flashed_messages, locked_cached_property, url_for, get_load_dotenv34+)35 from .logging import create_logger36 from .sessions import SecureCookieSessionInterface37 from .signals import appcontext_tearing_down, got_request_exception, \38 request_finished, request_started, request_tearing_down866 unmodified lines905 from .debughelpers import explain_ignored_app_run906 explain_ignored_app_run()907 return908 907- if load_dotenv:909+ if get_load_dotenv(load_dotenv):910 cli.load_dotenv()911 912 # if set, let env vars override previous values913 if 'FLASK_ENV' in os.environ:750 unmodified lines1664 return True1665 1666 trap_bad_request = self.config['TRAP_BAD_REQUEST_ERRORS']1667 1666- # if unset, trap based on debug mode1667- if (trap_bad_request is None and self.debug) or trap_bad_request:1668+ # if unset, trap key errors in debug mode1669+ if (1670+ trap_bad_request is None and self.debug1671+ and isinstance(e, BadRequestKeyError)1672+ ):1673+ return True1674+1675+ if trap_bad_request:1676 return isinstance(e, BadRequest)1677 1678 return False1679 250 unmodified lines1930 1931 status = headers = None1932 1933 # unpack tuple returns1926- if isinstance(rv, (tuple, list)):1934+ if isinstance(rv, tuple):1935 len_rv = len(rv)1936 1937 # a 3-tuple is unpacked directly1938 if len_rv == 3:377 unmodified linesmodified:flask/blueprints.py+4 added-6 removed
47 unmodified lines48 49 url_prefix = self.options.get('url_prefix')50 if url_prefix is None:51 url_prefix = self.blueprint.url_prefix52-52+ if url_prefix:53+ url_prefix = url_prefix.rstrip('/')54 #: The prefix that should be used for all URLs defined on the55 #: blueprint.55- if url_prefix and url_prefix[-1] == '/':56- url_prefix = url_prefix[:-1]57-56 self.url_prefix = url_prefix57 58 #: A dictionary with URL defaults that is added to each and every59 #: URL that was defined with the blueprint.4 unmodified lines64 """A helper method to register a rule (and optionally a view function)65 to the application. The endpoint is automatically prefixed with the66 blueprint's name.67 """70- if self.url_prefix:71- rule = self.url_prefix + rule68+ if self.url_prefix is not None:69+ rule = '/'.join((self.url_prefix, rule.lstrip('/')))70 options.setdefault('subdomain', self.subdomain)71 if endpoint is None:72 endpoint = _endpoint_from_view_func(view_func)73 defaults = self.url_defaults372 unmodified linesmodified:flask/cli.py+3 added-4 removed
26 unmodified lines27 28 from . import __version__29 from ._compat import getargspec, iteritems, reraise, text_type30 from .globals import current_app31-from .helpers import get_debug_flag, get_env31+from .helpers import get_debug_flag, get_env, get_load_dotenv32 33 try:34 import dotenv35 except ImportError:507 unmodified lines543 # call into a no-op. This is necessary to avoid ugly errors when the544 # script that is loaded here also attempts to start a server.545 os.environ['FLASK_RUN_FROM_CLI'] = 'true'546 547- if self.load_dotenv:547+ if get_load_dotenv(self.load_dotenv):548 load_dotenv()549 550 obj = kwargs.get('obj')551 30 unmodified lines582 :return: ``True`` if a file was loaded.583 584 .. versionadded:: 1.0585 """586-586 if dotenv is None:587 if path or os.path.exists('.env') or os.path.exists('.flaskenv'):588 click.secho(589 ' * Tip: There are .env files present.'591- ' Do "pip install python-dotenv" to use them',590+ ' Do "pip install python-dotenv" to use them.',591 fg='yellow')592 return593 594 if path is not None:300 unmodified linesmodified:flask/helpers.py+15 added
66 unmodified lines67 68 return val.lower() not in ('0', 'false', 'no')69 70 71+def get_load_dotenv(default=True):72+ """Get whether the user has disabled loading dotenv files by setting73+ :envvar:`FLASK_SKIP_DOTENV`. The default is ``True``, load the74+ files.75+76+ :param default: What to return if the env var isn't set.77+ """78+ val = os.environ.get('FLASK_SKIP_DOTENV')79+80+ if not val:81+ return default82+83+ return val.lower() in ('0', 'false', 'no')84+85+86 def _endpoint_from_view_func(view_func):87 """Internal helper that returns the default endpoint for a given88 function. This always is the function name.89 """955 unmodified linesmodified:tests/test_basic.py+19 added-6 removed
1025 unmodified lines1026 assert rv.data == b'E2'1027 1028 1029 def test_trapping_of_bad_request_key_errors(app, client):1030- @app.route('/fail')1030+ @app.route('/key')1031 def fail():1032 flask.request.form['missing_key']1033 1034+ @app.route('/abort')1035+ def allow_abort():1036+ flask.abort(400)1037+1034- rv = client.get('/fail')1038+ rv = client.get('/key')1039 assert rv.status_code == 4001040 assert b'missing_key' not in rv.data1041+ rv = client.get('/abort')1042+ assert rv.status_code == 4001043 1038- app.config['TRAP_BAD_REQUEST_ERRORS'] = True1039-1044+ app.debug = True1045 with pytest.raises(KeyError) as e:1041- client.get("/fail")1042-1046+ client.get("/key")1047 assert e.errisinstance(BadRequest)1048 assert 'missing_key' in e.value.description1049+ rv = client.get('/abort')1050+ assert rv.status_code == 4001051 1052+ app.debug = False1053+ app.config['TRAP_BAD_REQUEST_ERRORS'] = True1054+ with pytest.raises(KeyError):1055+ client.get('/key')1056+ with pytest.raises(BadRequest):1057+ client.get('/abort')1058 1059+1060 def test_trapping_of_all_http_exceptions(app, client):1061 app.config['TRAP_HTTP_EXCEPTIONS'] = True1062 1063 @app.route('/fail')899 unmodified linesmodified:tests/test_blueprints.py+12 added-7 removed
113 unmodified lines114 assert client.get('/forbidden').data == b'you shall not pass'115 assert client.get('/nope').data == b'you shall not pass'116 117 118+@pytest.mark.parametrize(('prefix', 'rule', 'url'), (119+ ('/foo/', '/bar', '/foo/bar'),120+ ('/foo/', 'bar', '/foo/bar'),121+ ('/foo', '/bar', '/foo/bar'),122+ ('/foo/', '//bar', '/foo/bar'),123+ ('/foo//', '/bar', '/foo/bar'),124+))118-def test_blueprint_prefix_slash(app, client):119- bp = flask.Blueprint('test', __name__, url_prefix='/bar/')125+def test_blueprint_prefix_slash(app, client, prefix, rule, url):126+ bp = flask.Blueprint('test', __name__, url_prefix=prefix)127 121- @bp.route('/foo')122- def foo():128+ @bp.route(rule)129+ def index():130 return '', 204131 132 app.register_blueprint(bp)126- app.register_blueprint(bp, url_prefix='/spam/')127- assert client.get('/bar/foo').status_code == 204133+ assert client.get(url).status_code == 204128- assert client.get('/spam/foo').status_code == 204134 135 136 def test_blueprint_url_defaults(app, client):137 bp = flask.Blueprint('test', __name__)713 unmodified linesmodified:tests/test_cli.py+8 added
472 unmodified lines473 load_dotenv()474 assert 'FOO' not in os.environ475 476 477+@need_dotenv478+def test_disable_dotenv_from_env(monkeypatch, runner):479+ monkeypatch.chdir(test_path)480+ monkeypatch.setitem(os.environ, 'FLASK_SKIP_DOTENV', '1')481+ runner.invoke(FlaskGroup())482+ assert 'FOO' not in os.environ483+484+485 def test_run_cert_path():486 # no key487 with pytest.raises(click.BadParameter):488 run_command.make_context('run', ['--cert', __file__])53 unmodified linesmodified:CHANGES.rst+36 added-3 removed
5 unmodified lines6 7 Version 1.18 -----------9 10-unreleased10+Unreleased11 12 13+Version 1.0.214+-------------15+16+Unreleased17+18+19 Version 1.0.120 -------------21 16-unreleased22+Released on April 29 201823 18-- Fix registering partials (with no ``__name__``) as view functions24+- Fix registering partials (with no ``__name__``) as view functions.25+ (`#2730`_)26+- Don't treat lists returned from view functions the same as tuples.27+ Only tuples are interpreted as response data. (`#2736`_)28+- Extra slashes between a blueprint's ``url_prefix`` and a route URL29+ are merged. This fixes some backwards compatibility issues with the30+ change in 1.0. (`#2731`_, `#2742`_)31+- Only trap ``BadRequestKeyError`` errors in debug mode, not all32+ ``BadRequest`` errors. This allows ``abort(400)`` to continue33+ working as expected. (`#2735`_)34+- The ``FLASK_SKIP_DOTENV`` environment variable can be set to ``1``35+ to skip automatically loading dotenv files. (`#2722`_)36 37+.. _#2722: https://github.com/pallets/flask/issues/272238+.. _#2730: https://github.com/pallets/flask/pull/273039+.. _#2731: https://github.com/pallets/flask/issues/273140+.. _#2735: https://github.com/pallets/flask/issues/273541+.. _#2736: https://github.com/pallets/flask/issues/273642+.. _#2742: https://github.com/pallets/flask/issues/274243+44+45 Version 1.046 -----------47 48 Released on April 26th 2018203 unmodified lines252 .. _#2693: https://github.com/pallets/flask/pull/2693253 .. _#2709: https://github.com/pallets/flask/pull/2709254 255 256+Version 0.12.4257+--------------258+259+Released on April 29 2018260+261+- Repackage 0.12.3 to fix package layout issue. (`#2728`_)262+263+264 Version 0.12.3265 --------------266 267 Released on April 26th 2018633 unmodified lines