Skip to content

Loading…

Added decorators #296

Closed
wants to merge 4 commits into from

2 participants

@Poincare

I have added decorators for before_request and after_request, please ignore the README changes, they were errors I had trouble reversing. If it doesn't apply cleanly, please tell me so I can fix it.

Also, keep up the excellent work, I love flask.

@mitsuhiko
Owner

before_request can already be used as a decorator:

@app.before_request
def my_function():
    ...
@mitsuhiko
Owner

Also, our whitespace rules are consistent: two spaces after fullstops in preformatted text.

@mitsuhiko mitsuhiko closed this
@Poincare

Ah, I see thanks for telling me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Showing with 35 additions and 6 deletions.
  1. +6 −6 README
  2. +29 −0 flask/app.py
View
12 README
@@ -6,30 +6,30 @@
~ What is Flask?
Flask is a microframework for Python based on Werkzeug
- and Jinja2. It's intended for small scale applications
+ and Jinja2. It's intended for small scale applications
and was developed with best intentions in mind.
~ Is it ready?
A preview release is out now, and I'm hoping for some
input about what you want from a microframework and
- how it should look like. Consider the API to slightly
+ how it should look like. Consider the API to slightly
improve over time.
~ What do I need?
- Jinja 2.4 and Werkzeug 0.6.1. `easy_install` will
+ Jinja 2.4 and Werkzeug 0.6.1. `easy_install` will
install them for you if you do `easy_install Flask==dev`.
- I encourage you to use a virtualenv. Check the docs for
+ I encourage you to use a virtualenv. Check the docs for
complete installation and usage instructions.
~ Where are the docs?
Go to http://flask.pocoo.org/ for a prebuilt version of
- the current documentation. Otherwise build them yourself
+ the current documentation. Otherwise build them yourself
from the sphinx sources in the docs folder.
~ Where can I get help?
Either use the #pocoo IRC channel on irc.freenode.net or
- ask on the mailinglist: http://flask.pocoo.org/mailinglist/
+ ask on the mailinglist: http://flask.pocoo.org/mailinglist/
View
29 flask/app.py
@@ -11,16 +11,19 @@
from __future__ import with_statement
+#Python imports
import sys
from threading import Lock
from datetime import timedelta
from itertools import chain
+#Werkzeug imports
from werkzeug.datastructures import ImmutableDict
from werkzeug.routing import Map, Rule
from werkzeug.exceptions import HTTPException, InternalServerError, \
MethodNotAllowed, BadRequest
+#Local imports
from .helpers import _PackageBoundObject, url_for, get_flashed_messages, \
locked_cached_property, _tojson_filter, _endpoint_from_view_func
from .wrappers import Request, Response
@@ -906,11 +909,37 @@ def decorator(f):
return f
return decorator
+ def before_request_call(self):
+ """A decorator that is used to register a function to run before each request.
+ Example::
+
+ @app.before_request_call()
+ def my_func():
+ pass
+ """
+ def decorator(f):
+ self.before_request(f)
+ return f
+ return decorator
+
def before_request(self, f):
"""Registers a function to run before each request."""
self.before_request_funcs.setdefault(None, []).append(f)
return f
+ def after_request_call(self):
+ """A decorator that is used to register a function to run after each request.
+ Example::
+
+ @app.after_request()
+ def my_func():
+ pass
+ """
+ def decorator(f):
+ self.after_request(f)
+ return f
+ return decorator
+
def after_request(self, f):
"""Register a function to be run after each request. Your function
must take one parameter, a :attr:`response_class` object and return
Something went wrong with that request. Please try again.