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 Armin Ronacher · committed by Armin Ronacher · parents 405c299, 325b960 · Browse files · 22 files+56 added−51 removed
This commit

State

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

Diff stats

22 files+56 added−51 removed

  • other:+56 added−51 removed

22 files+56 added−51 removed

modified:docs/deploying/cgi.rst+1 added-1 removed
36 unmodified lines
37Server Setup38------------3940Usually there are two ways to configure the server. Either just copy the41`.cgi` into a `cgi-bin` (and use `mod_rerwite` or something similar to41`.cgi` into a `cgi-bin` (and use `mod_rewrite` or something similar to42rewrite the URL) or let the server point to the file directly.4344In Apache for example you can put a like like this into the config:45
5 unmodified lines
modified:docs/deploying/fastcgi.rst+1 added-1 removed
121 unmodified lines
122 $ su www-data123 $ cd /var/www/yourapplication124 $ python application.fcgi125 Traceback (most recent call last):126 File "yourapplication.fcg", line 4, in <module>126 File "yourapplication.fcgi", line 4, in <module>127 ImportError: No module named yourapplication128129In this case the error seems to be "yourapplication" not being on the python130path. Common problems are:
9 unmodified lines
modified:docs/deploying/mod_wsgi.rst+2 added-2 removed
91 unmodified lines
92.. _installation instructions: http://code.google.com/p/modwsgi/wiki/QuickInstallationGuide93.. _virtual python: http://pypi.python.org/pypi/virtualenv94.. _mod_wsgi wiki: http://code.google.com/p/modwsgi/wiki/9596Toubleshooting97--------------96Troubleshooting97---------------9899If your application does not run, follow this guide to troubleshoot:100101**Problem:** application does not run, errorlog shows SystemExit ignored
65 unmodified lines
modified:docs/deploying/others.rst+2 added-2 removed
67 unmodified lines
68If you deploy your application behind an HTTP proxy you will need to69rewrite a few headers in order for the application to work. The two70problematic values in the WSGI environment usually are `REMOTE_ADDR` and71`HTTP_HOST`. Werkzeug ships a fixer that will solve some common setups,72but you might want to write your own WSGI middlware for specific setups.72but you might want to write your own WSGI middleware for specific setups.7374The most common setup invokes the host being set from `X-Forwarded-Host`75and the remote address from `X-Forwared-For`::75and the remote address from `X-Forward-For`::7677 from werkzeug.contrib.fixers import ProxyFix78 app.wsgi_app = ProxyFix(app.wsgi_app)79
19 unmodified lines
modified:docs/patterns/errorpages.rst+1 added-1 removed
31 unmodified lines
32 resources that previously existed and got deleted answer with 41033 instead of 404. If you are not deleting documents permanently from34 the database but just mark them as deleted, do the user a favour and35 use the 410 code instead and display a message that what he was36 looking for was deleted for all ethernity.36 looking for was deleted for all eternity.3738*500 Internal Server Error*39 Usually happens on programming errors or if the server is overloaded.40 A terrible good idea to have a nice page there, because your
37 unmodified lines
modified:docs/patterns/fileuploads.rst+3 added-3 removed
39 unmodified lines
4041Why do we limit the extensions that are allowed? You probably don't want42your users to be able to upload everything there if the server is directly43sending out the data to the client. That way you can make sure that users44are not able to upload HTML files that would cause XSS problems. Also45make sure to disallow `.php` files if the server executes them, but who46has PHP installed on his server, right? :)44are not able to upload HTML files that would cause XSS problems (see45:ref:`xss`). Also make sure to disallow `.php` files if the server46executes them, but who has PHP installed on his server, right? :)4748Next the functions that check if an extension is valid and that uploads49the file and redirects the user to the URL for the uploaded file::50
130 unmodified lines
modified:docs/patterns/flashing.rst+2 added-2 removed
28 unmodified lines
29 if request.form['username'] != 'admin' or \30 request.form['password'] != 'secret':31 error = 'Invalid credentials'32 else:33 flash('You were sucessfully logged in')33 flash('You were successfully logged in')34 return redirect(url_for('index'))35 return render_template('login.html', error=error)3637And here the ``layout.html`` template which does the magic:
61 unmodified lines
99 flash(u'Invalid password provided', 'error')100101Inside the template you then have to tell the102:func:`~flask.get_flashed_messages` function to also return the103categories. The loop looks slighty different in that situation then:103categories. The loop looks slightly different in that situation then:104105.. sourcecode:: html+jinja106107 {% with messages = get_flashed_messages(with_categories=true) %}
12 unmodified lines
modified:docs/patterns/lazyloading.rst+1 added-1 removed
98 unmodified lines
99 url('/', 'views.index')100 url('/user/<username>', 'views.user')101102One thing to keep in mind is that before and after request handlers have103to be in a file that is imported upfront to work propery on the first103to be in a file that is imported upfront to work properly on the first104request. The same goes for any kind of remaining decorator.
modified:docs/patterns/mongokit.rst+1 added-1 removed
76 unmodified lines
77validator for the maximum character length and uses a special MongoKit feature78called `use_dot_notation`. Per default MongoKit behaves like a python79dictionary but with `use_dot_notation` set to `True` you can use your80documents like you use models in nearly any other ORM by using dots to81seperate between attributes.81separate between attributes.8283You can insert entries into the database like this:8485>>> from yourapplication.database import connection
59 unmodified lines
modified:docs/patterns/packages.rst+6 added-1 removed
160 unmodified lines
161 from yourapplication.views.admin import admin162 from yourapplication.views.frontend import frontend163164 app = Flask(__name__)165 app.register_module(admin)165 app.register_module(admin, url_prefix='/admin')166 app.register_module(frontend)167168We register the modules with the app so that it can add them to the169URL map for our application. Note the prefix argument to the admin170module: by default when we register a module, that module's end-points171will be registered on `/` unless we specify this argument.172173So what is different when working with modules? It mainly affects URL174generation. Remember the :func:`~flask.url_for` function? When not175working with modules it accepts the name of the function as first176argument. This first argument is called the "endpoint". When you are
110 unmodified lines
modified:docs/patterns/sqlite3.rst+1 added-1 removed
59 unmodified lines
60 print the_username, 'has the id', user['user_id']6162To pass variable parts to the SQL statement, use a question mark in the63statement and pass in the arguments as a list. Never directly add them to64the SQL statement with string formattings because this makes it possible64the SQL statement with string formatting because this makes it possible65to attack the application using `SQL Injections66<http://en.wikipedia.org/wiki/SQL_injection>`_.6768Initial Schemas
19 unmodified lines
modified:docs/patterns/viewdecorators.rst+1 added-1 removed
87 unmodified lines
88 return rv89 return decorated_function90 return decorator9192Notice that this assumes an instanciated `cache` object is available, see92Notice that this assumes an instantiated `cache` object is available, see93:ref:`caching-pattern` for more information.949596Templating Decorator
51 unmodified lines
modified:docs/tutorial/setup.rst+1 added-1 removed
74 unmodified lines
75focus on that a little later. First we should get the database working.7677.. admonition:: Externally Visible Server7879 Want your server to be publically available? Check out the79 Want your server to be publicly available? Check out the80 :ref:`externally visible server <public-server>` section for more81 information.8283Continue with :ref:`tutorial-dbinit`.
modified:docs/api.rst+2 added-2 removed
419 unmodified lines
420Notes On Proxies421----------------422423Some of the objects provided by Flask are proxies to other objects. The424reason behind this is, that these proxies are shared between threads and424reason behind this is that these proxies are shared between threads and425they have to dispatch to the actual object bound to a thread behind the426scenes as necessary.427428Most of the time you don't have to care about that, but there are some429exceptions where it is good to know that this object is an actual proxy:430431- The proxy objects do not fake their inherited types, so if you want to432 perform actual instance checks, you have to do that on the instance433 that433 that is being proxied (see `_get_current_object` below).434- if the object reference is important (so for example for sending435 :ref:`signals`)436437If you need to get access to the underlying object that is proxied, you
4 unmodified lines
modified:docs/becomingbig.rst+1 added-1 removed
8 unmodified lines
910Flask is powered by Werkzeug and Jinja2, two libraries that are in use at11a number of large websites out there and all Flask does is bring those12two together. Being a microframework Flask does not do much more than13combinding existing libraries - there is not a lot of code involved.13combining existing libraries - there is not a lot of code involved.14What that means for large applications is that it's very easy to take the15code from Flask and put it into a new module within the applications and16expand on that.17
71 unmodified lines
modified:docs/config.rst+2 added-2 removed
59 unmodified lines
60 :class:`datetime.timedelta` object.61``USE_X_SENDFILE`` enable/disable x-sendfile62``LOGGER_NAME`` the name of the logger63``SERVER_NAME`` the name of the server. Required for64 subdomain support (eg: ``'localhost'``)64 subdomain support (e.g.: ``'localhost'``)65``MAX_CONTENT_LENGTH`` If set to a value in bytes, Flask will66 reject incoming requests with a67 content length greater than this by68 returning a 413 status code.
152 unmodified lines
221 code at all. If you are working often on different projects you can222 even create your own script for sourcing that activates a virtualenv223 and exports the development configuration for you.224- Use a tool like `fabric`_ in production to push code and225 configurations sepearately to the production server(s). For some225 configurations separately to the production server(s). For some226 details about how to do that, head over to the :ref:`deploy` pattern.227228.. _fabric: http://fabfile.org/
modified:docs/extensiondev.rst+1 added-1 removed
151 unmodified lines
152There are two recommended ways for an extension to initialize:153154initialization functions:155 If your extension is called `helloworld` you might have a function156 called ``init_helloworld(app[, extra_args])`` that initalizes the156 called ``init_helloworld(app[, extra_args])`` that initializes the157 extension for that application. It could attach before / after158 handlers etc.159160classes:
167 unmodified lines
modified:docs/license.rst+1 added-1 removed
2 unmodified lines
34Flask is licensed under a three clause BSD License. It basically means:5do whatever you want with it as long as the copyright in Flask sticks6around, the conditions are not modified and the disclaimer is present.7Furthermore you must not use the names of the authors to promote derivates7Furthermore you must not use the names of the authors to promote derivatives8of the software without written consent.910The full license text can be found below (:ref:`flask-license`). For the11documentation and artwork different licenses apply.
37 unmodified lines
modified:docs/security.rst+21 added-21 removed
60 unmodified lines
61Another big problem is CSRF. This is a very complex topic and I won't62outline it here in detail just mention what it is and how to theoretically63prevent it.6465So if your authentication information is stored in cookies you have66implicit state management. By that I mean that the state of "being logged67in" is controlled by a cookie and that cookie is sent with each request to68a page. Unfortunately that really means "each request" so also requests69triggered by 3rd party sites. If you don't keep that in mind some people70might be able to trick your application's users with social engineering to71do stupid things without them knowing.65If your authentication information is stored in cookies, you have implicit66state management. The state of "being logged in" is controlled by a67cookie, and that cookie is sent with each request to a page.68Unfortunately that includes requests triggered by 3rd party sites. If you69don't keep that in mind, some people might be able to trick your70application's users with social engineering to do stupid things without71them knowing.7273Say you have a specific URL that, when you sent `POST` requests to will74delete a user's profile (say `http://example.com/user/delete`). If an75attacker now creates a page that sends a post request to that page with76some JavaScript he just has to trick some users to that page and their77profiles will end up being deleted.76some JavaScript he just has to trick some users to load that page and77their profiles will end up being deleted.7879Imagine you would run Facebook with millions of concurrent users and80someone would send out links to images of little kittens. When a user81would go to that page their profiles would get deleted while they are79Imagine you were to run Facebook with millions of concurrent users and80someone would send out links to images of little kittens. When users81would go to that page, their profiles would get deleted while they are82looking at images of fluffy cats.8384So how can you prevent yourself from that? Basically for each request85that modifies content on the server you would have to either use a86one-time token and store that in the cookie **and** also transmit it with87the form data. After recieving the data on the server again you would88then have to compare the two tokens and ensure they are equal.84How can you prevent that? Basically for each request that modifies85content on the server you would have to either use a one-time token and86store that in the cookie **and** also transmit it with the form data.87After receiving the data on the server again, you would then have to88compare the two tokens and ensure they are equal.8990Why does not Flask do that for you? The ideal place for this to happen is91the form validation framework which does not exist in Flask.90Why does Flask not do that for you? The ideal place for this to happen is91the form validation framework, which does not exist in Flask.9293.. _json-security:9495JSON Security
14 unmodified lines
110generate JSON.111112So what is the issue and how to avoid it? The problem are arrays at113toplevel in JSON. Imagine you send the following data out in a JSON114request. Say that's exporting the names and email adresses of all your115friends for a part of the userinterface that is written in JavaScript.114request. Say that's exporting the names and email addresses of all your115friends for a part of the user interface that is written in JavaScript.116Not very uncommon:117118.. sourcecode:: javascript119
49 unmodified lines
modified:docs/styleguide.rst+3 added-3 removed
70 unmodified lines
71--------------------------7273General whitespace rules:74 - No whitespace for unary operators that are not words75 (eg: ``-``, ``~`` etc.) as well on the inner side of parentheses.75 (e.g.: ``-``, ``~`` etc.) as well on the inner side of parentheses.76 - Whitespace is placed between binary operators.7778 Good::79
70 unmodified lines
150151Docstring conventions:152 All docstrings are formatted with reStructuredText as understood by153 Sphinx. Depending on the number of lines in the docstring, they are154 layed out differently. If it's just one line, the closing triple154 laid out differently. If it's just one line, the closing triple155 quote is on the same line as the opening, otherwise the text is on156 the same line as the opening quote and the triple quote that closes157 the string on its own line::158
2 unmodified lines
161162163 def bar():164 """This is a longer docstring with so much information in there165 that it spans three lines. In this case the closing tripple quote165 that it spans three lines. In this case the closing triple quote166 is on its own line.167 """168169Module header:
31 unmodified lines
modified:docs/templating.rst+1 added-1 removed
142 unmodified lines
143 <p>autoescaping is disabled here144 <p>{{ will_not_be_escaped }}145 {% endautoescape %}146147Whenever you do this, please be very cautious about the varibles you are147Whenever you do this, please be very cautious about the variables you are148using in this block.149150Registering Filters151-------------------
37 unmodified lines
modified:docs/upgrading.rst+1 added-1 removed
50 unmodified lines
51order of after-request handlers. Previously they were called in the order52of the registration, now they are called in reverse order. This change53was made so that Flask behaves more like people expected it to work and54how other systems handle request pre- and postprocessing. If you55dependend on the order of execution of post-request functions, be sure to55depend on the order of execution of post-request functions, be sure to56change the order.5758Another change that breaks backwards compatibility is that context59processors will no longer override values passed directly to the template
78 unmodified lines