[Zope3-checkins] CVS: Zope3/src/zope/schema - vocabulary.py:1.15

Fred L. Drake, Jr. fred@zope.com
Fri, 13 Jun 2003 11:30:46 -0400


Update of /cvs-repository/Zope3/src/zope/schema
In directory cvs.zope.org:/tmp/cvs-serv4072

Modified Files:
	vocabulary.py 
Log Message:
- organize imports
- define a new descriptor that copies value for a container, so that
  getting the default value for a multi-value field does return a
  mutable object that can allow the default to be modified
- don't try to validate a value against a vocabulary if the value is
  empty


=== Zope3/src/zope/schema/vocabulary.py 1.14 => 1.15 ===
--- Zope3/src/zope/schema/vocabulary.py:1.14	Thu Jun  5 12:54:04 2003
+++ Zope3/src/zope/schema/vocabulary.py	Fri Jun 13 11:30:45 2003
@@ -14,8 +14,13 @@
 
 """Vocabulary support for schema."""
 
-from zope.schema import Field
+import copy
+
+from zope.interface.declarations import directlyProvides, implements
 from zope.schema import errornames
+from zope.schema import Field
+from zope.schema import MinMaxLen
+from zope.schema._bootstrapfields import ValidatedProperty
 from zope.schema.interfaces import ValidationError
 from zope.schema.interfaces import IVocabularyRegistry
 from zope.schema.interfaces import IVocabularyField
@@ -24,8 +29,6 @@
 from zope.schema.interfaces import IVocabularyUniqueListField
 from zope.schema.interfaces import IVocabulary, IVocabularyTokenized
 from zope.schema.interfaces import ITokenizedTerm
-from zope.interface.declarations import directlyProvides, implements
-from zope.schema import MinMaxLen
 
 try:
     basestring  # new in Python 2.3
@@ -33,6 +36,19 @@
     from types import StringTypes as basestring
 
 
+class ContainerValidatedProperty(ValidatedProperty):
+
+    def __get__(self, inst, type=None):
+        name, check = self._info
+        try:
+            value = inst.__dict__[name]
+        except KeyError:
+            raise AttributeError, name
+        if value is not None:
+            value = copy.copy(value)
+        return value
+
+
 class VocabularyField(Field):
     """Field that adds support for use of an external vocabulary.
 
@@ -80,6 +96,9 @@
     This class cannot be used directly; a subclass must be used to
     specify concrete behavior.
     """
+
+    default = ContainerValidatedProperty("default")
+
     def __init__(self, **kw):
         if self.__class__ is VocabularyMultiField:
             raise NotImplementedError(
@@ -88,6 +107,8 @@
 
     def _validate(self, value):
         vocab = self.vocabulary
+        if not value:
+            return
         if vocab is None:
             raise ValueError("can't validate value without vocabulary")
         for v in value: