work in progress: handling of result sets
git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@1752 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
parent
e821d791b5
commit
4ac0a16080
4 changed files with 45 additions and 13 deletions
|
@ -35,6 +35,20 @@ then provide a listing of persons...
|
|||
>>> len(list(rset.rows))
|
||||
3
|
||||
|
||||
As we have not yet provided a schema for the result set the rows are
|
||||
empty.
|
||||
|
||||
>>> r1 = rset.rows.next()
|
||||
>>> list(r1.cells)
|
||||
[]
|
||||
|
||||
>>> from cybertools.composer.schema.schema import Schema
|
||||
>>> from cybertools.composer.schema.field import Field
|
||||
>>> rset.schema = Schema(Field(u'firstName'), Field(u'lastName'), Field(u'birthDate'))
|
||||
>>> r1 = rset.rows.next()
|
||||
>>> [c.text for c in r1.cells]
|
||||
['Smith', 'John', '1956-08-01']
|
||||
|
||||
For the browser presentation we can also use a browser view providing
|
||||
the result set with extended attributes:
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
<h2 tal:content="item/title">Something</h2><br />
|
||||
<table>
|
||||
<tr tal:repeat="cell item/cells">
|
||||
<td><span tal:content="cell/title"
|
||||
<td><span tal:content="cell/field/title"
|
||||
i18n:translate="" />Fieldname:</td>
|
||||
<td tal:content="cell/text">Value</td>
|
||||
</tr>
|
||||
|
@ -27,7 +27,7 @@
|
|||
<td valign="top"
|
||||
repeat="cell row/cells">
|
||||
<a href="#"
|
||||
tal:omit-tag="not: cell/url"
|
||||
tal:omit-tag="not:cell/url"
|
||||
tal:attributes="href cell/url;
|
||||
title cell/urlTitle">
|
||||
<span tal:replace="cell/text">Value</span>
|
||||
|
|
|
@ -46,7 +46,7 @@ class IRow(Interface):
|
|||
|
||||
cells = Attribute(u'Mapping of data elements addressed by field names.')
|
||||
|
||||
value = Attribute(u'The object that is the base of this row.')
|
||||
context = Attribute(u'The object that is the base of this row.')
|
||||
|
||||
resultSet = Attribute('The result set this row belongs to.')
|
||||
schema = Attribute(u'The schema of the result set this row belongs to')
|
||||
|
@ -56,6 +56,8 @@ class ICell(Interface):
|
|||
""" A single cell of a listing or table.
|
||||
"""
|
||||
|
||||
field = Attribute(u'The schema field the cell belongs to.')
|
||||
|
||||
text = Attribute(u'Text to be displayed.')
|
||||
value = Attribute(u'The real object, often identical to text.')
|
||||
token = Attribute(u'May be used to identify a cell within '
|
||||
|
@ -64,12 +66,12 @@ class ICell(Interface):
|
|||
# string:${view/url}/.target${row/uniqueId}
|
||||
urlTitle = Attribute(u'Optional title for this link.')
|
||||
|
||||
title = Attribute('The title of the schema field this cell belongs to.')
|
||||
row = Attribute('The row this cell belongs to.')
|
||||
|
||||
def sortKey():
|
||||
""" Returns a value (typically a string or a sequence) that will
|
||||
be used for comparing cells in order to sort rows.
|
||||
be used for comparing cells in order to sort rows on the
|
||||
corresponding column.
|
||||
"""
|
||||
|
||||
|
||||
|
|
|
@ -27,6 +27,8 @@ $Id$
|
|||
|
||||
from zope.component import adapts
|
||||
from zope.interface import implements
|
||||
|
||||
from cybertools.composer.schema.schema import Schema
|
||||
from cybertools.reporter.interfaces import IDataSource
|
||||
from cybertools.reporter.interfaces import IResultSet, IRow, ICell
|
||||
|
||||
|
@ -42,14 +44,25 @@ class Cell(object):
|
|||
|
||||
@property
|
||||
def text(self):
|
||||
return value
|
||||
return str(self.value)
|
||||
|
||||
@property
|
||||
def token(self):
|
||||
return value
|
||||
return self.value
|
||||
|
||||
def sortKey(self):
|
||||
return value
|
||||
return self.value
|
||||
|
||||
@property
|
||||
def url(self):
|
||||
view = self.row.resultSet.view
|
||||
if view is None:
|
||||
return ''
|
||||
return IAbsoluteURL(self.row, view.request, name=field.__name__)
|
||||
|
||||
@property
|
||||
def urlTitle(self):
|
||||
return ''
|
||||
|
||||
|
||||
class Row(object):
|
||||
|
@ -57,14 +70,17 @@ class Row(object):
|
|||
implements(IRow)
|
||||
|
||||
def __init__(self, context, resultSet):
|
||||
self.context = context # a single object (in this case)
|
||||
self.context = context
|
||||
self.resultSet = resultSet
|
||||
|
||||
@property
|
||||
def schema(self):
|
||||
return self.resultSet.schema
|
||||
|
||||
@property
|
||||
def cells(self):
|
||||
schema = self.resultSet.schema
|
||||
for f in schema.fields:
|
||||
yield Cell(f, getattr(self.context, f.__name__), self)
|
||||
for f in self.resultSet.schema.fields:
|
||||
yield Cell(f, getattr(self.context, f.name), self)
|
||||
|
||||
|
||||
class ResultSet(object):
|
||||
|
@ -72,11 +88,11 @@ class ResultSet(object):
|
|||
implements(IResultSet)
|
||||
adapts(IDataSource)
|
||||
|
||||
schema = None
|
||||
view = None
|
||||
|
||||
def __init__(self, context):
|
||||
self.context = context
|
||||
self.schema = Schema()
|
||||
|
||||
@property
|
||||
def rows(self):
|
||||
|
|
Loading…
Add table
Reference in a new issue