librelist archives

« back to archive

Problems with Flask-Admin

Problems with Flask-Admin

From:
Quim Moncanut
Date:
2015-03-31 @ 11:15
Hi listers!

I have an app which uses flask-admin and flask-login. The Flask-Admin
part is a ModelView subclass implementing the is_accessible method. When
there is no user logged in (an anonymous user) or if the logged in user
is not an admin user, is_accessible returns False.

And if is_accesible returns False, the 'Clients' menu option
disappears from the Flask-Admin bar! Any idea about it?

Here is my layout (skipping irrelevant parts):

# wsgi/__init__.py

from Admin import my_admin
app = Flask(__name__, template_folder='../backend/Base/template', 
    static_folder='../backend/Base/static')
my_admin.init_app(app)


# Admin/__init__.py

from flask_admin import Admin
pymdb = MongoClient(
      config.MONGODB_REPLSET_URL, 
      replicaSet=config.MONGODB_REPLICASET
)
my_admin = Admin(name=u'Admin', static_url_path='/static')
my_admin.add_view(
    Admin_User(coll=pymdb['UsersDB']['Users'], 
    name='Customers')
)


# Admin/views/Admin_User.py

from flask_admin.contrib.pymongo import ModelView
from Admin.forms.Admin_User_Form import Admin_User_Form
import flask_login as login


class Admin_User(ModelView, object):
    can_create = False
    can_delete = False
    column_list = ('email', 'enabled')
    form = Admin_User_Form

    def is_accessible(self):
        return login.current_user.is_authenticated() and 
login.current_user.is_admin()

Thanks in advance.

Quim

Re: Problems with Flask-Admin

From:
Quim Moncanut
Date:
2015-04-01 @ 09:50
Hi there,

my mistake, folks. As stated in the flask-admin docs: "Please note that
item should be both visible and accessible to be displayed in menu."

So, hiding a menu item when it is not accessible is the normal
behaviour. (Hitting my head against the wall: RTFM!)

Thanks.

Quim