diff options
author | Sebastian Spaeth <Sebastian@SSpaeth.de> | 2012-12-05 13:37:19 +0100 |
---|---|---|
committer | Sebastian Spaeth <Sebastian@SSpaeth.de> | 2012-12-05 13:37:19 +0100 |
commit | ad7420281cdaf5846d8754a29e5006bde82d87f9 (patch) | |
tree | 49342c9f20af9cf024901f2d26c3b608687f428a | |
parent | 2222278dcdf70a1dbcba0e5955344259d0697752 (diff) | |
download | mediagoblin-ad7420281cdaf5846d8754a29e5006bde82d87f9.tar.lz mediagoblin-ad7420281cdaf5846d8754a29e5006bde82d87f9.tar.xz mediagoblin-ad7420281cdaf5846d8754a29e5006bde82d87f9.zip |
Create a active_user_from_url decorator
This can be used for URL patterns containing a <user> element. It will look
up the corresponding user among all active users and return a 404 NOT FOUND
page if there is no such active user. It then passes the User() instance as
url_user keyword argument to the decorated view function.
Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
-rw-r--r-- | mediagoblin/decorators.py | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/mediagoblin/decorators.py b/mediagoblin/decorators.py index 9be9d4cc..90b36771 100644 --- a/mediagoblin/decorators.py +++ b/mediagoblin/decorators.py @@ -22,6 +22,7 @@ from urllib import urlencode from webob import exc from mediagoblin.db.util import ObjectId, InvalidId +from mediagoblin.db.sql.models import User from mediagoblin.tools.response import redirect, render_404 @@ -52,6 +53,20 @@ def require_active_login(controller): return new_controller_func +def active_user_from_url(controller): + """Retrieve User() from <user> URL pattern and pass in as url_user=... + + Returns a 404 if no such active user has been found""" + @wraps(controller) + def wrapper(request, *args, **kwargs): + user = User.query.filter_by(username=request.matchdict['user']).first() + if user is None: + return render_404(request) + + return controller(request, *args, url_user=user, **kwargs) + + return wrapper + def user_may_delete_media(controller): """ |