源碼版本:H版
一、寫在前面
二、horizon前端請求
主要用瀏覽器 chrome 的開發者工具分析頁面發出的具體請求,此處是分析前端的一個 button 按鈕執行情況。請求鏈接地址:
其他相關元素:
三、URL后端綁定及APP的加載
根據Django的框架結構,使用URLconf文件(https://docs.djangoproject.com/en/1.4/topics/http/urls/)進行鏈接請求和后端處理(view)的綁定,使用view進行后端處理,使用template進行頁面渲染。
1、目錄結構:
horizon-------------組件,提供部分功能
|---__init__.py-----------控制着horizon包導入行為
|---base.py-----------horizon提供的Site類、Dashboard類、Panel類,負責整個基本架構
|---site_urls.py
openstack_dashboard----------------網站project根目錄
|---settings.py------------網站基本設置【1】
|---urls.py----------------網站基本URL設置
|---views.py---------------網站基本view
|---templates--------------網站基本template
|---dashboards
|---admin ---------------dashboard【2】
|---instances --------------panel【3】
|---panel.py----------------------負責往dashboard中注冊panel
|---urls.py
|---views.py
...
|---dashboard.py --------------負責往Horizon中注冊dashboard
|---models.py
...
Horizon項目架構: 主要分為兩個部分: horizon和openstack_dashboard。 horizon提供庫和功能組件, openstack_dashboard 是一個使用了horizon的Django項目。
2、URL綁定分析:
ROOT_URLCONF = 'openstack_dashboard.urls'
openstack_dashboard/urls.py
part2:
綜上所述,最終的include()導入的是Site類的_lazy_urls。
3、Site、Dashboard、Panel三者的加載
這里先說明Site、Dashboard和Panel三者的加載過程以便后面的進一步分析。Horizon采用了注冊機制,即以一個對象為根對象,將其他組件注冊到根對象的屬性中,這種機制使得軟件的可擴展性更強,並且條理清晰,貌似像一種設計模式來着。Horizon是一個Site類對象,往其中注冊Dashboard類時會構建一個Dashboard對象注冊到其屬性_registry字典中;Panel類往Dashboard類中注冊,注冊時會構建Panel對象注冊到Dashboard對象的_registry字典里。具體情況如下:
3.1 一個dashboard注冊過程import horizon class SystemPanels(horizon.PanelGroup): slug = "admin" name = _("System Panel") panels = ('overview', 'metering', 'hypervisors', 'instances', 'volumes', 'flavors', 'images', 'networks', 'routers', 'defaults', 'info') class IdentityPanels(horizon.PanelGroup): slug = "identity" name = _("Identity Panel") panels = ('domains', 'projects', 'users', 'groups', 'roles') class Admin(horizon.Dashboard): name = _("Admin")#用於display slug = "admin"#用於內部引用 """panels可以用來發現與該Dashboard相關的所有Panel,以便在以后往Dashboard中注冊這些Panel""" panels = (SystemPanels, IdentityPanels) default_panel = 'overview' permissions = ('openstack.roles.admin',) horizon.register(Admin)
horizon/base.py
Site類: def register(self, dashboard): """Registers a :class:`~horizon.Dashboard` with Horizon.""" return self._register(dashboard)
Registry類: def _register(self, cls): if not inspect.isclass(cls): raise ValueError('Only classes may be registered.') elif not issubclass(cls, self._registerable_class): raise ValueError('Only %s classes or subclasses may be registered.' % self._registerable_class.__name__) if cls not in self._registry: cls._registered_with = self self._registry[cls] = cls() return self._registry[cls]
在horizon/base.py中的Horizon對象的_registy映射中添加了Dashboard類à類實例的映射!
3.2 一個panel的注冊過程
以admin這個Dashboard中的instancs為例:import horizon """將admin這個Dashboard的dashboard.py導入""" from openstack_dashboard.dashboards.admin import dashboard class Aggregates(horizon.Panel): name = _("Host Aggregates") slug = 'aggregates' permissions = ('openstack.services.compute',) """在Dashboard為Admin中進行注冊""" dashboard.Admin.register(Aggregates)
horizon/base.py
Dashboard類: @classmethod def register(cls, panel): """檢查cls是否已經注冊到Horizon對象中,並且在cls中注冊panel""" panel_class = Horizon.register_panel(cls, panel) panel_mod = import_module(panel.__module__) panel_dir = os.path.dirname(panel_mod.__file__) template_dir = os.path.join(panel_dir, "templates") if os.path.exists(template_dir): key = os.path.join(cls.slug, panel.slug) loaders.panel_template_dirs[key] = template_dir return panel_class
Site類: def _urls(self): """實際調用HorizonComponent._get_default_urlpatterns,獲取site_urls.py中的urlpatterns""" urlpatterns = self._get_default_urlpatterns()【1】 """實際調用Site._autodiscover ,導入openstack_dashboard/settings.py中的HORIZON_CONFIG, INSTALLED_APPS配置的模塊中的dashboard.py和panel.py,此處會進行Dashboard的注冊""" """可參考文檔:http://docs.openstack.org/developer/horizon/topics/settings.html""" self._autodiscover()【2】 """發現每個Dashboard中的Panel,導入每個Panel的panel.py,從而注冊每個Panel""" for dash in self._registry.values(): dash._autodiscover()【3】 ... # Compile the dynamic urlconf. """dash為Dashboard類""" for dash in self._registry.values(): urlpatterns += patterns('', url(r'^%s/' % dash.slug, include(dash._decorated_urls))) # Return the three arguments to django.conf.urls.include return urlpatterns, self.namespace, self.slug
Dashboard類: @property def _decorated_urls(self): urlpatterns = self._get_default_urlpatterns()【1】 default_panel = None # Add in each panel's views except for the default view. """panel為Panel類""" for panel in self._registry.values(): if panel.slug == self.default_panel: default_panel = panel continue url_slug = panel.slug.replace('.', '/') urlpatterns += patterns('', url(r'^%s/' % url_slug, include(panel._decorated_urls))) ... # Return the three arguments to django.conf.urls.include return urlpatterns, self.slug, self.slug
Panel類: @property def _decorated_urls(self): """即查找panel的urls.py文件""" urlpatterns = self._get_default_urlpatterns()【1】
# Apply access controls to all views in the patterns permissions = getattr(self, 'permissions', []) _decorate_urlconf(urlpatterns, require_perms, permissions) _decorate_urlconf(urlpatterns, _current_component, panel=self)
# Return the three arguments to django.conf.urls.include return urlpatterns, self.slug, self.slug
def _autodiscover(self): """Discovers modules to register from ``settings.INSTALLED_APPS``. This makes sure that the appropriate modules get imported to register themselves with Horizon. """ if not getattr(self, '_registerable_class', None): raise ImproperlyConfigured('You must set a ' '"_registerable_class" property ' 'in order to use autodiscovery.') # Discover both dashboards and panels, in that order for mod_name in ('dashboard', 'panel'): """settings.INSTALLED_APPS感覺應該和openstack_dashboard/settings.py中的 ORIZON_CONFIG, INSTALLED_APPS有關""" for app in settings.INSTALLED_APPS: mod = import_module(app) try: before_import_registry = copy.copy(self._registry) import_module('%s.%s' % (app, mod_name)) except Exception: self._registry = before_import_registry if module_has_submodule(mod, mod_name): raise
4、結論如下:
對於請求: