From: Bradley Ayers Date: Mon, 6 Jun 2011 13:29:18 +0000 (+1000) Subject: Added ability to specify attrs keyword argument to a table constructor to override... X-Git-Tag: v0.5.0^2~5 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=3c72f774e6674d30785a8ace4578525a0e97ee05;p=django-tables2.git Added ability to specify attrs keyword argument to a table constructor to override the Table.Meta.attrs setting. --- diff --git a/django_tables/tables.py b/django_tables/tables.py index 7d516c5..fc9fc5d 100644 --- a/django_tables/tables.py +++ b/django_tables/tables.py @@ -6,6 +6,7 @@ from django.http import Http404 from django.template.loader import get_template from django.template import Context from django.utils.encoding import StrAndUnicode +from django.db.models.query import QuerySet from .utils import OrderBy, OrderByTuple, Accessor, AttributeDict from .rows import BoundRows, BoundRow from .columns import BoundColumns, Column @@ -22,9 +23,7 @@ class TableData(object): This class is used by :class:`.Table` to wrap any input table data. """ - def __init__(self, data, table): - from django.db.models.query import QuerySet if isinstance(data, QuerySet): self.queryset = data elif isinstance(data, list): @@ -185,10 +184,11 @@ class Table(StrAndUnicode): TableDataClass = TableData def __init__(self, data, order_by=None, sortable=None, empty_text=None, - exclude=None): + exclude=None, attrs=None): self._rows = BoundRows(self) self._columns = BoundColumns(self) self._data = self.TableDataClass(data=data, table=self) + self.attrs = attrs self.empty_text = empty_text self.sortable = sortable if order_by is None: @@ -282,7 +282,11 @@ class Table(StrAndUnicode): :rtype: :class:`~.utils.AttributeDict` object. """ - return self._meta.attrs + return self._attrs if self._attrs is not None else self._meta.attrs + + @attrs.setter + def attrs(self, value): + self._attrs = value def paginate(self, klass=Paginator, per_page=25, page=1, *args, **kwargs): self.paginator = klass(self.rows, per_page, *args, **kwargs) diff --git a/docs/index.rst b/docs/index.rst index d6e6a3f..be8b607 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -718,10 +718,20 @@ API Reference Default: ``{}`` + .. note:: + + This functionality is also available via the ``attrs`` keyword + argument to a table's constructor. + .. attribute:: empty_text Defines the text to display when the table has no rows. + .. note:: + + This functionality is also available via the ``empty_text`` keyword + argument to a table's constructor. + .. attribute:: exclude Defines which columns should be excluded from the table. This is useful @@ -745,6 +755,11 @@ API Reference Default: ``()`` + .. note:: + + This functionality is also available via the ``exclude`` keyword + argument to a table's constructor. + .. attribute:: order_by The default ordering. e.g. ``('name', '-age')``. A hyphen ``-`` can be @@ -754,6 +769,11 @@ API Reference Default: ``()`` + .. note:: + + This functionality is also available via the ``order_by`` keyword + argument to a table's constructor. + .. attribute:: sortable The default value for determining if a :class:`.Column` is sortable. @@ -767,6 +787,11 @@ API Reference Default: :const:`True` + .. note:: + + This functionality is also available via the ``sortable`` keyword + argument to a table's constructor. + :class:`TableData` Objects: ------------------------------ diff --git a/tests/core.py b/tests/core.py index 50af9ff..24f5b3a 100644 --- a/tests/core.py +++ b/tests/core.py @@ -56,6 +56,29 @@ def declarations(): assert 'added' in CityTable.base_columns +@core.test +def attrs(): + class TestTable(tables.Table): + class Meta: + attrs = {} + Assert({}) == TestTable([]).attrs + + class TestTable2(tables.Table): + class Meta: + attrs = {"a": "b"} + Assert({"a": "b"}) == TestTable2([]).attrs + + class TestTable3(tables.Table): + pass + Assert({}) == TestTable3([]).attrs + Assert({"a": "b"}) == TestTable3([], attrs={"a": "b"}).attrs + + class TestTable4(tables.Table): + class Meta: + attrs = {"a": "b"} + Assert({"c": "d"}) == TestTable4([], attrs={"c": "d"}).attrs + + @core.test def datasource_untouched(): """Ensure that data that is provided to the table (the datasource) is not