The horizon.tables.base Module

class horizon.tables.base.Cell(datum, data, column, row, attrs=None, classes=None)[source]

Bases: horizon.utils.html.HTMLElement

Represents a single cell in the table.

Cell.get_default_classes()[source]

Returns a flattened string of the cell’s CSS classes.

Cell.get_status_class(status)[source]

Returns a css class name determined by the status value.

Cell.status[source]

Gets the status for the column based on the cell’s data.

Cell.value[source]

Returns a formatted version of the data for final output.

This takes into consideration the link` and empty_value attributes.

class horizon.tables.base.Column(transform, verbose_name=None, sortable=False, link=None, hidden=False, attrs=None, status=False, status_choices=None, empty_value=None, filters=None, classes=None)[source]

Bases: horizon.utils.html.HTMLElement

A class which represents a single column in a DataTable.

horizon.tables.base.transform

A string or callable. If transform is a string, it should be the name of the attribute on the underlying data class which should be displayed in this column. If it is a callable, it will be passed the current row’s data at render-time and should return the contents of the cell. Required.

horizon.tables.base.verbose_name

The name for this column which should be used for display purposes. Defaults to the value of transform with the first letter of each word capitalized.

horizon.tables.base.sortable

Boolean to determine whether this column should be sortable or not. Defaults to False.

horizon.tables.base.hidden

Boolean to determine whether or not this column should be displayed when rendering the table. Default: False.

A string or callable which returns a URL which will be wrapped around this column’s text as a link.

horizon.tables.base.status

Boolean designating whether or not this column represents a status (i.e. “enabled/disabled”, “up/down”, “active/inactive”). Default: False.

horizon.tables.base.status_choices

A tuple of tuples representing the possible data values for the status column and their associated boolean equivalent. Positive states should equate to True, negative states should equate to False, and indeterminate states should be None.

Values are compared in a case-insensitive manner.

Example (these are also the default values):

status_choices = (
        ('enabled', True),
        ('true', True)
        ('up', True),
        ('active', True),
        ('on', True),
        ('none', None),
        ('unknown', None),
        ('', None),
        ('disabled', False),
        ('down', False),
        ('false', False),
        ('inactive', False),
        ('off', False),
    )
horizon.tables.base.empty_value

A string to be used for cells which have no data. Defaults to an empty string.

horizon.tables.base.filters

A list of functions (often template filters) to be applied to the value of the data for this column prior to output. This is effectively a shortcut for writing a custom transform function in simple cases.

horizon.tables.base.classes

An iterable of CSS classes which should be added to this column. Example: classes=('foo', 'bar').

horizon.tables.base.attrs

A dict of HTML attribute strings which should be added to this column. Example: attrs={"data-foo": "bar"}.

Column.get_data(datum)[source]

Returns the appropriate data for this column from the given input.

The return value will be either the attribute specified for this column or the return value of the attr:~horizon.tables.Column.transform method for this column.

Column.get_link_url(datum)[source]

Returns the final value for the column’s link property.

If link is a callable, it will be passed the current data object and should return a URL. Otherwise get_link_url will attempt to call reverse on link with the object’s id as a parameter. Failing that, it will simply return the value of link.

horizon.tables.base.DataTable[source]

alias of get_row_actions

class horizon.tables.base.DataTableMetaclass[source]

Bases: type

Metaclass to add options to DataTable class and collect columns.

class horizon.tables.base.DataTableOptions(options)[source]

Bases: object

Contains options for DataTable objects.

horizon.tables.base.name

A short name or slug for the table.

horizon.tables.base.verbose_name

A more verbose name for the table meant for display purposes.

horizon.tables.base.columns

A list of column objects or column names. Controls ordering/display of the columns in the table.

horizon.tables.base.table_actions

A list of action classes derived from the Action class. These actions will handle tasks such as bulk deletion, etc. for multiple objects at once.

horizon.tables.base.row_actions

A list similar to table_actions except tailored to appear for each row. These actions act on a single object at a time.

horizon.tables.base.actions_column

Boolean value to control rendering of an additional column containing the various actions for each row. Defaults to True if any actions are specified in the row_actions option.

horizon.tables.base.multi_select

Boolean value to control rendering of an extra column with checkboxes for selecting multiple objects in the table. Defaults to True if any actions are specified in the table_actions option.

horizon.tables.base.filter

Boolean value to control the display of the “filter” search box in the table actions. By default it checks whether or not an instance of FilterAction is in table_actions.

horizon.tables.base.template

String containing the template which should be used to render the table. Defaults to "horizon/common/_data_table.html".

horizon.tables.base.context_var_name

The name of the context variable which will contain the table when it is rendered. Defaults to "table".

horizon.tables.base.status_columns

A list or tuple of column names which represents the “state” of the data object being represented.

If status_columns is set, when the rows are rendered the value of this column will be used to add an extra class to the row in the form of "status_up" or "status_down" for that row’s data.

The row status is used by other Horizon components to trigger tasks such as dynamic AJAX updating.

horizon.tables.base.row_class

The class which should be used for rendering the rows of this table. Optional. Default: Row.

horizon.tables.base.column_class

The class which should be used for handling the columns of this table. Optional. Default: Column.

class horizon.tables.base.Row(table, datum=None)[source]

Bases: horizon.utils.html.HTMLElement

Represents a row in the table.

When iterated, the Row instance will yield each of its cells.

Rows are capable of AJAX updating, with a little added work:

The ajax property needs to be set to True, and subclasses need to define a get_data method which returns a data object appropriate for consumption by the table (effectively the “get” lookup versus the table’s “list” lookup).

The automatic update interval is configurable by setting the key ajax_poll_interval in the settings.HORIZON_CONFIG dictionary. Default: 2500 (measured in milliseconds).

horizon.tables.base.table

The table which this row belongs to.

horizon.tables.base.datum

The data object which this row represents.

horizon.tables.base.id

A string uniquely representing this row composed of the table name and the row data object’s identifier.

horizon.tables.base.cells

The cells belonging to this row stored in a SortedDict object. This attribute is populated during instantiation.

horizon.tables.base.status

Boolean value representing the status of this row calculated from the values of the table’s status_columns if they are set.

horizon.tables.base.status_class

Returns a css class for the status of the row based on status.

horizon.tables.base.ajax

Boolean value to determine whether ajax updating for this row is enabled.

horizon.tables.base.ajax_action_name

String that is used for the query parameter key to request AJAX updates. Generally you won’t need to change this value. Default: "row_update".

Row.get_ajax_update_url()[source]
Row.get_cells()[source]

Returns the bound cells for this row in order.

Row.get_data(request, obj_id)[source]

Fetches the updated data for the row based on the object id passed in. Must be implemented by a subclass to allow AJAX updating.

Row.load_cells(datum=None)[source]

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 __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.

Row.render()[source]
Row.status[source]
Row.status_class[source]

Previous topic

The horizon.tables.views Module

Next topic

The horizon.tables.actions Module

This Page