Horizon: dynamic table count

asked 2015-07-30 08:54:41 -0500

urdak gravatar image

I would like to create a view with table count, which is based on an API call. Is there any way to make a view with a dynamic table count? I know there's a MultiTableView, but it only allows a static amount of tables.

class MultiTableView(MultiTableMixin, views.HorizonTemplateView):
"""A class-based generic view to handle the display and processing of
multiple :class:`~horizon.tables.DataTable` classes in a single view.

Three steps are required to use this view: set the ``table_classes``
attribute with a tuple of the desired
:class:`~horizon.tables.DataTable` classes;
define a ``get_{{ table_name }}_data`` method for each table class
which returns a set of data for that table; and specify a template for
the ``template_name`` attribute.
"""

I tried making a workaround for this by setting table_classes and using setattr() to create get_{{ table_name }}_data functions in the constructor of MultiTableView, but I am unable to get the request from the constructor to get the data from the API call:

'IndexView' object has no attribute 'request'

Here's an example:

class IndexView(tables.MultiTableView):
    table_classes = ()
    template_name = 'project/new_stacks/index.html'

    def __init__(self, *args, **kwargs):
        super(IndexView, self).__init__(*args, **kwargs)
        stacks, more, prev = api.heat.stacks_list(self.request) # can't get self.request in init !!!
        for i, instance_list in enumerate(_get_instances(self.request, stacks)): # and here
            setattr(self, 'get_' + i + '_data', self._get_data(instance_list))
            self.table_classes += (project_tables.InstanceTable,)

    def _get_data(self, arg):
        def _get_data():
            return arg
        return _get_data

Is there a better solution? Or is there a way to get the request in the constructor?

edit retag flag offensive close merge delete