Skip to content

Instantly share code, notes, and snippets.

@jawsthegame
Created November 21, 2012 19:07
Show Gist options
  • Save jawsthegame/4126952 to your computer and use it in GitHub Desktop.
Save jawsthegame/4126952 to your computer and use it in GitHub Desktop.
def admin_required(func):
"""
Wraps a function to ensure that it's only viewable by logged in admins. If a
user is not logged in, it redirects to a login page with an acceptable error
message. If a user is logged in, but not a admin, it takes them to the root
page with an error.
"""
@wraps(func)
@login_required
def view_wrapper(*args, **kwargs):
if not g.user.is_admin:
flash("You must have a admin account to view this page.", 'error')
remove_user(g.user)
return redirect('/admin/')
return func(*args, **kwargs)
return view_wrapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment