Skip to content

Commit

Permalink
mostly done, just need to add empty_text functionality, and sortable
Browse files Browse the repository at this point in the history
  • Loading branch information
bradleyayers committed Apr 6, 2011
1 parent c5b6cbf commit 13a9014
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 277 deletions.
154 changes: 0 additions & 154 deletions django_tables/proxies.py

This file was deleted.

30 changes: 9 additions & 21 deletions django_tables/rows.py
Expand Up @@ -3,7 +3,6 @@
import inspect
from django.utils.safestring import EscapeUnicode, SafeData
from django.utils.functional import curry
from .proxies import TemplateSafeLazyProxy


class BoundRow(object):
Expand Down Expand Up @@ -113,25 +112,25 @@ def value():
return raw if raw is not None else bound_column.default

kwargs = {
'value': TemplateSafeLazyProxy(value),
'record': self.record,
'column': bound_column.column,
'bound_column': bound_column,
'bound_row': self,
'table': self._table,
'value': value, # already a function
'record': lambda: self.record,
'column': lambda: bound_column.column,
'bound_column': lambda: bound_column,
'bound_row': lambda: self,
'table': lambda: self._table,
}
render_FOO = 'render_' + bound_column.name
render = getattr(self.table, render_FOO, bound_column.column.render)

# just give a list of all available methods
available = ifilter(curry(hasattr, inspect), ('getfullargspec', 'getargspec'))
spec = getattr(inspect, next(available))
funcs = ifilter(curry(hasattr, inspect), ('getfullargspec', 'getargspec'))
spec = getattr(inspect, next(funcs))
# only provide the arguments that the func is interested in
kw = {}
for name in spec(render).args:
if name == 'self':
continue
kw[name] = kwargs[name]
kw[name] = kwargs[name]()
return render(**kw)

def __contains__(self, item):
Expand All @@ -157,17 +156,6 @@ class BoundRows(object):
def __init__(self, table):
self.table = table

def page(self):
"""
If the table is paginated, return an iterable of :class:`.BoundRow`
objects that appear on the current page.
:rtype: iterable of :class:`.BoundRow` objects, or :const:`None`.
"""
if not hasattr(self.table, 'page'):
return None
return iter(self.table.page.object_list)

def __iter__(self):
"""Convience method for :meth:`.BoundRows.all`"""
for record in self.table.data:
Expand Down
48 changes: 30 additions & 18 deletions django_tables/tables.py
Expand Up @@ -144,6 +144,7 @@ def __init__(self, options=None):
order_by = (order_by, )
self.order_by = OrderByTuple(order_by)
self.attrs = AttributeDict(getattr(options, 'attrs', {}))
self.empty_text = getattr(options, 'empty_text', None)


class Table(StrAndUnicode):
Expand All @@ -153,50 +154,45 @@ class Table(StrAndUnicode):
:type data: ``list`` or ``QuerySet``
:param data: The :term:`table data`.
:type order_by: ``Table.DoNotOrder``, ``None``, ``tuple`` or ``basestring``
:type order_by: ``None``, ``tuple`` or ``string``
:param order_by: sort the table based on these columns prior to display.
(default :attr:`.Table.Meta.order_by`)
The ``order_by`` argument is optional and allows the table's
``Meta.order_by`` option to be overridden. If the ``bool(order_by)``
evaluates to ``False``, the table's ``Meta.order_by`` will be used. If you
want to disable a default ordering, you must pass in the value
``Table.DoNotOrder``.
``Meta.order_by`` option to be overridden. If the ``order_by is None``
the table's ``Meta.order_by`` will be used. If you want to disable a
default ordering, simply use an empty ``tuple``, ``string``, or ``list``,
e.g. ``Table(…, order_by='')``.
Example:
.. code-block:: python
def obj_list(request):
...
# We don't want a default sort
order_by = request.GET.get('sort', SimpleTable.DoNotOrder)
# If there's no ?sort=…, we don't want to fallback to
# Table.Meta.order_by, thus we must not default to passing in None
order_by = request.GET.get('sort', ())
table = SimpleTable(data, order_by=order_by)
...
"""
__metaclass__ = DeclarativeColumnsMetaclass

# this value is not the same as None. it means 'use the default sort
# order', which may (or may not) be inherited from the table options.
# None means 'do not sort the data', ignoring the default.
DoNotOrder = type('DoNotOrder', (), {})
TableDataClass = TableData

def __init__(self, data, order_by=None):
def __init__(self, data, order_by=None, sortable=None, empty_text=None):
self._rows = BoundRows(self) # bound rows
self._columns = BoundColumns(self) # bound columns
self._data = self.TableDataClass(data=data, table=self)

# None is a valid order, so we must use DefaultOrder as a flag
# to fall back to the table sort order.
if not order_by:
if order_by is None:
self.order_by = self._meta.order_by
elif order_by is Table.DoNotOrder:
self.order_by = None
else:
self.order_by = order_by

self.sortable = sortable
self.empty_text = empty_text

# Make a copy so that modifying this will not touch the class
# definition. Note that this is different from forms, where the
# copy is made available in a ``fields`` attribute. See the
Expand Down Expand Up @@ -234,6 +230,22 @@ def order_by(self, value):
self._order_by = order_by
self._data.order_by(order_by)

@property
def sortable(self):
return self._sortable if self._sortable is not None else self._meta.sortable

@sortable.setter
def sortable(self, value):
self._sortable = value

@property
def empty_text(self):
return self._empty_text if self._empty_text is not None else self._meta.empty_text

@empty_text.setter
def empty_text(self, value):
self._empty_text = value

@property
def rows(self):
return self._rows
Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Expand Up @@ -52,7 +52,7 @@
# The short X.Y version.
version = '0.4.0'
# The full version, including alpha/beta/rc tags.
release = '0.4.0.beta4'
release = '0.4.0.beta5'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -4,7 +4,7 @@

setup(
name='django-tables',
version='0.4.0.beta4',
version='0.4.0.beta5',
description='Table framework for Django',

author='Bradley Ayers',
Expand Down
3 changes: 2 additions & 1 deletion tests/__init__.py
Expand Up @@ -26,6 +26,7 @@
from .templates import templates
from .models import models
from .utils import utils
from .rows import rows


everything = Tests([core, templates, models, utils])
everything = Tests([core, templates, models, utils, rows])

0 comments on commit 13a9014

Please sign in to comment.