[Checkins] SVN: z3c.schema/trunk/src/z3c/schema/optchoice/
Implemented a field that allows either to select a choice from a
Stephan Richter
srichter at cosmos.phy.tufts.edu
Thu Dec 28 11:20:08 EST 2006
Log message for revision 71659:
Implemented a field that allows either to select a choice from a
vocabulary or enter a custom value.
The field itself is not that interesting, but the widget will be added
soon, which will basically be a combo box with the possibility to enter
a custom value.
Changed:
A z3c.schema/trunk/src/z3c/schema/optchoice/
A z3c.schema/trunk/src/z3c/schema/optchoice/README.txt
A z3c.schema/trunk/src/z3c/schema/optchoice/__init__.py
A z3c.schema/trunk/src/z3c/schema/optchoice/field.py
A z3c.schema/trunk/src/z3c/schema/optchoice/interfaces.py
A z3c.schema/trunk/src/z3c/schema/optchoice/tests.py
-=-
Added: z3c.schema/trunk/src/z3c/schema/optchoice/README.txt
===================================================================
--- z3c.schema/trunk/src/z3c/schema/optchoice/README.txt 2006-12-28 13:43:44 UTC (rev 71658)
+++ z3c.schema/trunk/src/z3c/schema/optchoice/README.txt 2006-12-28 16:20:08 UTC (rev 71659)
@@ -0,0 +1,60 @@
+================
+Optional Choices
+================
+
+The optional choice field is desiged to offer a set of default choices or
+allow, optionally, to enter a custom value. The custom value has to conform to
+a specified field.
+
+Here is an example of creating such a field:
+
+ >>> import zope.schema
+ >>> from z3c.schema.optchoice import OptionalChoice
+
+ >>> optchoice = OptionalChoice(
+ ... title=u'Occupation',
+ ... values=(u'Programmer', u'Designer', u'Project Manager'),
+ ... value_type=zope.schema.TextLine())
+
+Note that the value type *must* be a field:
+
+ >>> OptionalChoice(
+ ... title=u'Occupation',
+ ... values=(u'Programmer', u'Designer', u'Project Manager'),
+ ... value_type=object())
+ Traceback (most recent call last):
+ ValueError: 'value_type' must be field instance.
+
+Let's now ensure that we can validate not only choices, but also custom
+values:
+
+ >>> optchoice.validate(u'Programmer')
+ >>> optchoice.validate(u'Project Manager')
+ >>> optchoice.validate(u'Scripter')
+
+ >>> optchoice.validate(u'Scripter\nHTML\n')
+ Traceback (most recent call last):
+ ...
+ ConstraintNotSatisfied: Scripter
+ HTML
+
+Let's now ensure that we can convert values from unicode to a real value as
+well. To demonstrate this feature, we have to create a more restrictive
+optional choice field:
+
+ >>> optchoice = OptionalChoice(
+ ... title=u'Age',
+ ... values=(10, 20, 30, 40, 50),
+ ... value_type=zope.schema.Int(min=0))
+
+ >>> optchoice.fromUnicode(u'10')
+ 10
+ >>> optchoice.fromUnicode(u'40')
+ 40
+ >>> optchoice.fromUnicode(u'45')
+ 45
+
+ >>> optchoice.fromUnicode(u'-10')
+ Traceback (most recent call last):
+ ...
+ TooSmall: (-10, 0)
Property changes on: z3c.schema/trunk/src/z3c/schema/optchoice/README.txt
___________________________________________________________________
Name: svn:eol-style
+ native
Added: z3c.schema/trunk/src/z3c/schema/optchoice/__init__.py
===================================================================
--- z3c.schema/trunk/src/z3c/schema/optchoice/__init__.py 2006-12-28 13:43:44 UTC (rev 71658)
+++ z3c.schema/trunk/src/z3c/schema/optchoice/__init__.py 2006-12-28 16:20:08 UTC (rev 71659)
@@ -0,0 +1,19 @@
+##############################################################################
+#
+# Copyright (c) 2006 Zope Foundation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""
+$Id$
+"""
+
+from z3c.schema.optchoice.interfaces import IOptionalChoice
+from z3c.schema.optchoice.field import OptionalChoice
Property changes on: z3c.schema/trunk/src/z3c/schema/optchoice/__init__.py
___________________________________________________________________
Name: svn:keywords
+ Id
Added: z3c.schema/trunk/src/z3c/schema/optchoice/field.py
===================================================================
--- z3c.schema/trunk/src/z3c/schema/optchoice/field.py 2006-12-28 13:43:44 UTC (rev 71658)
+++ z3c.schema/trunk/src/z3c/schema/optchoice/field.py 2006-12-28 16:20:08 UTC (rev 71659)
@@ -0,0 +1,40 @@
+###############################################################################
+#
+# Copyright 2006 by refline (Schweiz) AG, CH-5630 Muri
+#
+###############################################################################
+"""Special Fields
+
+$Id$
+"""
+__docformat__ = "reStructuredText"
+import zope.interface
+import zope.schema
+from zope.schema.interfaces import IField, ValidationError
+
+from z3c.schema.optchoice import interfaces
+
+
+class OptionalChoice(zope.schema.Choice):
+ """Optional Choice field."""
+
+ zope.interface.implements(interfaces.IOptionalChoice)
+
+ def __init__(self, value_type, **kw):
+ super(OptionalChoice, self).__init__(**kw)
+ # whine if value_type is not a field
+ if value_type is not None and not IField.providedBy(value_type):
+ raise ValueError("'value_type' must be field instance.")
+ self.value_type = value_type
+
+ def _validate(self, value):
+ try:
+ super(OptionalChoice, self)._validate(value)
+ except ValidationError:
+ self.value_type._validate(value)
+
+ def fromUnicode(self, value):
+ try:
+ return super(OptionalChoice, self).fromUnicode(value)
+ except ValidationError:
+ return self.value_type.fromUnicode(value)
Property changes on: z3c.schema/trunk/src/z3c/schema/optchoice/field.py
___________________________________________________________________
Name: svn:keywords
+ Id
Added: z3c.schema/trunk/src/z3c/schema/optchoice/interfaces.py
===================================================================
--- z3c.schema/trunk/src/z3c/schema/optchoice/interfaces.py 2006-12-28 13:43:44 UTC (rev 71658)
+++ z3c.schema/trunk/src/z3c/schema/optchoice/interfaces.py 2006-12-28 16:20:08 UTC (rev 71659)
@@ -0,0 +1,50 @@
+##############################################################################
+#
+# Copyright (c) 2006 Zope Foundation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""
+$Id$
+"""
+__docformat__ = "reStructuredText"
+
+import zope.schema
+import zope.schema.interfaces
+
+from z3c.i18n import MessageFactory as _
+
+
+class IOptionalChoice(zope.schema.interfaces.IChoice,
+ zope.schema.interfaces.IFromUnicode):
+ """Optional Choice
+
+ This field can either represent a choice or a textline value.
+
+ Validation proceeds as follows:
+
+ 1. Check whether the value is one of the choices. If so, return
+ successfully.
+
+ 2. If no choice match was found, validate the value against the
+ ``value_type`` field.
+
+ The conversion from unicode values proceeds in a similar fashion:
+
+ 1. Try to match the unicode value to a token in the vocabulary.
+
+ 2. If no match was made, call the unicode conversion method of the
+ ``value_type`` field.
+ """
+
+ value_type = zope.schema.Field(
+ title = _("Value Type"),
+ description = _(u"The freely entered values must be of this type."),
+ required=True)
Property changes on: z3c.schema/trunk/src/z3c/schema/optchoice/interfaces.py
___________________________________________________________________
Name: svn:keywords
+ Id
Added: z3c.schema/trunk/src/z3c/schema/optchoice/tests.py
===================================================================
--- z3c.schema/trunk/src/z3c/schema/optchoice/tests.py 2006-12-28 13:43:44 UTC (rev 71658)
+++ z3c.schema/trunk/src/z3c/schema/optchoice/tests.py 2006-12-28 16:20:08 UTC (rev 71659)
@@ -0,0 +1,24 @@
+###############################################################################
+#
+# Copyright 2006 by refline (Schweiz) AG, CH-5630 Muri
+#
+###############################################################################
+"""Refline Recruiter Tests
+
+$Id$
+"""
+__docformat__ = 'restructuredtext'
+
+import unittest
+from zope.testing import doctest
+from zope.testing.doctestunit import DocFileSuite
+
+
+def test_suite():
+ return unittest.TestSuite((
+ DocFileSuite('README.txt',
+ optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS,),
+ ))
+
+if __name__ == '__main__':
+ unittest.main(defaultTest='test_suite')
Property changes on: z3c.schema/trunk/src/z3c/schema/optchoice/tests.py
___________________________________________________________________
Name: svn:keywords
+ Id
More information about the Checkins
mailing list