1. 程式人生 > >OpenStack(kilo)介面dashboard的二次開發(三)-增加Dashboard

OpenStack(kilo)介面dashboard的二次開發(三)-增加Dashboard

前面的博文已經完成了panel與panelgroup的自定義增加,這篇博文將對Dashboard(一級選單)的增加進行一個簡單總結。

增加Dashboard(一級選單)

首先進入/usr/share/openstack-dashbaord/openstack_dashboard/dashboards目錄,會看到如下的目錄結構:

|--__init__.py
|--admin
|--project
|--identity
|--router
|--settings

在博主以前的文章OpenStack(kilo)介面dashboard的二次開發(一)中已經說明,以上幾個資料夾代表了openstack介面上的幾個一級選單(Dashboard)。對於Dashboard下的PanelGroup與Panel的自定義增加以及相關檔案結構已經分別在

OpenStack(kilo)介面dashboard的二次開發(一)OpenStack(kilo)介面dashboard的二次開發(二)中簡單總結了,這裡就不總結檔案結構了。現在增加一個Dashboard,首先在dashboards下建立一個資料夾如下:

|--__init__.py
|--admin
|--project
|--identity
|--router
|--settings
|--myproject   #自定義增加的

myproject的檔案結構如下:

myproject
|--dashboard.py
|--__init__.py
|--mypanel           #這是在前面博文中增加的Panel,現在放到這來

接下來分別看看每個檔案的內容:
dashboard.py

#-*- coding:utf-8 -*-

from django.utils.translation import ugettext_lazy as _

import horizon

'''上一篇博文中增加的PanelGroup'''
class MyPanels(horizon.PanelGroup):
    slug = "mypanelgroup"
    name = "Mypanelgroup"
    panels = ('mypanel',)

class Myproject(horizon.Dashboard)
:
name = "扶艾的專案" slug = "myproject" panels = (MyPanels,) default_panel = 'mypanel' permissions = ('openstack.role.admin',) horizon.register(Myproject)

然後看看mypanel資料夾的結構:

mypanel
|--__init__.py
|--panel.py
|--tables.py
|--templates
  |--mypanel
    |--index.html
|--urls.py
|--views.py

這些檔案的作用已將在前面博文總結,這裡就不再贅述了。下面分別看看它們裡面的內容:
panel.py

import horizon

from openstack_dashboard.dashboards.myproject import dashboard


class Mypanel(horizon.Panel):
    name = "mypanel"
    slug = 'mypanel'
    permissions = ('openstack.roles.admin', 'openstack.services.compute')


dashboard.Myproject.register(Mypanel)

tables.py

from horizon import tables

class MypanelTable(tables.DataTable):
    column1 = tables.Column("column1", verbose_name="column1")
    class Meta(object):
        name = "mypaneltable"
        verbose_name = "mypaneltable"

index.html

{% extends 'base.html' %}{% load i18n %}{% block title %}{% trans "mypanel" %}{% endblock %}{% block main %}{{ table.render }}{% endblock %}

urls.py

from django.conf.urls import patterns
from django.conf.urls import url

from openstack_dashboard.dashboards.myproject.mypanel import views


urlpatterns = patterns(
    'openstack_dashboard.dashboards.myproject.mypanel.views',
    url(r'^$', views.MypanelIndexView.as_view(), name='index'),
)

views.py

from horizon import tables

from openstack_dashboard.dashboards.myproject.mypanel \
    import tables as project_tables

class MypanelIndexView(tables.DataTableView):
    table_class = project_tables.MypanelTable
    template_name = 'myproject/mypanel/index.html'
    page_title = "mypanel"

    def get_data(self):
        data = []
        return data

上面即是要增加Dashboard所需要的檔案,並且把之前博文中自定義的PanelGroup與Panel加了進來。現在有了這些檔案重啟httpd服務,介面上仍然不會顯示自定義的Dashboard。因為開關沒開啟,這時候還需要增加一個檔案來啟用它。進入/usr/share/openstack-dashboard/openstack_dashboard/enabled/資料夾可以看到很多以下劃線加數字開頭的檔案,這些都是啟用那些Dashboard的檔案,模仿一個增加:
_35_myproject.py

DASHBOARD = 'myproject'

ADD_INSTALLED_APPS = [
    'openstack_dashboard.dashboards.myproject',
]

該資料夾的名稱前面的數字大小代表的是Dashboard在選單欄的順序,數字越小越靠前,這裡是35,目前是最大的,所以Dashboard會顯示在最後。重啟httpd服務,檢視效果
這裡寫圖片描述
更多精彩文章,請搜尋微信公眾號“扶艾”。我們定期分享OpenStack相關技術文章,在這裡,只有純乾貨
這裡寫圖片描述