[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/table/ Added column module, changed formatters to take request instead of context.

Benji York benji at zope.com
Mon Jan 17 11:08:09 EST 2005


Log message for revision 28854:
  Added column module, changed formatters to take request instead of context.

Changed:
  U   Zope3/trunk/src/zope/app/table/README.txt
  U   Zope3/trunk/src/zope/app/table/__init__.py
  A   Zope3/trunk/src/zope/app/table/column.py
  U   Zope3/trunk/src/zope/app/table/interfaces.py

-=-
Modified: Zope3/trunk/src/zope/app/table/README.txt
===================================================================
--- Zope3/trunk/src/zope/app/table/README.txt	2005-01-17 15:30:10 UTC (rev 28853)
+++ Zope3/trunk/src/zope/app/table/README.txt	2005-01-17 16:08:08 UTC (rev 28854)
@@ -48,18 +48,21 @@
 When a table is rendered its display is modified with the use of a
 configuration object.  Such objects must conform to ITableConfiguration::
 
-    >>> from zope.app.table.interfaces import ITableConfiguration
-    >>> class MyTableConfiguration:
-    ...     zope.interface.implements(ITableConfiguration)
-    ...     visible_columns = ('First', 'Third')
-    ...     sort_on = None
-    ...     sort_reverse = False
-    ...     batch_size = 10
-    ...     def __init__(self, columns):
-    ...         self.columns = columns
-    >>> config = MyTableConfiguration(columns)
+    >>> from zope.app.table import TableConfiguration
+    >>> config = TableConfiguration(columns)
 
+By default, all columns are visible::
 
+    >>> config.visible_columns
+    ('First', 'Second', 'Third')
+
+But, we don't want to see the column titled "Second"::
+
+    >>> config.visible_columns = ('First', 'Third')
+    >>> config.visible_columns
+    ('First', 'Third')
+
+
 Table Formatters
 ================
 
@@ -67,8 +70,8 @@
 Formatter`` is used. 
 
     >>> from zope.app.table import TableFormatter
-    >>> context = {}
-    >>> formatter = TableFormatter(config, context)
+    >>> request = None
+    >>> formatter = TableFormatter(config, request)
 
 We need some data to format::
 
@@ -80,7 +83,8 @@
 
     >>> items = [DataItem('a0', 'b0', 'c0'), DataItem('a1', 'b1', 'c1')]
 
-The simplest way to use one is to call the ``render`` method::
+The simplest way to use one is to tell the formatter to render the entire
+table::
 
     >>> print formatter.renderTable(items)
     <table>
@@ -141,10 +145,11 @@
 
 ``TableFormatter`` instances can be configured to sort their output. 
 
-    >>> config = MyTableConfiguration(columns)
+    >>> config = TableConfiguration(columns)
     >>> config.sort_on = 'Second'
     >>> config.sort_reverse = True
-    >>> formatter = TableFormatter(config, context)
+    >>> config.visible_columns = ('First', 'Third')
+    >>> formatter = TableFormatter(config, request)
     >>> print formatter.renderTable(items)
     <table>
     <tr><th>First</th><th>Third</th></tr>
@@ -154,10 +159,11 @@
 
 When batching sorted tables, the sorting is applied first, then the batching::
 
-    >>> config = MyTableConfiguration(columns)
+    >>> config = TableConfiguration(columns)
     >>> config.sort_on = 'Second'
     >>> config.sort_reverse = True
-    >>> formatter = TableFormatter(config, context, batch_start=1)
+    >>> config.visible_columns = ('First', 'Third')
+    >>> formatter = TableFormatter(config, request, batch_start=1)
     >>> print formatter.renderTable(items*2)
     <table>
     <tr><th>First</th><th>Third</th></tr>
@@ -181,8 +187,9 @@
     ...     GetItemColumn('Second', 'b'),
     ...     GetItemColumn('Third', 'c'),
     ...     ]
-    >>> config = MyTableConfiguration(columns)
-    >>> formatter = TableFormatter(config, context)
+    >>> config = TableConfiguration(columns)
+    >>> config.visible_columns = ('First', 'Third')
+    >>> formatter = TableFormatter(config, request)
     >>> print formatter.renderTable(items)
     <table>
     <tr><th><div style="width:200px">First</div></th><th>Third</th></tr>

Modified: Zope3/trunk/src/zope/app/table/__init__.py
===================================================================
--- Zope3/trunk/src/zope/app/table/__init__.py	2005-01-17 15:30:10 UTC (rev 28853)
+++ Zope3/trunk/src/zope/app/table/__init__.py	2005-01-17 16:08:08 UTC (rev 28854)
@@ -1,7 +1,11 @@
+import zope
+
+import interfaces
+
 class TableFormatter:
-    def __init__(self, config, context, batch_start=0):
+    def __init__(self, config, request, batch_start=0):
         self.config = config
-        self.context = context
+        self.request = request
         self.batch_start = batch_start
         self.columns_by_title = dict([(col.title,col) for col in config.columns])
 
@@ -30,7 +34,10 @@
         columns = self.getVisibleColumns()
         headers = []
         for column in columns:
-            headers.append(column.renderHeader(self))
+            contents = column.renderHeader(self)
+            if contents == None:
+                contents = ''
+            headers.append(str(contents))
 
         return headers
     
@@ -51,7 +58,10 @@
             cells = []
             sort_key = key_func(item, self)
             for column in columns:
-                cells.append(column.renderCell(item, self))
+                contents = column.renderCell(item, self)
+                if contents == None:
+                    contents = ''
+                cells.append(str(contents))
             rows.append((sort_key, cells))
 
         rows.sort()
@@ -62,3 +72,26 @@
         
         rows = rows[self.batch_start:batch_end]
         return rows
+
+class TableConfiguration:
+    zope.interface.implements(interfaces.ITableConfiguration)
+    sort_on = None
+    sort_reverse = False
+    batch_size = 0
+    def __init__(self, columns):
+        self.columns = columns
+        self._visible_columns = None
+
+    def visible_columns():
+        def fget(self):
+            if self._visible_columns == None:
+                return tuple([col.title for col in self.columns])
+            else:
+                return self._visible_columns
+
+        def fset(self, value):
+            self._visible_columns = visible_columns
+
+        return locals()
+
+    visible_columns = property(**visible_columns())

Added: Zope3/trunk/src/zope/app/table/column.py
===================================================================
--- Zope3/trunk/src/zope/app/table/column.py	2005-01-17 15:30:10 UTC (rev 28853)
+++ Zope3/trunk/src/zope/app/table/column.py	2005-01-17 16:08:08 UTC (rev 28854)
@@ -0,0 +1,46 @@
+import zope
+import interfaces
+
+class Unspecified: pass
+
+def getAttribute(item, attribute, default=Unspecified):
+        for attribute in attribute.split('.'):
+            if hasattr(item, attribute) \
+            or (hasattr(item, 'has_key') and item.has_key(attribute)) \
+            or default is Unspecified:
+                if hasattr(item, 'has_key'):
+                    item = item[attribute]
+                else:
+                    item = getattr(item, attribute)
+            else:
+                return default
+        return item
+
+class Column:
+    zope.interface.implements(interfaces.IColumn)
+
+    def __init__(self, title):
+        self.title = title
+
+    def renderHeader(self, formatter):
+        return self.title 
+
+    def renderCell(self, item, formatter):
+        return item
+
+    def getSortKey(self, item, formatter):
+        return None
+
+
+class AttributeColumn(Column):
+    zope.interface.implements(interfaces.IColumn)
+
+    def __init__(self, title, attribute, default=Unspecified):
+        self.title = title
+        self.attribute = attribute
+        self.default = default
+
+        self.sortable = False
+
+    def renderCell(self, item, formatter):
+        return getAttribute(item, self.attribute)

Modified: Zope3/trunk/src/zope/app/table/interfaces.py
===================================================================
--- Zope3/trunk/src/zope/app/table/interfaces.py	2005-01-17 15:30:10 UTC (rev 28853)
+++ Zope3/trunk/src/zope/app/table/interfaces.py	2005-01-17 16:08:08 UTC (rev 28854)
@@ -47,7 +47,7 @@
 
     title = zope.schema.TextLine(
         title=u'Title',
-        description=u'The title of the column, usually displayed in the table',
+        description=u'The title of the column, used in configuration diologs.',
         required=True,
         )
 



More information about the Zope3-Checkins mailing list