# coding: utf-8 from __future__ import unicode_literals from . import columns from .config import RequestConfig from .rows import BoundRows from .utils import (Accessor, AttributeDict, build_request, cached_property, computed_values, OrderBy, OrderByTuple, segment, Sequence) import copy import sys from django.core.paginator import Paginator from django.db.models.fields import FieldDoesNotExist from django.template import RequestContext from django.template.loader import get_template from collections import OrderedDict import six import warnings QUERYSET_ACCESSOR_SEPARATOR = '__' class TableData(object): """ Exposes a consistent API for :term:`table data`. :param data: iterable containing data for each row :type data: `~django.db.query.QuerySet` or `list` of `dict` :param table: `.Table` object """ def __init__(self, data, table): self.table = table # data may be a QuerySet-like objects with count() and order_by() if (hasattr(data, 'count') and callable(data.count) and hasattr(data, 'order_by') and callable(data.order_by)): self.queryset = data # otherwise it must be convertable to a list else: # do some light validation if hasattr(data, '__iter__') or (hasattr(data, '__len__') and hasattr(data, '__getitem__')): self.list = list(data) else: raise ValueError( 'data must be QuerySet-like (have count and ' 'order_by) or support list(data) -- %s has ' 'neither' % type(data).__name__ ) def __len__(self): if not hasattr(self, "_length"): # Use the queryset count() method to get the length, instead of # loading all results into memory. This allows, for example, # smart paginators that use len() to perform better. self._length = (self.queryset.count() if hasattr(self, 'queryset') else len(self.list)) return self._length @property def data(self): return self.queryset if hasattr(self, "queryset") else self.list @property def ordering(self): """ Returns the list of order by aliases that are enforcing ordering on the data. If the data is unordered, an empty sequence is returned. If the ordering can not be determined, `None` is returned. This works by inspecting the actual underlying data. As such it's only supported for querysets. """ if hasattr(self, "queryset"): aliases = {} for bound_column in self.table.columns: aliases[bound_column.order_by_alias] = bound_column.order_by try: return next(segment(self.queryset.query.order_by, aliases)) except StopIteration: pass def order_by(self, aliases): """ Order the data based on order by aliases (prefixed column names) in the table. :param aliases: optionally prefixed names of columns ('-' indicates descending order) in order of significance with regard to data ordering. :type aliases: `~.utils.OrderByTuple` """ accessors = [] for alias in aliases: bound_column = self.table.columns[OrderBy(alias).bare] # bound_column.order_by reflects the current ordering applied to # the table. As such we need to check the current ordering on the # column and use the opposite if it doesn't match the alias prefix. if alias[0] != bound_column.order_by_alias[0]: accessors += bound_column.order_by.opposite else: accessors += bound_column.order_by if hasattr(self, "queryset"): translate = lambda accessor: accessor.replace(Accessor.SEPARATOR, QUERYSET_ACCESSOR_SEPARATOR) self.queryset = self.queryset.order_by(*(translate(a) for a in accessors)) else: self.list.sort(key=OrderByTuple(accessors).key) def __iter__(self): """ for ... in ... default to using this. There's a bug in Django 1.3 with indexing into querysets, so this side-steps that problem (as well as just being a better way to iterate). """ return iter(self.data) def __getitem__(self, key): """ Slicing returns a new `.TableData` instance, indexing returns a single record. """ return self.data[key] @cached_property def verbose_name(self): """ The full (singular) name for the data. Queryset data has its model's `~django.db.Model.Meta.verbose_name` honored. List data is checked for a ``verbose_name`` attribute, and falls back to using ``"item"``. """ if hasattr(self, "queryset"): return self.queryset.model._meta.verbose_name return getattr(self.list, "verbose_name", "item") @cached_property def verbose_name_plural(self): """ The full (plural) name of the data. This uses the same approach as `.verbose_name`. """ if hasattr(self, "queryset"): return self.queryset.model._meta.verbose_name_plural return getattr(self.list, "verbose_name_plural", "items") class DeclarativeColumnsMetaclass(type): """ Metaclass that converts `.Column` objects defined on a class to the dictionary `.Table.base_columns`, taking into account parent class ``base_columns`` as well. """ def __new__(mcs, name, bases, attrs): attrs["_meta"] = opts = TableOptions(attrs.get("Meta", None)) # extract declared columns cols, remainder = [], {} for attr_name, attr in attrs.items(): if isinstance(attr, columns.Column): cols.append((attr_name, attr)) else: remainder[attr_name] = attr attrs = remainder cols.sort(key=lambda x: x[1].creation_counter) # If this class is subclassing other tables, add their fields as # well. Note that we loop over the bases in *reverse* - this is # necessary to preserve the correct order of columns. parent_columns = [] for base in bases[::-1]: if hasattr(base, "base_columns"): parent_columns = list(base.base_columns.items()) + parent_columns # Start with the parent columns attrs["base_columns"] = OrderedDict(parent_columns) # Possibly add some generated columns based on a model if opts.model: extra = OrderedDict() # honor Table.Meta.fields, fallback to model._meta.fields if opts.fields: # Each item in opts.fields is the name of a model field or a # normal attribute on the model for field_name in opts.fields: try: field = opts.model._meta.get_field(field_name) except FieldDoesNotExist: extra[field_name] = columns.Column() else: extra[field_name] = columns.library.column_for_field(field) else: for field in opts.model._meta.fields: extra[field.name] = columns.library.column_for_field(field) attrs["base_columns"].update(extra) # Explicit columns override both parent and generated columns attrs["base_columns"].update(OrderedDict(cols)) # Apply any explicit exclude setting for exclusion in opts.exclude: if exclusion in attrs["base_columns"]: attrs["base_columns"].pop(exclusion) # Now reorder the columns based on explicit sequence if opts.sequence: opts.sequence.expand(attrs["base_columns"].keys()) # Table's sequence defaults to sequence declared in Meta #attrs['_sequence'] = opts.sequence attrs["base_columns"] = OrderedDict(((x, attrs["base_columns"][x]) for x in opts.sequence)) # set localize on columns for col_name in attrs["base_columns"].keys(): localize_column = None if col_name in opts.localize: localize_column = True # unlocalize gets higher precedence if col_name in opts.unlocalize: localize_column = False if localize_column is not None: attrs["base_columns"][col_name].localize = localize_column return super(DeclarativeColumnsMetaclass, mcs).__new__(mcs, name, bases, attrs) class TableOptions(object): """ Extracts and exposes options for a `.Table` from a `.Table.Meta` when the table is defined. See `.Table` for documentation on the impact of variables in this class. :param options: options for a table :type options: `.Table.Meta` on a `.Table` """ # pylint: disable=R0902 def __init__(self, options=None): super(TableOptions, self).__init__() self.attrs = AttributeDict(getattr(options, "attrs", {})) self.default = getattr(options, "default", "—") self.empty_text = getattr(options, "empty_text", None) self.fields = getattr(options, "fields", ()) self.exclude = getattr(options, "exclude", ()) order_by = getattr(options, "order_by", None) if isinstance(order_by, six.string_types): order_by = (order_by, ) self.order_by = OrderByTuple(order_by) if order_by is not None else None self.order_by_field = getattr(options, "order_by_field", "sort") self.page_field = getattr(options, "page_field", "page") self.per_page = getattr(options, "per_page", 25) self.per_page_field = getattr(options, "per_page_field", "per_page") self.prefix = getattr(options, "prefix", "") self.sequence = Sequence(getattr(options, "sequence", ())) if hasattr(options, "sortable"): warnings.warn("`Table.Meta.sortable` is deprecated, use `orderable` instead", DeprecationWarning) self.orderable = self.sortable = getattr(options, "orderable", getattr(options, "sortable", True)) self.model = getattr(options, "model", None) self.template = getattr(options, "template", "django_tables2/table.html") self.localize = getattr(options, "localize", ()) self.unlocalize = getattr(options, "unlocalize", ()) class TableBase(object): """ A representation of a table. .. attribute:: attrs HTML attributes to add to the ``