¡@

Home 

OpenStack Study: base.py

OpenStack Index

**** CubicPower OpenStack Study ****

# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2012 Nebula, Inc.

#

# 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 import template

from django.utils.translation import ugettext_lazy as _

from horizon.browsers.breadcrumb import Breadcrumb # noqa

from horizon.tables import DataTable # noqa

from horizon.utils import html

**** CubicPower OpenStack Study ****

class ResourceBrowser(html.HTMLElement):

"""A class which

**** CubicPower OpenStack Study ****

    def __init__(self, request, tables_dict=None, attrs=None, **kwargs):

        super(ResourceBrowser, self).__init__()

        self.name = self.name or self.__class__.__name__

        self.verbose_name = self.verbose_name or self.name.title()

        self.request = request

        self.kwargs = kwargs

        self.has_breadcrumb = getattr(self, "has_breadcrumb")

        if self.has_breadcrumb:

            self.breadcrumb_template = getattr(self, "breadcrumb_template")

            self.breadcrumb_url = getattr(self, "breadcrumb_url")

            if not self.breadcrumb_url:

                raise ValueError("You must specify a breadcrumb_url "

                                 "if the has_breadcrumb is set to True.")

        self.attrs.update(attrs or {})

        self.check_table_class(self.content_table_class, "content_table_class")

        self.check_table_class(self.navigation_table_class,

                               "navigation_table_class")

        if tables_dict:

            self.set_tables(tables_dict)

**** CubicPower OpenStack Study ****

    def check_table_class(self, cls, attr_name):

        if not cls or not issubclass(cls, DataTable):

            raise ValueError("You must specify a DataTable subclass for "

                             "the %s attribute on %s."

                             % (attr_name, self.__class__.__name__))

**** CubicPower OpenStack Study ****

    def set_tables(self, tables):

        """Sets the table instances on the browser from a dictionary mapping

        table names to table instances (as constructed by MultiTableView).

        """

        self.navigation_table = tables[self.navigation_table_class._meta.name]

        self.content_table = tables[self.content_table_class._meta.name]

        navigation_item = self.kwargs.get(self.navigation_kwarg_name)

        content_path = self.kwargs.get(self.content_kwarg_name)

        # Tells the navigation table what is selected.

        self.navigation_table.current_item_id = navigation_item

        if self.has_breadcrumb:

            self.prepare_breadcrumb(tables, navigation_item, content_path)

**** CubicPower OpenStack Study ****

    def prepare_breadcrumb(self, tables, navigation_item, content_path):

        if self.has_breadcrumb and navigation_item and content_path:

            for table in tables.values():

                table.breadcrumb = Breadcrumb(self.request,

                                              self.breadcrumb_template,

                                              navigation_item,

                                              content_path,

                                              self.breadcrumb_url)

**** CubicPower OpenStack Study ****

    def render(self):

        browser_template = template.loader.get_template(self.template)

        extra_context = {self.context_var_name: self}

        context = template.RequestContext(self.request, extra_context)

        return browser_template.render(context)