Flask-Admin不同的表单和column_list用于不同的角色 [英] Flask-Admin different forms and column_list for different roles

查看:188
本文介绍了Flask-Admin不同的表单和column_list用于不同的角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关注此问题 Flask-管理员基于角色的访问权限-根据角色修改访问权限我不理解如何实现基于角色的视图,尤其是有关form和column_lists的视图.

Following up on this question Flask-Admin Role Based Access - Modify access based on role I don't understand how to implement role-based views, especially regarding the form and column_lists.

说,如果用户是普通用户或超级用户,我希望MyModelView显示不同的列.

Say I want MyModelView to show different columns if the user is a regular user or a superuser.

MyModelView中覆盖is_accessible完全无效

from flask_security import Security, SQLAlchemyUserDatastore, current_user

class MyModelView(SafeModelView):

    # ...

    def is_accessible(self):
        if current_user.has_role('superuser'):
            self.column_list = superuser_colum_list
            self.form_columns = superuser_form_columns
        else:
            self.column_list = user_colum_list
            self.form_columns = user_form_columns
        return super(MyModelView, self).is_accessible()

    # Has same effect as 

    def is_accessible(self):
        return super(MyModelView, self).is_accessible()

并定义条件类属性不起作用,因为未定义current_user(根据NoneType -is-authenticated> current_user.is_authenticated()上的AttributeError(错误)).与在ModelView的__init__中所做的相同,current_user仍未定义

and defining conditional class attributes does not work either as current_user is not defined (NoneType error as per AttributeError on current_user.is_authenticated()). Doing the same in the ModelView's __init__ being equivalent, current_user is still not defined

class MyModelView(SafeModelView):

    #[stuff]

    if current_user.has_role('superuser'):
            column_list = superuser_colum_list
            form_columns = superuser_form_columns
    else:
        column_list = user_colum_list
        form_columns = user_form_columns

    #[other stuff]

FYI,SafeModelView可以是在前面提到的问题中从dgBaseView继承的任何类.

FYI, SafeModelView can be any class inheriting from dgBaseView in the previously mentioned question.

推荐答案

我通常将诸如column_list之类的视图类属性定义为属性.它允许您向其中添加一些动态逻辑:

I usually define view class attributes such as column_list as properties. It allows you to add some dynamic logic to them:

from flask import has_app_context
from flask_security import current_user

class MyModelView(SafeModelView):
    @property
    def column_list(self):
        if has_app_context() and current_user.has_role('superuser'):
            return superuser_column_list
        return user_column_list

    @property
    def _list_columns(self):
        return self.get_list_columns()

    @_list_columns.setter
    def _list_columns(self, value):
        pass

使用这种方法的问题(以及为什么在is_accessible函数中重新分配column_list值无效)的原因是,许多视图属性在应用程序启动时被缓存并存储在私有属性中.例如,column_list缓存在_list_columns属性中,因此您也需要重新定义它.您可以在中查看此缓存的工作方式flask_admin.model.base.BaseModelView._refresh_cache 方法.

The problem with using this approach (and why your reassigning of column_list values in is_accessible function took no effect) is that many view attributes are cached on application launch and stored in private attributes. column_list for example is cached in _list_columns attribute so you need to redefine it as well. You can look how this caching works in flask_admin.model.base.BaseModelView._refresh_cache method.

烧瓶has_app_context方法,因为当current_user变量还没有有意义的值时,第一次column_list读取是在应用程序启动时发生的.

Flask has_app_context method is needed here because first column_list read is happened on application launch when your current_user variable has no meaningful value yet.

同样可以使用form_columns属性来完成.这些属性将如下所示:

The same can be done with form_columns attribute. The properties will look like this:

@property
def form_columns(self):
    if has_app_context() and current_user.has_role('superuser'):
        return superuser_form_columns
    return user_form_columns

@property
def _create_form_class(self):
    return self.get_create_form()

@_create_form_class.setter
def _create_form_class(self, value)
    pass

@property
def _edit_form_class(self):
    return self.get_edit_form()

@_edit_form_class.setter
def _edit_form_class(self, value):
    pass

这篇关于Flask-Admin不同的表单和column_list用于不同的角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆