[Checkins] SVN: z3c.table/trunk/ Added ``GetItemColumn`` which gets the value by index/key access.

Adam Groszer cvs-admin at zope.org
Thu Aug 9 09:39:53 UTC 2012


Log message for revision 127443:
  Added ``GetItemColumn`` which gets the value by index/key access.

Changed:
  U   z3c.table/trunk/CHANGES.txt
  U   z3c.table/trunk/src/z3c/table/column.py
  U   z3c.table/trunk/src/z3c/table/column.txt

-=-
Modified: z3c.table/trunk/CHANGES.txt
===================================================================
--- z3c.table/trunk/CHANGES.txt	2012-08-09 09:16:10 UTC (rev 127442)
+++ z3c.table/trunk/CHANGES.txt	2012-08-09 09:39:50 UTC (rev 127443)
@@ -9,6 +9,7 @@
 
 - Added cell highlight (``getCSSHighlightClass``) CSS option
 
+- Added ``GetItemColumn`` which gets the value by index/key access.
 
 0.9.1 (2011-08-03)
 ------------------

Modified: z3c.table/trunk/src/z3c/table/column.py
===================================================================
--- z3c.table/trunk/src/z3c/table/column.py	2012-08-09 09:16:10 UTC (rev 127442)
+++ z3c.table/trunk/src/z3c/table/column.py	2012-08-09 09:39:50 UTC (rev 127443)
@@ -224,9 +224,9 @@
     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)
 
 
@@ -245,6 +245,24 @@
         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."""
 

Modified: z3c.table/trunk/src/z3c/table/column.txt
===================================================================
--- z3c.table/trunk/src/z3c/table/column.txt	2012-08-09 09:16:10 UTC (rev 127442)
+++ z3c.table/trunk/src/z3c/table/column.txt	2012-08-09 09:39:50 UTC (rev 127443)
@@ -471,11 +471,10 @@
 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):
   ...
   ...     attrName = 'title'
@@ -484,9 +483,6 @@
   >>> class GetAttrColumnTable(table.Table):
   ...     cssClassSortedOn = None
   ...
-  ...     attrName = 'title'
-  ...     defaultValue = u'missing'
-  ...
   ...     def setUpColumns(self):
   ...         return [
   ...             column.addColumn(self, GetTitleColumn, u'title'),
@@ -524,7 +520,7 @@
   </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):
   ...
@@ -534,9 +530,6 @@
   >>> class GetAttrColumnTable(table.Table):
   ...     cssClassSortedOn = None
   ...
-  ...     attrName = 'title'
-  ...     defaultValue = u'missing'
-  ...
   ...     def setUpColumns(self):
   ...         return [
   ...             column.addColumn(self, UndefinedAttributeColumn, u'missing'),
@@ -620,6 +613,227 @@
   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
 ----------------------
 



More information about the checkins mailing list