Merge branch 'master' of github.com:mitsuhiko/flask
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Jimmy McCarthy · committed by Jimmy McCarthy · parents a136019, 7f38674 · Browse files · 7 files·+38 added−29 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
7 files+38 added−29 removed
- tests:+12 added−0 removed
- other:+26 added−29 removed
Provenance
- Commit
04a3eee- Tree
ad76418- Author
Jimmy McCarthy <james.mccarthy@corvisa.com>- Committer
Jimmy McCarthy <james.mccarthy@corvisa.com>- Parent 1
a136019- Parent 2
7f38674
7 files changed+38 added−29 removed
modified:docs/deploying/wsgi-standalone.rst+2 added-2 removed
29 unmodified lines30 Gevent31 -------32 33 `Gevent`_ is a coroutine-based Python networking library that uses34-`greenlet`_ to provide a high-level synchronous API on top of `libevent`_34+`greenlet`_ to provide a high-level synchronous API on top of `libev`_35 event loop::36 37 from gevent.wsgi import WSGIServer38 from yourapplication import app2 unmodified lines41 http_server.serve_forever()42 43 .. _Gevent: http://www.gevent.org/44 .. _greenlet: http://greenlet.readthedocs.org/en/latest/45-.. _libevent: http://libevent.org/45+.. _libev: http://software.schmorp.de/pkg/libev.html46 47 Twisted Web48 -----------49 84 unmodified linesmodified:docs/patterns/wtforms.rst+12 added-12 removed
1 Form Validation with WTForms2 ============================3 4-When you have to work with form data submitted by a browser view code4+When you have to work with form data submitted by a browser view, code5 quickly becomes very hard to read. There are libraries out there designed6 to make this process easier to manage. One of them is `WTForms`_ which we7 will handle here. If you find yourself in the situation of having many8 forms, you might want to give it a try.2 unmodified lines11 first. I recommend breaking up the application into multiple modules12 (:ref:`larger-applications`) for that and adding a separate module for the13 forms.14 15-.. admonition:: Getting most of WTForms with an Extension15+.. admonition:: Getting the most out of WTForms with an Extension16 17- The `Flask-WTF`_ extension expands on this pattern and adds a few18- handful little helpers that make working with forms and Flask more17+ The `Flask-WTF`_ extension expands on this pattern and adds a18+ few little helpers that make working with forms and Flask more19 fun. You can get it from `PyPI20 <https://pypi.python.org/pypi/Flask-WTF>`_.21 22 .. _Flask-WTF: http://pythonhosted.org/Flask-WTF/30 unmodified lines53 flash('Thanks for registering')54 return redirect(url_for('login'))55 return render_template('register.html', form=form)56 57-Notice that we are implying that the view is using SQLAlchemy here58-(:ref:`sqlalchemy-pattern`) but this is no requirement of course. Adapt57+Notice we're implying that the view is using SQLAlchemy here58+(:ref:`sqlalchemy-pattern`), but that's not a requirement, of course. Adapt59 the code as necessary.60 61 Things to remember:62 63 1. create the form from the request :attr:`~flask.request.form` value if64 the data is submitted via the HTTP ``POST`` method and65 :attr:`~flask.request.args` if the data is submitted as ``GET``.66 2. to validate the data, call the :func:`~wtforms.form.Form.validate`67- method which will return ``True`` if the data validates, ``False``67+ method, which will return ``True`` if the data validates, ``False``68 otherwise.69 3. to access individual values from the form, access `form.<NAME>.data`.70 71 Forms in Templates72 ------------------73 74-Now to the template side. When you pass the form to the templates you can74+Now to the template side. When you pass the form to the templates, you can75 easily render them there. Look at the following example template to see76 how easy this is. WTForms does half the form generation for us already.77 To make it even nicer, we can write a macro that renders a field with78 label and a list of errors if there are any.15 unmodified lines94 </dd>95 {% endmacro %}96 97 This macro accepts a couple of keyword arguments that are forwarded to98-WTForm's field function that renders the field for us. The keyword99-arguments will be inserted as HTML attributes. So for example you can98+WTForm's field function, which renders the field for us. The keyword99+arguments will be inserted as HTML attributes. So, for example, you can100 call ``render_field(form.username, class='username')`` to add a class to101 the input element. Note that WTForms returns standard Python unicode102-strings, so we have to tell Jinja2 that this data is already HTML escaped102+strings, so we have to tell Jinja2 that this data is already HTML-escaped103 with the ``|safe`` filter.104 105-Here the :file:`register.html` template for the function we used above which105+Here is the :file:`register.html` template for the function we used above, which106 takes advantage of the :file:`_formhelpers.html` template:107 108 .. sourcecode:: html+jinja109 17 unmodified linesmodified:examples/jqueryexample/jqueryexample.py+3 added
21 unmodified lines22 23 @app.route('/')24 def index():25 return render_template('index.html')26+27+if __name__ == '__main__':28+ app.run()modified:flask/_compat.py+4 added-13 removed
52 unmodified lines53 return cls54 55 56 def with_metaclass(meta, *bases):57+ """Create a base class with a metaclass."""58 # This requires a bit of explanation: the basic idea is to make a59 # dummy metaclass for one level of class instantiation that replaces59- # itself with the actual metaclass. Because of internal type checks60- # we also need to make sure that we downgrade the custom metaclass61- # for one level to something closer to type (that's why __call__ and62- # __init__ comes back from type etc.).63- #64- # This has the advantage over six.with_metaclass in that it does not65- # introduce dummy classes into the final MRO.66- class metaclass(meta):67- __call__ = type.__call__68- __init__ = type.__init__60+ # itself with the actual metaclass.61+ class metaclass(type):62 def __new__(cls, name, this_bases, d):70- if this_bases is None:71- return type.__new__(cls, name, (), d)63 return meta(name, bases, d)73- return metaclass('temporary_class', None, {})64+ return type.__new__(metaclass, 'temporary_class', (), {})65 66 67 # Certain versions of pypy have a bug where clearing the exception stack68 # breaks the __exit__ function in a very peculiar way. This is currently20 unmodified linesmodified:flask/app.py+3 added-2 removed
1769 unmodified lines1770 try:1771 rv = handler(error, endpoint, values)1772 if rv is not None:1773 return rv1774- except BuildError as error:1775- pass1774+ except BuildError as e:1775+ # make error available outside except block (py3)1776+ error = e1777 1778 # At this point we want to reraise the exception. If the error is1779 # still the same one we can reraise it with the original traceback,1780 # otherwise we raise it from here.197 unmodified linesmodified:tests/test_basic.py+12 added
1067 unmodified lines1068 with app.test_request_context():1069 assert flask.url_for('spam') == '/test_handler/'1070 1071 1072+def test_build_error_handler_reraise():1073+ app = flask.Flask(__name__)1074+1075+ # Test a custom handler which reraises the BuildError1076+ def handler_raises_build_error(error, endpoint, values):1077+ raise error1078+ app.url_build_error_handlers.append(handler_raises_build_error)1079+1080+ with app.test_request_context():1081+ pytest.raises(BuildError, flask.url_for, 'not.existing')1082+1083+1084 def test_custom_converters():1085 from werkzeug.routing import BaseConverter1086 1087 class ListConverter(BaseConverter):572 unmodified linesmodified:CHANGES+2 added
92 unmodified lines93 correctly.94 - Changed logic of before first request handlers to flip the flag after95 invoking. This will allow some uses that are potentially dangerous but96 should probably be permitted.97+- Fixed Python 3 bug when a handler from `app.url_build_error_handlers`98+ reraises the `BuildError`.99 100 Version 0.10.1101 --------------102 457 unmodified lines