Skip to content

Commit

Permalink
commit test revision showing how to use Choice fields.
Browse files Browse the repository at this point in the history
  • Loading branch information
garyposter committed Jan 26, 2007
1 parent ce42518 commit fbc51f3
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 9 deletions.
2 changes: 0 additions & 2 deletions src/zc/table/fieldcolumn.py
Expand Up @@ -68,8 +68,6 @@ def getId(self, item, formatter):

class FieldColumn(BaseColumn):
"""Column that supports field/widget update
Note that fields are only bound if bind == True.
"""

__slots__ = ('title', 'name', 'field') # to emphasize that this should not
Expand Down
134 changes: 128 additions & 6 deletions src/zc/table/fieldcolumn.txt
Expand Up @@ -55,19 +55,24 @@ with names and email addresses:
... email = schema.TextLine(
... title=u'Email Address',
... constraint=re.compile('\w+@\w+([.]\w+)+$').match)
... salutation = schema.Choice(
... title=u'Salutation',
... values = ['Mr','Ms'],
... )

>>> class Contact:
... interface.implements(IContact)
... def __init__(self, id, name, email):
... def __init__(self, id, name, email, salutation):
... self.id = id
... self.name = name
... self.email = email
... self.salutation = salutation

>>> contacts = (
... Contact('1', 'Bob Smith', 'bob@zope.com'),
... Contact('2', 'Sally Baker', 'sally@zope.com'),
... Contact('3', 'Jethro Tul', 'jethro@zope.com'),
... Contact('4', 'Joe Walsh', 'joe@zope.com'),
... Contact('1', 'Bob Smith', 'bob@zope.com', 'Mr'),
... Contact('2', 'Sally Baker', 'sally@zope.com', 'Ms'),
... Contact('3', 'Jethro Tul', 'jethro@zope.com', 'Mr'),
... Contact('4', 'Joe Walsh', 'joe@zope.com', 'Mr'),
... )

We'll define columns that allow us to display and edit name and
Expand All @@ -77,8 +82,15 @@ email addresses.
>>> class ContactColumn(fieldcolumn.FieldColumn):
... def getId(self, item, formatter):
... return fieldcolumn.toSafe(item.id)
...
>>> class BindingContactColumn(ContactColumn):
... def getFieldContext(self, item, formatter):
... return item
...
>>> columns = (ContactColumn(IContact["name"]),
... ContactColumn(IContact["email"]))
... ContactColumn(IContact["email"]),
... BindingContactColumn(IContact["salutation"])
... )

Now, with this, we can create a table with input widgets. The columns don't
need a context other than the items themselves, so we ignore that part of the
Expand All @@ -100,6 +112,9 @@ table formatter instantiation:
<th>
Email Address
</th>
<th>
Salutation
</th>
</tr>
</thead>
<tbody>
Expand All @@ -112,6 +127,19 @@ table formatter instantiation:
<input class="textType" id="test.1.email" name="test.1.email"
size="20" type="text" value="bob@zope.com" />
</td>
<td>
<div>
<div class="value">
<select id="test.1.salutation" name="test.1.salutation"
size="1" >
<option selected="selected" value="Mr">Mr</option>
<option value="Ms">Ms</option>
</select>
</div>
<input name="test.1.salutation-empty-marker" type="hidden"
value="1" />
</div>
</td>
</tr>
<tr>
<td>
Expand All @@ -122,6 +150,19 @@ table formatter instantiation:
<input class="textType" id="test.2.email" name="test.2.email"
size="20" type="text" value="sally@zope.com" />
</td>
<td>
<div>
<div class="value">
<select id="test.2.salutation" name="test.2.salutation"
size="1" >
<option value="Mr">Mr</option>
<option selected="selected" value="Ms">Ms</option>
</select>
</div>
<input name="test.2.salutation-empty-marker" type="hidden"
value="1" />
</div>
</td>
</tr>
<tr>
<td>
Expand All @@ -132,6 +173,19 @@ table formatter instantiation:
<input class="textType" id="test.3.email" name="test.3.email"
size="20" type="text" value="jethro@zope.com" />
</td>
<td>
<div>
<div class="value">
<select id="test.3.salutation" name="test.3.salutation"
size="1" >
<option selected="selected" value="Mr">Mr</option>
<option value="Ms">Ms</option>
</select>
</div>
<input name="test.3.salutation-empty-marker" type="hidden"
value="1" />
</div>
</td>
</tr>
<tr>
<td>
Expand All @@ -142,6 +196,19 @@ table formatter instantiation:
<input class="textType" id="test.4.email" name="test.4.email"
size="20" type="text" value="joe@zope.com" />
</td>
<td>
<div>
<div class="value">
<select id="test.4.salutation" name="test.4.salutation"
size="1" >
<option selected="selected" value="Mr">Mr</option>
<option value="Ms">Ms</option>
</select>
</div>
<input name="test.4.salutation-empty-marker" type="hidden"
value="1" />
</div>
</td>
</tr>
</tbody>
</table>
Expand All @@ -162,6 +229,9 @@ If the request has input for a value, then this will override item data:
<th>
Email Address
</th>
<th>
Salutation
</th>
</tr>
</thead>
<tbody>
Expand All @@ -174,6 +244,19 @@ If the request has input for a value, then this will override item data:
<input class="textType" id="test.1.email" name="test.1.email"
size="20" type="text" value="bob@zope.com" />
</td>
<td>
<div>
<div class="value">
<select id="test.1.salutation" name="test.1.salutation"
size="1" >
<option selected="selected" value="Mr">Mr</option>
<option value="Ms">Ms</option>
</select>
</div>
<input name="test.1.salutation-empty-marker" type="hidden"
value="1" />
</div>
</td>
</tr>
<tr>
<td>
Expand All @@ -184,6 +267,19 @@ If the request has input for a value, then this will override item data:
<input class="textType" id="test.2.email" name="test.2.email"
size="20" type="text" value="sally@zope.com" />
</td>
<td>
<div>
<div class="value">
<select id="test.2.salutation" name="test.2.salutation"
size="1" >
<option value="Mr">Mr</option>
<option selected="selected" value="Ms">Ms</option>
</select>
</div>
<input name="test.2.salutation-empty-marker" type="hidden"
value="1" />
</div>
</td>
</tr>
<tr>
<td>
Expand All @@ -194,6 +290,19 @@ If the request has input for a value, then this will override item data:
<input class="textType" id="test.3.email" name="test.3.email"
size="20" type="text" value="jethro@zope.com" />
</td>
<td>
<div>
<div class="value">
<select id="test.3.salutation" name="test.3.salutation"
size="1" >
<option selected="selected" value="Mr">Mr</option>
<option value="Ms">Ms</option>
</select>
</div>
<input name="test.3.salutation-empty-marker" type="hidden"
value="1" />
</div>
</td>
</tr>
<tr>
<td>
Expand All @@ -204,6 +313,19 @@ If the request has input for a value, then this will override item data:
<input class="textType" id="test.4.email" name="test.4.email"
size="20" type="text" value="walsh@zope.com" />
</td>
<td>
<div>
<div class="value">
<select id="test.4.salutation" name="test.4.salutation"
size="1" >
<option selected="selected" value="Mr">Mr</option>
<option value="Ms">Ms</option>
</select>
</div>
<input name="test.4.salutation-empty-marker" type="hidden"
value="1" />
</div>
</td>
</tr>
</tbody>
</table>
Expand Down
28 changes: 27 additions & 1 deletion src/zc/table/tests.py
Expand Up @@ -37,6 +37,32 @@ def columnSetUp(test):
),
zope.app.form.interfaces.IInputWidget)

def fieldColumnSetUp(test):
columnSetUp(test)
component.provideAdapter(
zope.app.form.browser.ChoiceDisplayWidget,
(zope.schema.interfaces.IChoice,
zope.publisher.interfaces.browser.IBrowserRequest),
zope.app.form.interfaces.IDisplayWidget)
component.provideAdapter(
zope.app.form.browser.ChoiceInputWidget,
(zope.schema.interfaces.IChoice,
zope.publisher.interfaces.browser.IBrowserRequest),
zope.app.form.interfaces.IInputWidget)
component.provideAdapter(
zope.app.form.browser.DropdownWidget,
(zope.schema.interfaces.IChoice,
zope.schema.interfaces.IVocabularyTokenized,
zope.publisher.interfaces.browser.IBrowserRequest),
zope.app.form.interfaces.IInputWidget)
component.provideAdapter(
zope.app.form.browser.ChoiceDisplayWidget,
(zope.schema.interfaces.IChoice,
zope.schema.interfaces.IVocabularyTokenized,
zope.publisher.interfaces.browser.IBrowserRequest),
zope.app.form.interfaces.IDisplayWidget)


def test_suite():
from zope.testing import doctest
return unittest.TestSuite((
Expand All @@ -50,7 +76,7 @@ def test_suite():
),
doctest.DocFileSuite(
'fieldcolumn.txt',
setUp = columnSetUp, tearDown=placelesssetup.tearDown,
setUp = fieldColumnSetUp, tearDown=placelesssetup.tearDown,
optionflags=doctest.NORMALIZE_WHITESPACE+doctest.ELLIPSIS,
),
))
Expand Down

0 comments on commit fbc51f3

Please sign in to comment.