# vim: tabstop=4 shiftwidth=4 softtabstop=4 # # 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. import copy import logging import operator import sys from django import forms import django.http from django import template from django.utils import datastructures from django.utils import html from django.utils import http from django.utils import termcolors from django.utils.translation import ugettext_lazy as _ # noqa from horizon import conf from horizon import exceptions from horizon import messages from horizon.tables import actions as table_actions from horizon.tables import base as horizon_tables LOG = logging.getLogger(__name__) PALETTE = termcolors.PALETTES[termcolors.DEFAULT_PALETTE] STRING_SEPARATOR = "__" class Column(horizon_tables.Column): def __init__(self, transform, verbose_name=None, sortable=True, link=None, allowed_data_types=[], hidden=False, attrs=None, status=False, status_choices=None, display_choices=None, empty_value=None, filters=None, classes=None, summation=None, auto=None, truncate=None, link_classes=None, # FIXME: Added for TableStep: form_widget=None, form_widget_attributes=None ): super(Column, self).__init__( transform, verbose_name, sortable, link, allowed_data_types, hidden, attrs, status, status_choices, display_choices, empty_value, filters, classes, summation, auto, truncate, link_classes) self.form_widget = form_widget # FIXME: TableStep self.form_widget_attributes = form_widget_attributes or {} # TableStep class Row(horizon_tables.Row): def load_cells(self, datum=None): """ Load the row's data (either provided at initialization or as an argument to this function), initiailize all the cells contained by this row, and set the appropriate row properties which require the row's data to be determined. This function is called automatically by :meth:`~horizon.tables.Row.__init__` if the ``datum`` argument is provided. However, by not providing the data during initialization this function allows for the possibility of a two-step loading pattern when you need a row instance but don't yet have the data available. """ # Compile all the cells on instantiation. table = self.table if datum: self.datum = datum else: datum = self.datum cells = [] for column in table.columns.values(): if column.auto == "multi_select": # FIXME: TableStep code modified # multi_select fields in the table must be checked after # a server action # TODO(remove this ugly code and create proper TableFormWidget) multi_select_values = [] if (getattr(table, 'request', False) and getattr(table.request, 'POST', False)): multi_select_values = table.request.POST.getlist( self.table._meta.multi_select_name) multi_select_values += getattr(table, 'active_multi_select_values', []) if unicode(table.get_object_id(datum)) in multi_select_values: multi_select_value = lambda value: True else: multi_select_value = lambda value: False widget = forms.CheckboxInput(check_test=multi_select_value) # Convert value to string to avoid accidental type conversion data = widget.render(self.table._meta.multi_select_name, unicode(table.get_object_id(datum))) # FIXME: end of added TableStep code table._data_cache[column][table.get_object_id(datum)] = data elif column.auto == "form_widget": # FIXME: Added for TableStep: widget = column.form_widget widget_name = "%s__%s__%s" % \ (self.table._meta.multi_select_name, column.name, unicode(table.get_object_id(datum))) data = widget.render(widget_name, column.get_data(datum), column.form_widget_attributes) table._data_cache[column][table.get_object_id(datum)] = data elif column.auto == "actions": data = table.render_row_actions(datum) table._data_cache[column][table.get_object_id(datum)] = data else: data = column.get_data(datum) cell = horizon_tables.Cell(datum, data, column, self) cells.append((column.name or column.auto, cell)) self.cells = datastructures.SortedDict(cells) if self.ajax: interval = conf.HORIZON_CONFIG['ajax_poll_interval'] self.attrs['data-update-interval'] = interval self.attrs['data-update-url'] = self.get_ajax_update_url() self.classes.append("ajax-update") # Add the row's status class and id to the attributes to be rendered. self.classes.append(self.status_class) id_vals = {"table": self.table.name, "sep": STRING_SEPARATOR, "id": table.get_object_id(datum)} self.id = "%(table)s%(sep)srow%(sep)s%(id)s" % id_vals self.attrs['id'] = self.id # Add the row's display name if available display_name = table.get_object_display(datum) if display_name: self.attrs['data-display'] = html.escape(display_name) class DataTableOptions(horizon_tables.DataTableOptions): def __init__(self, options): super(DataTableOptions, self).__init__(options) # FIXME: TableStep self.row_class = getattr(options, 'row_class', Row) self.column_class = getattr(options, 'column_class', Column) self.multi_select_name = getattr(options, 'multi_select_name', 'object_ids') class DataTableMetaclass(type): """ Metaclass to add options to DataTable class and collect columns. """ def __new__(mcs, name, bases, attrs): # Process options from Meta class_name = name attrs["_meta"] = opts = DataTableOptions(attrs.get("Meta", None)) # Gather columns; this prevents the column from being an attribute # on the DataTable class and avoids naming conflicts. columns = [] for attr_name, obj in attrs.items(): if issubclass(type(obj), (opts.column_class, Column)): column_instance = attrs.pop(attr_name) column_instance.name = attr_name column_instance.classes.append('normal_column') columns.append((attr_name, column_instance)) columns.sort(key=lambda x: x[1].creation_counter) # Iterate in reverse to preserve final order for base in bases[::-1]: if hasattr(base, 'base_columns'): columns = base.base_columns.items() + columns attrs['base_columns'] = datastructures.SortedDict(columns) # If the table is in a ResourceBrowser, the column number must meet # these limits because of the width of the browser. if opts.browser_table == "navigation" and len(columns) > 1: raise ValueError("You can only assign one column to %s." % class_name) if opts.browser_table == "content" and len(columns) > 2: raise ValueError("You can only assign two columns to %s." % class_name) if opts.columns: # Remove any columns that weren't declared if we're being explicit # NOTE: we're iterating a COPY of the list here! for column_data in columns[:]: if column_data[0] not in opts.columns: columns.pop(columns.index(column_data)) # Re-order based on declared columns columns.sort(key=lambda x: attrs['_meta'].columns.index(x[0])) # Add in our auto-generated columns if opts.multi_select and opts.browser_table != "navigation": multi_select = opts.column_class("multi_select", verbose_name="", auto="multi_select") multi_select.classes.append('multi_select_column') columns.insert(0, ("multi_select", multi_select)) if opts.actions_column: actions_column = opts.column_class("actions", verbose_name=_("Actions"), auto="actions") actions_column.classes.append('actions_column') columns.append(("actions", actions_column)) # Store this set of columns internally so we can copy them per-instance attrs['_columns'] = datastructures.SortedDict(columns) # Gather and register actions for later access since we only want # to instantiate them once. # (list() call gives deterministic sort order, which sets don't have.) actions = list(set(opts.row_actions) | set(opts.table_actions)) actions.sort(key=operator.attrgetter('name')) actions_dict = datastructures.SortedDict([(action.name, action()) for action in actions]) attrs['base_actions'] = actions_dict if opts._filter_action: # Replace our filter action with the instantiated version opts._filter_action = actions_dict[opts._filter_action.name] # Create our new class! return type.__new__(mcs, name, bases, attrs) class DataTable(object): """ A class which defines a table with all data and associated actions. .. attribute:: name String. Read-only access to the name specified in the table's Meta options. .. attribute:: multi_select Boolean. Read-only access to whether or not this table should display a column for multi-select checkboxes. .. attribute:: data Read-only access to the data this table represents. .. attribute:: filtered_data Read-only access to the data this table represents, filtered by the :meth:`~horizon.tables.FilterAction.filter` method of the table's :class:`~horizon.tables.FilterAction` class (if one is provided) using the current request's query parameters. """ __metaclass__ = DataTableMetaclass def __init__(self, request, data=None, needs_form_wrapper=None, **kwargs): self.request = request self.data = data self.kwargs = kwargs self._needs_form_wrapper = needs_form_wrapper self._no_data_message = self._meta.no_data_message self.breadcrumb = None self.current_item_id = None self.permissions = self._meta.permissions # Create a new set columns = [] for key, _column in self._columns.items(): column = copy.copy(_column) column.table = self columns.append((key, column)) self.columns = datastructures.SortedDict(columns) self._populate_data_cache() # Associate these actions with this table for action in self.base_actions.values(): action.table = self self.needs_summary_row = any([col.summation for col in self.columns.values()]) def __unicode__(self): return unicode(self._meta.verbose_name) def __repr__(self): return '<%s: %s>' % (self.__class__.__name__, self._meta.name) @property def name(self): return self._meta.name @property def footer(self): return self._meta.footer @property def multi_select(self): return self._meta.multi_select @property def filtered_data(self): if not hasattr(self, '_filtered_data'): self._filtered_data = self.data if self._meta.filter and self._meta._filter_action: action = self._meta._filter_action filter_string = self.get_filter_string() request_method = self.request.method needs_preloading = (not filter_string and request_method == 'GET' and action.needs_preloading) valid_method = (request_method == action.method) if (filter_string and valid_method) or needs_preloading: if self._meta.mixed_data_type: self._filtered_data = action.data_type_filter(self, self.data, filter_string) else: self._filtered_data = action.filter(self, self.data, filter_string) return self._filtered_data def get_filter_string(self): filter_action = self._meta._filter_action param_name = filter_action.get_param_name() filter_string = self.request.POST.get(param_name, '') return filter_string def _populate_data_cache(self): self._data_cache = {} # Set up hash tables to store data points for each column for column in self.get_columns(): self._data_cache[column] = {} def _filter_action(self, action, request, datum=None): try: # Catch user errors in permission functions here row_matched = True if self._meta.mixed_data_type: row_matched = action.data_type_matched(datum) return action._allowed(request, datum) and row_matched except Exception: LOG.exception("Error while checking action permissions.") return None def is_browser_table(self): if self._meta.browser_table: return True return False def render(self): """ Renders the table using the template from the table options. """ table_template = template.loader.get_template(self._meta.template) extra_context = {self._meta.context_var_name: self} context = template.RequestContext(self.request, extra_context) return table_template.render(context) def get_absolute_url(self): """ Returns the canonical URL for this table. This is used for the POST action attribute on the form element wrapping the table. In many cases it is also useful for redirecting after a successful action on the table. For convenience it defaults to the value of ``request.get_full_path()`` with any query string stripped off, e.g. the path at which the table was requested. """ return self.request.get_full_path().partition('?')[0] def get_empty_message(self): """ Returns the message to be displayed when there is no data. """ return self._no_data_message def get_object_by_id(self, lookup): """ Returns the data object from the table's dataset which matches the ``lookup`` parameter specified. An error will be raised if the match is not a single data object. We will convert the object id and ``lookup`` to unicode before comparison. Uses :meth:`~horizon.tables.DataTable.get_object_id` internally. """ if not isinstance(lookup, unicode): lookup = unicode(str(lookup), 'utf-8') matches = [] for datum in self.data: obj_id = self.get_object_id(datum) if not isinstance(obj_id, unicode): obj_id = unicode(str(obj_id), 'utf-8') if obj_id == lookup: matches.append(datum) if len(matches) > 1: raise ValueError("Multiple matches were returned for that id: %s." % matches) if not matches: raise exceptions.Http302(self.get_absolute_url(), _('No match returned for the id "%s".') % lookup) return matches[0] @property def has_actions(self): """ Boolean. Indicates whether there are any available actions on this table. """ if not self.base_actions: return False return any(self.get_table_actions()) or any(self._meta.row_actions) @property def needs_form_wrapper(self): """ Boolean. Indicates whather this table should be rendered wrapped in a ``