Back to index

plone3  3.1.7
fieldsets.py
Go to the documentation of this file.
00001 from zope.schema import getFieldsInOrder
00002 from zope.formlib.form import FormField
00003 from zope.formlib.form import FormFields
00004 from zope.formlib.form import _initkw
00005 from zope.interface import implements
00006 from zope.interface.interface import InterfaceClass
00007 from zope.schema.interfaces import IField
00008 
00009 from interfaces import IFormFieldsets
00010 
00011 class FormFieldsets(FormFields):
00012     """
00013     Let's make sure that this implementation actually fulfills the API.
00014 
00015       >>> from zope.interface.verify import verifyClass
00016       >>> verifyClass(IFormFieldsets, FormFieldsets)
00017       True
00018       """
00019 
00020     implements(IFormFieldsets)
00021 
00022     id = ''
00023     label = None
00024     description = None
00025     fieldsets = ()
00026 
00027     def __init__(self, *args, **kw):
00028         # Most of this code is copied from zope.formlib.form.Formfields
00029         # licensed under the ZPL 2.1
00030         keep_readonly, omit_readonly, defaults = _initkw(**kw)
00031 
00032         fields = []
00033         fieldsets = ()
00034         for arg in args:
00035             if isinstance(arg, InterfaceClass):
00036                 for name, field in getFieldsInOrder(arg):
00037                     fields.append((name, field))
00038             elif IField.providedBy(arg):
00039                 name = arg.__name__
00040                 if not name:
00041                     raise ValueError("Field has no name")
00042                 fields.append((name, arg))
00043             elif isinstance(arg, FormFieldsets):
00044                 fieldsets = fieldsets + (arg, )
00045                 for form_field in arg:
00046                     fields.append((form_field.__name__, form_field))
00047             elif isinstance(arg, FormFields):
00048                 for form_field in arg:
00049                     fields.append((form_field.__name__, form_field))
00050             elif isinstance(arg, FormField):
00051                 fields.append((arg.__name__, arg))
00052             else:
00053                 raise TypeError("Unrecognized argument type", arg)
00054 
00055         self.fieldsets = fieldsets
00056 
00057         seq = []
00058         byname = {}
00059         for name, field in fields:
00060             if isinstance(field, FormField):
00061                 form_field = field
00062             else:
00063                 if field.readonly:
00064                     if omit_readonly and (name not in keep_readonly):
00065                         continue
00066                 form_field = FormField(field, **defaults)
00067                 name = form_field.__name__
00068 
00069             if name in byname:
00070                 raise ValueError("Duplicate name", name)
00071             seq.append(form_field)
00072             byname[name] = form_field
00073 
00074         self.__FormFields_seq__ = seq
00075         self.__FormFields_byname__ = byname
00076 
00077     def __add__(self, other):
00078         if not isinstance(other, FormFieldsets) and \
00079            not isinstance(other, FormFields):
00080             return NotImplemented
00081         return self.__class__(self, other)