Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Require login for certain blueprints in Flask?

What's the common approach of adding access control to a blueprint in Flask?

For example I have a blueprint called admin with url_prefix='/admin'

How do I force all views under /admin must be authenticated superuser first?

like image 752
est Avatar asked Dec 14 '11 09:12

est


People also ask

How do I register my blueprints in Flask?

To use any Flask Blueprint, you have to import it and then register it in the application using register_blueprint() . When a Flask Blueprint is registered, the application is extended with its contents. While the application is running, go to http://localhost:5000 using your web browser.

Is Flask blueprint necessary?

You don't have to use blueprints. Just import current_app as app in your routes.py (or views.py, whatever) and you are free to go.

What is the point of Flask blueprint?

A Flask blueprint helps you to create reusable instances of your application. It does so by organizing your project in modules. Those modules are then registered the main application. They help in creating an application factory.


1 Answers

Found it http://flask.pocoo.org/snippets/59/

from flask import Blueprint
from flask import redirect, request
from google.appengine.api import users

bp = Blueprint('admin', __name__)

@bp.before_request
def restrict_bp_to_admins():
    if not users.is_current_user_admin():
        return redirect(users.create_login_url(request.url))
like image 113
est Avatar answered Dec 08 '22 13:12

est