[Checkins] SVN: Products.CMFDefault/trunk/Products/CMFDefault/ - added special input widget for object IDs

Yvo Schubbe y.2008 at wcm-solutions.de
Sat Sep 20 07:54:36 EDT 2008


Log message for revision 91284:
  - added special input widget for object IDs

Changed:
  U   Products.CMFDefault/trunk/Products/CMFDefault/CHANGES.txt
  U   Products.CMFDefault/trunk/Products/CMFDefault/formlib/widgets.py
  U   Products.CMFDefault/trunk/Products/CMFDefault/formlib/widgets.txt

-=-
Modified: Products.CMFDefault/trunk/Products/CMFDefault/CHANGES.txt
===================================================================
--- Products.CMFDefault/trunk/Products/CMFDefault/CHANGES.txt	2008-09-19 21:50:51 UTC (rev 91283)
+++ Products.CMFDefault/trunk/Products/CMFDefault/CHANGES.txt	2008-09-20 11:54:34 UTC (rev 91284)
@@ -4,6 +4,8 @@
 2.2.0 (unreleased)
 ------------------
 
+- formlib widgets: Added special input widget for object IDs.
+
 - main_template: Display action icons, thereby replacing the separate
   CMFActionIcons product.
 

Modified: Products.CMFDefault/trunk/Products/CMFDefault/formlib/widgets.py
===================================================================
--- Products.CMFDefault/trunk/Products/CMFDefault/formlib/widgets.py	2008-09-19 21:50:51 UTC (rev 91283)
+++ Products.CMFDefault/trunk/Products/CMFDefault/formlib/widgets.py	2008-09-20 11:54:34 UTC (rev 91284)
@@ -16,6 +16,7 @@
 """
 
 from zope.app.form import InputWidget
+from zope.app.form.browser import ASCIIWidget
 from zope.app.form.browser import BrowserWidget
 from zope.app.form.browser import FileWidget
 from zope.app.form.browser import MultiSelectSetWidget
@@ -132,6 +133,21 @@
 
 # special widgets
 
+class IDInputWidget(ASCIIWidget):
+
+    def getInputValue(self):
+        value = super(IDInputWidget, self).getInputValue()
+        if value:
+            context = getattr(self.context.context, 'context',
+                              self.context.context)
+            if not context.checkIdAvailable(value):
+                err_msg = _(u'Please choose another ID.')
+                self._error = WidgetInputError(self.context.__name__,
+                                               self.label, err_msg)
+                raise self._error
+        return value
+
+
 class SubjectInputWidget(InputWidget, BrowserWidget):
 
     implementsOnly(IInputWidget)

Modified: Products.CMFDefault/trunk/Products/CMFDefault/formlib/widgets.txt
===================================================================
--- Products.CMFDefault/trunk/Products/CMFDefault/formlib/widgets.txt	2008-09-19 21:50:51 UTC (rev 91283)
+++ Products.CMFDefault/trunk/Products/CMFDefault/formlib/widgets.txt	2008-09-20 11:54:34 UTC (rev 91284)
@@ -63,6 +63,79 @@
       None
 
 
+  Testing IDInputWidget:
+
+    Set one up::
+
+      >>> class DummyFolder:
+      ...     def checkIdAvailable(self, value):
+      ...         if value == 'INVALID': return False
+      ...         return True
+      >>> class DummyView: pass
+      >>> view = DummyView()
+      >>> view.context = DummyFolder()
+
+      >>> request = DummyRequest()
+      >>> request.form['PREFIX.NAME'] = u'FOO'
+
+      >>> from Products.CMFDefault.formlib.widgets import IDInputWidget
+      >>> from zope.schema import ASCIILine
+      >>> ascii_field = ASCIILine(__name__='NAME',
+      ...                         title=u'FIELD TITLE').bind(view)
+
+      >>> widget = IDInputWidget(ascii_field, request)
+
+    IWidget::
+
+      >>> widget.name
+      'field.NAME'
+      >>> widget.label
+      u'FIELD TITLE'
+      >>> widget.hint
+      u''
+      >>> widget.visible
+      True
+      >>> widget.setRenderedValue(('BAZ',))
+      >>> widget.setRenderedValue(widget._data_marker)
+      >>> widget.setPrefix('PREFIX')
+      >>> widget.name
+      'PREFIX.NAME'
+
+    IInputWidget::
+
+      >>> widget.required
+      True
+      >>> widget.getInputValue()
+      'FOO'
+      >>> #widget.applyChanges(content)
+      >>> widget.hasInput()
+      True
+      >>> widget.hasValidInput()
+      True
+
+    IBrowserWidget::
+
+      >>> print widget()
+      <input class="textType" id="PREFIX.NAME" name="PREFIX.NAME" size="20"
+      type="text" value="FOO" />
+
+    Missing input::
+
+      >>> request.form['PREFIX.NAME'] = u''
+      >>> print widget.getInputValue()
+      Traceback (most recent call last):
+      ...
+      WidgetInputError: ('NAME', u'FIELD TITLE', )
+
+    Invalid input::
+
+      >>> request.form['PREFIX.NAME'] = u'INVALID'
+      >>> print widget.getInputValue()
+      Traceback (most recent call last):
+      ...
+      WidgetInputError: ('NAME', u'FIELD TITLE', u'Please choose another ID.')
+
+
   Testing SubjectInputWidget:
 
     Set one up::
@@ -71,7 +144,6 @@
       >>> from Products.CMFCore.interfaces import IMetadataTool
       >>> class DummyTool:
       ...     def listAllowedSubjects(self, context): return ('SPAM', 'EGGS')
-      >>> class DummyView: pass
       >>> view = DummyView()
       >>> view.portal_metadata = DummyTool()
       >>> getSiteManager().registerUtility(view.portal_metadata, IMetadataTool)



More information about the Checkins mailing list