Deprecate Attrs

This commit is contained in:
Bradley Ayers 2012-09-18 15:07:24 +10:00
parent 0df5c4d50f
commit 594664fdec
7 changed files with 53 additions and 59 deletions

View File

@ -90,6 +90,7 @@ v0.12.0
delete all your data when a column had an accessor of ``delete``
- Add ``default`` and ``value`` to context of ``TemplateColumn``
- Add cardinality indication to the pagination area of a table
- ``Attrs`` is deprecated, use ``dict`` instead
v0.11.0
-------

View File

@ -14,7 +14,7 @@ from itertools import ifilter, islice
import warnings
import inspect
from .templatetags.django_tables2 import title
from .utils import A, AttributeDict, Attrs, OrderBy, OrderByTuple
from .utils import A, AttributeDict, OrderBy, OrderByTuple
funcs = ifilter(curry(hasattr, inspect), ('getfullargspec', 'getargspec'))
@ -99,10 +99,10 @@ class Column(object): # pylint: disable=R0902
:type orderable: :class:`bool`
:param orderable: If :const:`False`, this column will not be allowed to
influence row ordering/sorting.
:type attrs: :class:`Attrs` object
:type attrs: :class:`dict` object
:param attrs: HTML attributes to be added to components in the column
Supported ``Attrs`` keys are:
Supported ``attrs`` keys are:
- *th* -- ``<th>`` element in header
- *td* -- ``<td>`` element in body
@ -132,12 +132,7 @@ class Column(object): # pylint: disable=R0902
if orderable is None:
orderable = sortable
self.orderable = orderable
attrs = attrs or Attrs()
if not isinstance(attrs, Attrs):
warnings.warn('attrs must be Attrs object, not %s'
% type(attrs).__name__, DeprecationWarning)
attrs = Attrs(attrs)
self.attrs = attrs
self.attrs = attrs or {}
# massage order_by into an OrderByTuple or None
order_by = (order_by, ) if isinstance(order_by, basestring) else order_by
self.order_by = OrderByTuple(order_by) if order_by is not None else None
@ -236,7 +231,7 @@ class BooleanColumn(Column):
Rendered values are wrapped in a ``<span>`` to allow customisation by
themes. By default the span is given the class ``true``, ``false``.
In addition to ``Attrs`` keys supported by ``Column``, the following are
In addition to ``attrs`` keys supported by ``Column``, the following are
available:
- *span* -- adds attributes to the <span> tag
@ -290,7 +285,7 @@ class CheckBoxColumn(Column):
implemented. If you want something to actually happen, you'll need to
implement that yourself.
In addition to ``Attrs`` keys supported by ``Column``, the following are
In addition to ``attrs`` keys supported by ``Column``, the following are
available:
- *input* -- ``<input>`` elements in both ``<td>`` and ``<th>``.
@ -298,19 +293,23 @@ class CheckBoxColumn(Column):
- *td__input* -- Replaces *input* attrs in body cells.
"""
def __init__(self, attrs=None, **extra):
header_attrs = extra.pop('header_attrs', None)
# For backwards compatibility, passing in a normal dict effectively
# should assign attributes to the `<input>` tag.
attrs = attrs or Attrs()
if not isinstance(attrs, Attrs):
warnings.warn('attrs must be Attrs object, not %s'
% type(attrs).__name__, DeprecationWarning)
attrs = Attrs(td__input=attrs)
valid = set(("input", "th__input", "td__input", "th", "td", "cell"))
if attrs and not set(attrs) & set(valid):
# if none of the keys in attrs are actually valid, assume it's some
# old code that should be be interpreted as {"td__input": ...}
warnings.warn('attrs keys must be one of %s, interpreting as {"td__input": %s}'
% (', '.join(valid), attrs), DeprecationWarning)
attrs = {"td__input": attrs}
# This is done for backwards compatible too, there used to be a
# ``header_attrs`` argument, but this has been deprecated. We'll
# maintain it for a while by translating it into ``head.checkbox``.
if header_attrs:
attrs.setdefault('th__input', header_attrs)
if "header_attrs" in extra:
warnings.warn('header_attrs argument is deprecated, '
'use attrs={"th__input": ...} instead',
DeprecationWarning)
attrs.setdefault('th__input', {}).update(extra.pop('header_attrs'))
kwargs = {b'orderable': False, b'attrs': attrs}
kwargs.update(extra)
@ -345,12 +344,13 @@ class BaseLinkColumn(Column):
``<a href="...">`` tag.
"""
def __init__(self, attrs=None, *args, **kwargs):
# backwards compatible translation for naive attrs value
attrs = attrs or Attrs()
if not isinstance(attrs, Attrs):
warnings.warn('attrs must be Attrs object, not %s'
% type(attrs).__name__, DeprecationWarning)
attrs = Attrs(a=attrs)
valid = set(("a", "th", "td", "cell"))
if attrs and not set(attrs) & set(valid):
# if none of the keys in attrs are actually valid, assume it's some
# old code that should be be interpreted as {"a": ...}
warnings.warn('attrs keys must be one of %s, interpreting as {"a": %s}'
% (', '.join(valid), attrs), DeprecationWarning)
attrs = {"a": attrs}
kwargs[b'attrs'] = attrs
super(BaseLinkColumn, self).__init__(*args, **kwargs)
@ -417,7 +417,7 @@ class LinkColumn(BaseLinkColumn):
class PeopleTable(tables.Table):
name = tables.LinkColumn('people_detail', args=[A('pk')])
In addition to ``Attrs`` keys supported by ``Column``, the following are
In addition to ``attrs`` keys supported by ``Column``, the following are
available:
- *a* -- ``<a>`` elements in ``<td>``.
@ -685,7 +685,7 @@ class BoundColumn(object):
what's actually defined in the column attrs. This makes writing
templates easier.
"""
# Work on a copy of the Attrs object since we're tweaking stuff
# Work on a copy of the attrs object since we're tweaking stuff
attrs = dict(self.column.attrs)
# Find the relevant th attributes (fall back to cell if th isn't

View File

@ -6,6 +6,7 @@ from django.utils.safestring import mark_safe
from django.test.client import FakePayload
from itertools import chain
from StringIO import StringIO
import warnings
class Sequence(list):
@ -352,19 +353,12 @@ class AttributeDict(dict):
class Attrs(dict):
"""
A collection of :class:`AttributeDict`, each given a key.
This class is used as a container to hold differenct sets of attributes for
a given column. Keys indicate where the attributes should be used, and
support varies depending on the column.
It's used in favour of a standard `dict` to enable backwards compatibility.
Before it was introduced, columns had an `attrs` parameter that would be
given a `dict` and would assign it to a single (typically input) element.
The new approach allows attributes to be specified for multiple elements.
By using the `Attrs` class your intention to use the new mechanism is
explicit.
Backwards compatibility, deprecated.
"""
def __init__(self, *args, **kwargs):
super(Attrs, self).__init__(*args, **kwargs)
warnings.warn("Attrs class is deprecated, use dict instead.",
DeprecationWarning)
def segment(sequence, aliases):

View File

@ -14,7 +14,7 @@ sys.path.pop(0)
project = 'django-tables2'
with open('../django_tables2/__init__.py', 'r') as f:
with open('../django_tables2/__init__.py', 'rb') as f:
release = re.search('__version__ = "(.+?)"', f.read()).group(1)
version = release.rpartition('.')[0]

View File

@ -462,20 +462,19 @@ fields with a table-specific name. e.g.
Column attributes
=================
Column attributes can be specified using the :class:`.Attrs` object. An
``Attrs`` object defines HTML tag attributes for one of more elements within
the column. Depending on the column, different elements are supported, however
``th`` and ``td`` are supported universally.
Column attributes can be specified using the :class:`dict` with specific keys.
The dict defines HTML attributes for one of more elements within the column.
Depending on the column, different elements are supported, however ``th``,
``td``, and ``cell`` are supported universally.
e.g.
.. sourcecode:: python
>>> from django_tables2 import Attrs
>>> import django_tables2 as tables
>>>
>>> class SimpleTable(tables.Table):
... name = tables.Column(attrs=Attrs(th={"id": "foo"}))
... name = tables.Column(attrs={"th": {"id": "foo"}})
...
>>> SimpleTable(data).as_html()
"{snip}<thead><tr><th id="foo" class="name">{snip}<tbody><tr><td class="name">{snip}"
@ -856,12 +855,6 @@ API Reference
.. autoclass:: django_tables2.utils.Accessor
:class:`Attrs` Objects:
--------------------------
.. autoclass:: django_tables2.utils.Attrs
:class:`RequestConfig` Objects:
-------------------------------

View File

@ -1,10 +1,15 @@
#!/usr/bin/env python
import re
from setuptools import setup, find_packages
with open('django_tables2/__init__.py', 'rb') as f:
version = re.search('__version__ = "(.+?)"', f.read()).group(1)
setup(
name='django-tables2',
version='0.11.0',
version=version,
description='Table/data-grid framework for Django',
author='Bradley Ayers',

View File

@ -96,10 +96,11 @@ def attrs_should_be_translated_for_backwards_compatibility():
@checkboxcolumn.test
def new_attrs_should_be_supported():
class TestTable(tables.Table):
col1 = tables.CheckBoxColumn(attrs=Attrs(th__input={"th_key": "th_value"},
td__input={"td_key": "td_value"}))
col2 = tables.CheckBoxColumn(attrs=Attrs(input={"key": "value"}))
with warns(DeprecationWarning):
class TestTable(tables.Table):
col1 = tables.CheckBoxColumn(attrs=Attrs(th__input={"th_key": "th_value"},
td__input={"td_key": "td_value"}))
col2 = tables.CheckBoxColumn(attrs=Attrs(input={"key": "value"}))
table = TestTable([{"col1": "data", "col2": "data"}])
assert attrs(table.columns["col1"].header) == {"type": "checkbox", "th_key": "th_value"}
@ -368,7 +369,7 @@ def bound_columns_should_support_indexing():
@general.test
def cell_attrs_applies_to_td_and_th():
class SimpleTable(tables.Table):
a = tables.Column(attrs=Attrs(cell={"key": "value"}))
a = tables.Column(attrs={"cell": {"key": "value"}})
# providing data ensures 1 row is rendered
table = SimpleTable([{"a": "value"}])
@ -495,7 +496,7 @@ def old_style_attrs_should_still_work():
def a_attrs_should_be_supported():
class TestTable(tables.Table):
col = tables.LinkColumn('occupation', kwargs={"pk": A('col')},
attrs=Attrs(a={"title": "Occupation Title"}))
attrs={"a": {"title": "Occupation Title"}})
table = TestTable([{"col": 0}])
assert attrs(table.rows[0]["col"]) == {"href": reverse("occupation", kwargs={"pk": 0}),