Flask-Admin can_create = True only for a given user

白昼怎懂夜的黑 提交于 2019-12-10 18:52:53

问题


I am using Flask-Admin in conjunction with Flask-Login and mongoengine.

I wish to customize views depending on users. Hereafter, an example with can_create, that allows model creation.


class MyModelView(ModelView):
    column_exclude_list = ['password']
    def is_accessible(self):
        if (login.current_user.login != 'admin'):
            can_create=False
        return login.current_user.is_authenticated()

Such a piece of code has no effect: all users can still create, no difference between admin and non-admin users.

Thanks a lot for any hint on how it's possible to allow model creation only to a given user.


回答1:


Look like that you just created local variable can_create, so you can try self.can_create = False. But flask-admin create one instance of View and this can be problems with concurrency. However better separate logic for checking accessibility and changing view state. So probably better use next code:

class MyModelView(ModelView):
    column_exclude_list = ['password']

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

    @property
    def can_create(self):
        return login.current_user.login == 'admin'


来源:https://stackoverflow.com/questions/17137601/flask-admin-can-create-true-only-for-a-given-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!