在Devstack基础上开发dashboard(三)——自定义菜单配置分组和显示

devstack版本: queens
运行环境: CentOS Linux release 7.5.1804

horizon中自定义菜单配置分组和显示

首先看一下horizon的基本目录结构,如下:
项目结构
这里面openstack_dashboard-->dashboards-->sample 为自定义的dashboard的目录,里面存放有各个页面的文件等。

1、本地化配置

下面这张图:
本地化文件
这里面存放的是各个本地化的字段映射关系文件,展示出的目录对应简体中文的文件。分别为python和js中的映射关系。里面以如下方式定义扩展的字段映射关系:

# 自定义扩展本地化配置

msgid "Samples"
msgstr "示例模块"

msgid "Panel1"
msgstr "模板1"

msgid "Panel2"
msgstr "模板2"

msgid "Panel3"
msgstr "模板3"

注意:修改过本地化文件后,需要进行python manage.py compilemessages进行编译后才能生效。

2、菜单生效和顺序配置

下图显示的是如何安排horizon中各个dashboard的菜单先后顺序,都在名为enabled的文件夹下定义。如下图:
菜单显示顺序
这个文件夹下的所有配置文件都会在Django启动的时候,进行解析并查找对应的页面文件,如果目录中有残留的文件,找不到其中对应的模板页面,那么在启动的时候,就会报错。这一点需要注意。
另外,每一个py文件的文件名初始几个数字,表示在菜单中各个页面的显示先后顺序,根据数字大小进行排序。
下图是我自定义的几个分组,可以展示出几个菜单结构。
enabled组
其中XXexample.py是入口的dashboard菜单,内容如下:

# The name of the dashboard to be added to HORIZON['dashboards']. Required.
DASHBOARD = 'sample'
# If set to True, this dashboard will be set as the default dashboard.
#DEFAULT = True
# A dictionary of exception classes to be added to HORIZON['exceptions'].
ADD_EXCEPTIONS = {}
# A list of applications to be added to INSTALLED_APPS.
ADD_INSTALLED_APPS = ['openstack_dashboard.dashboards.sample']

ADD_ANGULAR_MODULES = [
    'horizon.dashboard.project',
]

AUTO_DISCOVER_STATIC_FILES = True

ADD_JS_FILES = []

ADD_JS_SPEC_FILES = []

对应的dashboard名称sample会对应到openstack_dashboard-->dashboards-->sample目录中的dashboard.py文件中定义的类的标签。
openstack_dashboard-->dashboards-->sample-->dashboard.py内容如下:

# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

from django.utils.translation import ugettext_lazy as _

import horizon


class Example(horizon.Dashboard):
    name = _("Samples")
    slug = "sample"


horizon.register(Example)

其中slug指定的名称,即是在XXexample.py中定义的名称,通过这个字段就可以找到对应的文件了。其他的地方查找方法类似。

3、菜单分组配置

另外,这里通过enabled中的含有group的几个文件,定义了一个虚拟的分组,这个分组将不同的panel加入到对应的菜单组中。
_90010_example_panel1_group.py内容如下:

from django.utils.translation import ugettext_lazy as _

# The slug of the panel group to be added to HORIZON_CONFIG. Required.
PANEL_GROUP = 'panel1group'
# The display name of the PANEL_GROUP. Required.
PANEL_GROUP_NAME = _('Panel1')
# The slug of the dashboard the PANEL_GROUP associated with. Required.
PANEL_GROUP_DASHBOARD = 'sample'

这里定义了panel1group,属于sample这个dashboard。这个group的名字是Panel1,在本地化的配置文件里,对应的中文名称为模板1
_90040_example_panel1.py文件的内容如下:

# The slug of the panel to be added to HORIZON_CONFIG. Required.
PANEL = 'panel1'
# The slug of the dashboard the PANEL associated with. Required.
PANEL_DASHBOARD = 'sample'
# The slug of the panel group the PANEL is associated with.
PANEL_GROUP = 'panel1group'

# If set, it will update the default panel of the PANEL_DASHBOARD.
DEFAULT_PANEL = 'panel1'

# Python panel class of the PANEL to be added.
ADD_PANEL = 'openstack_dashboard.dashboards.sample.panel1.panel.Panel1'

可以看到,它是属于panel1group,并且panel的名字也定义了,对应的panel文件通过ADD_PANEL指定。
panel1
可以找到对应的panel文件里面slug的名称为panel1,所有的名称都对应上了,这样就可以一步步找到要显示的panel了。

在上述配置关系处理好以后,将文件同步到服务器上,重启httpd,即可看到生效的页面效果。如下所示:
示例页面
可以看出,自定义的页面已经能够显示出来,并且按照设置的组进行划分开。菜单对应的名称也通过本地化配置文件进行翻译显示。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值