我已经简要浏览了flask-appbuilder的文档。在大多数示例中,ModelView将列出所有资源。如果存在相关视图,FAB允许admin(用户)根据其关系定义检查相关项目的详细信息。
然而,我不知道如何显示属于当前用户的resoruces。在其他框架中,通过自定义SQL查询(where语句)很容易实现。但我没有找到来自FAB示例的sql查询的演示项目,除了一些数据库测试日期插入代码。
例如,让我们对物联网相关项目进行成像。所有者拥有许多设备,许多用户可以使用一个设备。因此,
models.py
class Account(Model):
id = Column(Integer, primary_key = True)
username = Column(String(128), unique = True)
password = Column(String(128))
email = Column(String(128))
cellphone = Column(String(32))
def __repr__(self):
return self.username
class Device(Model):
id = Column(Integer, primary_key = True)
snr = Column(String(256), unique = True)
name = Column(String(128))
addr = Column(String(256))
latitude = Column(Float)
longitude = Column(Float)
status = Column(Enum('init','normal','transfer','suspend'), default = 'init')
owner_id = Column(Integer, ForeignKey('account.id'))
owner = relationship("Account")
def __repr__(self):
return self.name
class Access(Model):
id = Column(Integer, primary_key = True)
name = Column(String(128))
submit = Column(Date)
approve = Column(Date)
status = Column(Enum('init','approve','reject','expire'), default='init')
dev_id = Column(Integer, ForeignKey('device.id'))
device = relationship("Device")
user_id = Column(Integer, ForeignKey('account.id'))
user = relationship("Account")
def __repr__(self):
return self.name
views.py
class AccessView(ModelView):
datamodel = SQLAInterface(Access)
label_columns = {}
list_columns = ['device','owner','submit','approve']
class DeviceView(ModelView):
datamodel = SQLAInterface(Device)
related_views = [ApplicationView, EventView]
label_columns = {'snr':'SNR',
'owner_id':'Owner'}
list_columns = ['name','snr','addr','owner']
edit_columns = ['name','snr','owner','addr','latitude','longitude','status']
add_columns = edit_columns
show_fieldsets = [
('Summary',
{'fields':['name','snr','owner']}
),
('Device Info',
{'fields':['addr','latitude','longitude','status'],'expanded':True}
),
]
@expose('/mine')
def mine(self):
msg = "Mine Assets"
return self.render_template('testpage.html', msg = msg)
@expose('/granted')
def granted(self):
msg = "Granted Assets"
return self.render_template('testpage.html', msg = msg)
db.create_all()
appbuilder.add_view(DeviceView,
"My Devices",
icon = "fa-heart",
category = "Devices",
category_icon = "fa-microchip")
appbuilder.add_view(AccountView,
"Account",
icon = "fa-group",
category = "Devices")
appbuilder.add_view(AccessView,
"Access",
icon = "fa-handshake-o",
category = "Devices")
如果我们为三个视图定义了以下网址:
但是,我不知道如何编写我的方法/授予方法。通过使用db.session.query()?
db.session.query(设备,所有者=所有者)或 db.session.query(访问权限,用户=用户)
或者我还没有读过的任何文件?
答案 0 :(得分:0)
我找到了一个解决方案,至少部分答案。除了DeviceView for admin之外,我还将另一个DeviceXView(作为临时名称)定义为当前用户的设备列表,并使用base_filter缩小模型中的数据集范围。
class DeviceXView(ModelView):
datamodel = SQLAInterface(Device)
base_filters = [['owner', FilterEqualFunction, get_user],]
related_views = [ApplicationView, EventView]
label_columns = {'snr':'SNR',
'api_dev_id':'Device ID',
'api_dev_key':'Device Key',
'api_id':'API ID',
'api_key':'API Key',
'owner_id':'Owner'}
list_columns = ['name','snr','addr','owner']
edit_columns = ['name','snr','owner','addr','latitude','longitude','status','api_id','api_key','api_dev_id','api_dev_key']
add_columns = edit_columns
show_fieldsets = [
('Summary',
{'fields':['name','snr','owner']}
),
('Device Info',
{'fields':['addr','latitude','longitude','status','api_id','api_key','api_dev_id','api_dev_key'],'expanded':True}
),
]
这适用于当前用户拥有的所有偏离。为了显示用户可以访问的所有设备,我们可以将AccessView和DeviceView定义为相关的View。