[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/form/browser/ Moved
HTML generation of SequenceWidgets from Python to a page template.
Albertas Agejevas
alga at pov.lt
Fri Mar 3 17:59:51 EST 2006
Log message for revision 65782:
Moved HTML generation of SequenceWidgets from Python to a page template.
Changed:
A Zope3/trunk/src/zope/app/form/browser/sequencewidget.pt
U Zope3/trunk/src/zope/app/form/browser/sequencewidget.py
U Zope3/trunk/src/zope/app/form/browser/tests/test_sequencewidget.py
-=-
Added: Zope3/trunk/src/zope/app/form/browser/sequencewidget.pt
===================================================================
--- Zope3/trunk/src/zope/app/form/browser/sequencewidget.pt 2006-03-03 21:51:58 UTC (rev 65781)
+++ Zope3/trunk/src/zope/app/form/browser/sequencewidget.pt 2006-03-03 22:59:51 UTC (rev 65782)
@@ -0,0 +1,28 @@
+<table border="0" class="sequencewidget">
+ <tr tal:repeat="widget view/widgets">
+ <td>
+ <input class="editcheck" type="checkbox"
+ tal:attributes="
+ name string:${view/name}.remove_${repeat/widget/index}"
+ tal:condition="view/need_delete" />
+ </td>
+ <td>
+ <span tal:define="error widget/error"
+ tal:replace="structure error" tal:condition="error" />
+ <input tal:replace="structure widget" />
+ </td>
+ </tr>
+ <tr>
+ <td colspan="2">
+ <input type="submit" value="Remove selected items"
+ tal:condition="view/need_delete"
+ tal:attributes="name string:${view/name}.remove"
+ i18n:attributes="value remove-selected-items" />
+ <input type="submit" value="Add"
+ tal:condition="view/need_add"
+ tal:attributes="name string:${view/name}.add"
+ i18n:attributes="value add-button" />
+ </td>
+ </tr>
+</table>
+<input tal:replace="structure view/marker" />
Property changes on: Zope3/trunk/src/zope/app/form/browser/sequencewidget.pt
___________________________________________________________________
Name: svn:keywords
+ Id
Modified: Zope3/trunk/src/zope/app/form/browser/sequencewidget.py
===================================================================
--- Zope3/trunk/src/zope/app/form/browser/sequencewidget.py 2006-03-03 21:51:58 UTC (rev 65781)
+++ Zope3/trunk/src/zope/app/form/browser/sequencewidget.py 2006-03-03 22:59:51 UTC (rev 65782)
@@ -26,6 +26,7 @@
from zope.app.form import InputWidget
from zope.app.form.browser.widget import BrowserWidget
from zope.app.form.browser.widget import DisplayWidget, renderElement
+from zope.app.pagetemplate.viewpagetemplatefile import ViewPageTemplateFile
from zope.app.i18n import ZopeMessageFactory as _
@@ -38,6 +39,8 @@
implements(IInputWidget)
+ template = ViewPageTemplateFile('sequencewidget.pt')
+
_type = tuple
def __init__(self, context, field, request, subwidget=None):
@@ -50,56 +53,28 @@
def __call__(self):
"""Render the widget"""
- assert self.context.value_type is not None
+ self._update()
+ return self.template()
- render = []
-
- # length of sequence info
+ def _update(self):
+ """Set various attributes for the template"""
sequence = self._getRenderedValue()
num_items = len(sequence)
- min_length = self.context.min_length
- max_length = self.context.max_length
+ self.need_add = (not self.context.max_length
+ or num_items < self.context.max_length)
+ self.need_delete = num_items and num_items > self.context.min_length
+ self.marker = self._getPresenceMarker(num_items)
- # generate each widget from items in the sequence - adding a
- # "remove" button for each one
- for i in range(num_items):
- value = sequence[i]
- render.append('<tr><td>')
- if num_items > min_length:
- render.append(
- '<input class="editcheck" type="checkbox" '
- 'name="%s.remove_%d" />\n' % (self.name, i)
- )
+ def widgets(self):
+ """Return a list of widgets to display"""
+ sequence = self._getRenderedValue()
+ result = []
+ for i, value in enumerate(sequence):
widget = self._getWidget(i)
widget.setRenderedValue(value)
- error = widget.error()
- if error:
- render.append(error)
- render.append('\n')
- render.append(widget() + '</td></tr>\n')
+ result.append(widget)
+ return result
- # possibly generate the "remove" and "add" buttons
- buttons = ''
- if render and num_items > min_length:
- button_label = _('remove-selected-items', "Remove selected items")
- button_label = translate(button_label, context=self.request,
- default=button_label)
- buttons += ('<input type="submit" value="%s" name="%s.remove"/>'
- % (button_label, self.name))
- if max_length is None or num_items < max_length:
- field = self.context.value_type
- button_label = _('Add %s')
- button_label = translate(button_label, context=self.request,
- default=button_label)
- button_label = button_label % (field.title or field.__name__)
- buttons += '<input type="submit" name="%s.add" value="%s" />\n' % (
- self.name, button_label)
- if buttons:
- render.append('<tr><td>%s</td></tr>\n' % buttons)
-
- return ('<table border="0">\n%s</table>\n%s'
- % (''.join(render), self._getPresenceMarker(num_items)))
-
def _getWidget(self, i):
"""Return a widget for the i-th number of the sequence.
Modified: Zope3/trunk/src/zope/app/form/browser/tests/test_sequencewidget.py
===================================================================
--- Zope3/trunk/src/zope/app/form/browser/tests/test_sequencewidget.py 2006-03-03 21:51:58 UTC (rev 65781)
+++ Zope3/trunk/src/zope/app/form/browser/tests/test_sequencewidget.py 2006-03-03 22:59:51 UTC (rev 65782)
@@ -58,6 +58,10 @@
self._widget = self._WidgetFactory(
self.field, self.field.value_type, self.request)
+ def setUp(self):
+ setup.placefulSetUp()
+ self.setUpContent()
+
def _FieldFactory(self, **kw):
kw.update({
'__name__': u'foo',
@@ -301,10 +305,17 @@
>>> print widget()
<BLANKLINE>
...
- <tr><td><input class="editcheck" type="checkbox"
- name="field.foo.remove_0" />
- <input class="textType" id="field.foo.0.bar" name="field.foo.0.bar"
- size="20" type="text" value="" /></td></tr>
+ <tr>
+ <td>
+ <input class="editcheck" type="checkbox"
+ name="field.foo.remove_0" />
+ </td>
+ <td>
+ <input class="textType" id="field.foo.0.bar"
+ name="field.foo.0.bar"
+ size="20" type="text" value="" />
+ </td>
+ </tr>
...
However, if we call getInputValue or hasValidInput, the
@@ -316,11 +327,18 @@
>>> print widget()
<BLANKLINE>
...
- <tr><td><input class="editcheck" type="checkbox"
- name="field.foo.remove_0" />
- <span class="error">Required input is missing.</span>
- <input class="textType" id="field.foo.0.bar" name="field.foo.0.bar"
- size="20" type="text" value="" /></td></tr>
+ <tr>
+ <td>
+ <input class="editcheck" type="checkbox"
+ name="field.foo.remove_0" />
+ </td>
+ <td>
+ <span class="error">Required input is missing.</span>
+ <input class="textType" id="field.foo.0.bar"
+ name="field.foo.0.bar"
+ size="20" type="text" value="" />
+ </td>
+ </tr>
...
"""
@@ -390,14 +408,14 @@
def setUp(test):
- setup.placelessSetUp()
+ setup.placefulSetUp()
ztapi.browserViewProviding(ITextLine, TextWidget, IInputWidget)
ztapi.browserViewProviding(IWidgetInputError, WidgetInputErrorView,
IWidgetInputErrorView)
def tearDown(test):
- setup.placelessTearDown()
+ setup.placefulTearDown()
def test_suite():
More information about the Zope3-Checkins
mailing list