Flask 管理员角色将 roles_accapted 添加到 add_view

Flask admin roles adding roles_accapted to add_view

我已经设法让角色使用此代码,但是!

class Roled(object):
    def is_accessible(self):
        roles_accepted = getattr(self, 'roles_accepted', None)
        if flask_login.current_user.is_authenticated:
            user = CustomUser.objects.get(email=flask_login.current_user.id)
            for role in user.roles:
                for permission in role.permissions:
                    if permission == "admin":
                        return flask_login.current_user.is_authenticated

    def inaccessible_callback(self, name, **kwargs):
        # redirect to login page if user doesn't have access
        return redirect(url_for('login', next=request.url))


class AdminView(Roled, ModelView):
    def __init__(self, *args, **kwargs):
        self.roles_accepted = kwargs.pop('roles_accepted', list())
        super(AdminView, self).__init__(*args, **kwargs)


class TranslationView(Roled, ModelView):
    column_filters = ['eng']
    column_searchable_list = ('eng','geo', 'name')

现在,在添加 Flask 管理视图时,我在向每个 add_view 添加 roles_accapted 时遇到问题,就像在

中一样

您的 TranslationView 应该继承自 AdminView class。即

class TranslationView(AdminView):
    column_filters = ['eng']
    column_searchable_list = ('eng','geo', 'name')