Skip to content

Commit

Permalink
Added GetItemColumn which gets the value by index/key access.
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Groszer committed Aug 9, 2012
1 parent c146449 commit bd5a63c
Show file tree
Hide file tree
Showing 3 changed files with 246 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ CHANGES

- Added cell highlight (``getCSSHighlightClass``) CSS option

- Added ``GetItemColumn`` which gets the value by index/key access.

0.9.1 (2011-08-03)
------------------
Expand Down
22 changes: 20 additions & 2 deletions src/z3c/table/column.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,9 @@ def update(self):
def renderCell(self, item):
selected = u''
if item in self.selectedItems:
selected='checked="checked"'
selected = 'checked="checked"'
return u'<input type="checkbox" class="%s" name="%s" value="%s" %s />' \
%('checkbox-widget', self.getItemKey(item), self.getItemValue(item),
% ('checkbox-widget', self.getItemKey(item), self.getItemValue(item),
selected)


Expand All @@ -245,6 +245,24 @@ def renderCell(self, item):
return self.getValue(item)


class GetItemColumn(Column):
"""Get value from item index/key column."""

idx = None
defaultValue = u''

def getValue(self, obj):
if obj is not None and self.idx is not None:
try:
return obj[self.idx]
except (KeyError, IndexError, Unauthorized):
return self.defaultValue
return self.defaultValue

def renderCell(self, item):
return self.getValue(item)


class I18nGetAttrColumn(GetAttrColumn):
"""GetAttrColumn which translates its content."""

Expand Down
236 changes: 225 additions & 11 deletions src/z3c/table/column.txt
Original file line number Diff line number Diff line change
Expand Up @@ -471,10 +471,9 @@ adapter which only returns ``02/02/02 02:02`` as modified date:
GetAttrColumn
-------------

The ``GetAttrColumn`` column is a mixin which is used in ``CreatedColumn`` and
in ``ModifiedColumn``. Not all code get used if everything is fine. So let's
test the column itself and force some use case:

The ``GetAttrColumn`` column is a handy column that retrieves the value from
the item by attribute access.
It also provides a ``defaultValue`` in case an exception happens.

>>> class GetTitleColumn(column.GetAttrColumn):
...
Expand All @@ -484,9 +483,6 @@ test the column itself and force some use case:
>>> class GetAttrColumnTable(table.Table):
... cssClassSortedOn = None
...
... attrName = 'title'
... defaultValue = u'missing'
...
... def setUpColumns(self):
... return [
... column.addColumn(self, GetTitleColumn, u'title'),
Expand Down Expand Up @@ -524,7 +520,7 @@ Render and update the table:
</table>

If we use a non-existing Attribute, we do not raise an AttributeError, we will
get the default value defined from the ``GetAttrColumnTable``:
get the default value:

>>> class UndefinedAttributeColumn(column.GetAttrColumn):
...
Expand All @@ -534,9 +530,6 @@ get the default value defined from the ``GetAttrColumnTable``:
>>> class GetAttrColumnTable(table.Table):
... cssClassSortedOn = None
...
... attrName = 'title'
... defaultValue = u'missing'
...
... def setUpColumns(self):
... return [
... column.addColumn(self, UndefinedAttributeColumn, u'missing'),
Expand Down Expand Up @@ -620,6 +613,227 @@ And test the attribute access:
u'missing'


GetItemColumn
-------------

The ``GetItemColumn`` column is a handy column that retrieves the value from
the item by index or key access. That means the item can be a tuple, list, dict
or anything that implements that.
It also provides a ``defaultValue`` in case an exception happens.

Dict-ish
.........

>>> sampleDictData = [
... dict(name='foo', value=1),
... dict(name='bar', value=7),
... dict(name='moo', value=42),]

>>> class GetDictColumnTable(table.Table):
... cssClassSortedOn = None
...
... def setUpColumns(self):
... return [
... column.addColumn(self, column.GetItemColumn, u'name',
... header=u'Name',
... idx='name', defaultValue='missing'),
... column.addColumn(self, column.GetItemColumn, u'value',
... header=u'Value',
... idx='value', defaultValue='missing'),
... ]
... @property
... def values(self):
... return sampleDictData

Render and update the table:

>>> request = TestRequest()
>>> getDictColumnTable = GetDictColumnTable(sampleDictData, request)
>>> getDictColumnTable.update()
>>> print getDictColumnTable.render()
<table>
<thead>
<tr>
<th>Name</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>bar</td>
<td>7</td>
</tr>
<tr>
<td>foo</td>
<td>1</td>
</tr>
<tr>
<td>moo</td>
<td>42</td>
</tr>
</tbody>
</table>

If we use a non-existing index/key, we do not raise an exception, we will
get the default value:

>>> class GetDictColumnTable(table.Table):
... cssClassSortedOn = None
...
... def setUpColumns(self):
... return [
... column.addColumn(self, column.GetItemColumn, u'name',
... idx='not-existing', defaultValue='missing'),
... ]
... @property
... def values(self):
... return sampleDictData

Render and update the table:

>>> request = TestRequest()
>>> getDictColumnTable = GetDictColumnTable(container, request)
>>> getDictColumnTable.update()
>>> print getDictColumnTable.render()
<table>
<thead>
<tr>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td>missing</td>
</tr>
<tr>
<td>missing</td>
</tr>
<tr>
<td>missing</td>
</tr>
</tbody>
</table>

A missing ``idx`` in ``GetItemColumn`` would also end in return the
``defaultValue``:

>>> class BadIdxColumn(column.GetItemColumn):
...
... defaultValue = u'missing'

>>> firstItem = sampleDictData[0]
>>> simpleTable = table.Table(sampleDictData, request)
>>> badColumn = column.addColumn(simpleTable, BadIdxColumn, u'bad')
>>> badColumn.renderCell(firstItem)
u'missing'

Tuple/List-ish
...............

>>> sampleTupleData = [
... (50, 'bar'),
... (42, 'cent'),
... (7, 'bild'),]

>>> class GetTupleColumnTable(table.Table):
... cssClassSortedOn = None
...
... def setUpColumns(self):
... return [
... column.addColumn(self, column.GetItemColumn, u'name',
... header=u'Name',
... idx=1, defaultValue='missing'),
... column.addColumn(self, column.GetItemColumn, u'value',
... header=u'Value',
... idx=0, defaultValue='missing'),
... ]
... @property
... def values(self):
... return sampleTupleData

Render and update the table:

>>> request = TestRequest()
>>> getTupleColumnTable = GetTupleColumnTable(sampleTupleData, request)
>>> getTupleColumnTable.update()
>>> print getTupleColumnTable.render()
<table>
<thead>
<tr>
<th>Name</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>bar</td>
<td>50</td>
</tr>
<tr>
<td>bild</td>
<td>7</td>
</tr>
<tr>
<td>cent</td>
<td>42</td>
</tr>
</tbody>
</table>

If we use a non-existing index/key, we do not raise an exception, we will
get the default value:

>>> class GetTupleColumnTable(table.Table):
... cssClassSortedOn = None
...
... def setUpColumns(self):
... return [
... column.addColumn(self, column.GetItemColumn, u'name',
... idx=42, defaultValue='missing'),
... ]
... @property
... def values(self):
... return sampleTupleData

Render and update the table:

>>> request = TestRequest()
>>> getTupleColumnTable = GetTupleColumnTable(container, request)
>>> getTupleColumnTable.update()
>>> print getTupleColumnTable.render()
<table>
<thead>
<tr>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td>missing</td>
</tr>
<tr>
<td>missing</td>
</tr>
<tr>
<td>missing</td>
</tr>
</tbody>
</table>

A missing ``idx`` in ``GetItemColumn`` would also end in return the
``defaultValue``:

>>> class BadIdxColumn(column.GetItemColumn):
...
... defaultValue = u'missing'

>>> firstItem = sampleTupleData[0]
>>> simpleTable = table.Table(sampleTupleData, request)
>>> badColumn = column.addColumn(simpleTable, BadIdxColumn, u'bad')
>>> badColumn.renderCell(firstItem)
u'missing'


GetAttrFormatterColumn
----------------------

Expand Down

0 comments on commit bd5a63c

Please sign in to comment.