Skip to content
Myr

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

7 files+38 added−29 removed

modified:docs/deploying/wsgi-standalone.rst+2 added-2 removed
29 unmodified lines
30Gevent31-------3233`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`_35event loop::3637 from gevent.wsgi import WSGIServer38 from yourapplication import app
2 unmodified lines
41 http_server.serve_forever()4243.. _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.html4647Twisted Web48-----------49
84 unmodified lines
modified:docs/patterns/wtforms.rst+12 added-12 removed
1Form Validation with WTForms2============================34When you have to work with form data submitted by a browser view code4When you have to work with form data submitted by a browser view, code5quickly becomes very hard to read. There are libraries out there designed6to make this process easier to manage. One of them is `WTForms`_ which we7will handle here. If you find yourself in the situation of having many8forms, you might want to give it a try.
2 unmodified lines
11first. I recommend breaking up the application into multiple modules12(:ref:`larger-applications`) for that and adding a separate module for the13forms.1415.. admonition:: Getting most of WTForms with an Extension15.. admonition:: Getting the most out of WTForms with an Extension1617 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>`_.2122.. _Flask-WTF: http://pythonhosted.org/Flask-WTF/
30 unmodified lines
53 flash('Thanks for registering')54 return redirect(url_for('login'))55 return render_template('register.html', form=form)5657Notice that we are implying that the view is using SQLAlchemy here58(:ref:`sqlalchemy-pattern`) but this is no requirement of course. Adapt57Notice we're implying that the view is using SQLAlchemy here58(:ref:`sqlalchemy-pattern`), but that's not a requirement, of course. Adapt59the code as necessary.6061Things to remember:62631. 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``.662. 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.693. to access individual values from the form, access `form.<NAME>.data`.7071Forms in Templates72------------------7374Now to the template side. When you pass the form to the templates you can74Now to the template side. When you pass the form to the templates, you can75easily render them there. Look at the following example template to see76how easy this is. WTForms does half the form generation for us already.77To make it even nicer, we can write a macro that renders a field with78label and a list of errors if there are any.
15 unmodified lines
94 </dd>95 {% endmacro %}9697This macro accepts a couple of keyword arguments that are forwarded to98WTForm's field function that renders the field for us. The keyword99arguments will be inserted as HTML attributes. So for example you can98WTForm's field function, which renders the field for us. The keyword99arguments will be inserted as HTML attributes. So, for example, you can100call ``render_field(form.username, class='username')`` to add a class to101the input element. Note that WTForms returns standard Python unicode102strings, so we have to tell Jinja2 that this data is already HTML escaped102strings, so we have to tell Jinja2 that this data is already HTML-escaped103with the ``|safe`` filter.104105Here the :file:`register.html` template for the function we used above which105Here is the :file:`register.html` template for the function we used above, which106takes advantage of the :file:`_formhelpers.html` template:107108.. sourcecode:: html+jinja109
17 unmodified lines
modified:examples/jqueryexample/jqueryexample.py+3 added
21 unmodified lines
2223@app.route('/')24def index():25 return render_template('index.html')2627if __name__ == '__main__':28 app.run()
modified:flask/_compat.py+4 added-13 removed
52 unmodified lines
53 return cls545556def 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', (), {})656667# Certain versions of pypy have a bug where clearing the exception stack68# breaks the __exit__ function in a very peculiar way. This is currently
20 unmodified lines
modified:flask/app.py+3 added-2 removed
1769 unmodified lines
1770 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 = e17771778 # 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 lines
modified:tests/test_basic.py+12 added
1067 unmodified lines
1068 with app.test_request_context():1069 assert flask.url_for('spam') == '/test_handler/'107010711072def test_build_error_handler_reraise():1073 app = flask.Flask(__name__)10741075 # 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)10791080 with app.test_request_context():1081 pytest.raises(BuildError, flask.url_for, 'not.existing')108210831084def test_custom_converters():1085 from werkzeug.routing import BaseConverter10861087 class ListConverter(BaseConverter):
572 unmodified lines
modified:CHANGES+2 added
92 unmodified lines
93 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`.99100Version 0.10.1101--------------102
457 unmodified lines