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>
This commit is contained in:
parent
2222278dcd
commit
ad7420281c
@ -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):
|
||||
"""
|
||||
|
Loading…
x
Reference in New Issue
Block a user