[Zope3-dev] Re: widgets for sources not yet complete?

Sam Stainsby sam at stainsby.id.au
Fri Apr 21 05:25:38 EDT 2006


On Thu, 20 Apr 2006 09:55:39 -0400, Gary Poster wrote: 
> Go for it.

The following code works at least for for SourceDropdownWidget and
SourceOrderedMultiSelectWidget. I don't have contributor
privileges so I can't check it in. Happy to do so if someone wants to give
my the access, otherwise someone else can do it.

Cheers,
Sam.
----

# Input widgets for IIterableSource.

# These widgets reuse the old-style vocabulary widgets via the class
# IterableSourceVocabulary that adapts a source (and its ITerms object) 
# into a vocabulary. When/if vocabularies go away, these classes
# should be updated into full implementations.


class IterableSourceVocabulary(object):
    """Adapts an iterable source into a vocabulary.
    
    There must be an ITerms implementation registered to obtain
    the terms."""
    
    zope.interface.implements(IVocabularyTokenized)
    
    def __init__(self, source, request):
        self.source = source
        self.terms = zapi.getMultiAdapter(
            (source, request), zope.app.form.browser.interfaces.ITerms)
    
    def getTerm(self, value):
        return self.terms.getTerm(value)
        
    def getTermByToken(self, token):
        value = self.terms.getValue(token)
        return self.getTerm(value)
        
    def __iter__(self):
        return imap(
            lambda value: self.getTerm(value), self.source.__iter__())
            
    def __len__(self):
        return self.source.__len__()
        
    def __contains__(self, value):
        return self.source.__contains__(value)


class SourceSelectWidget(SelectWidget):
    """Provide a selection list for the item."""
    
    def __init__(self, field, source, request):
        super(SourceSelectWidget, self).__init__(
            field, IterableSourceVocabulary(source, request), request)

class SourceDropdownWidget(SourceSelectWidget):
    """Variation of the SourceSelectWidget that uses a drop-down list."""
    
    size = 1

class SourceRadioWidget(RadioWidget):
    """Radio widget for single item choices."""
    
    def __init__(self, field, source, request):
        super(SourceRadioWidget, self).__init__(
            field, IterableSourceVocabulary(source, request), request)

class SourceMultiSelectSetWidget(MultiSelectWidget):
    """Provide a selection list for the set to be selected."""
    
    def __init__(self, field, source, request):
        super(MultiSelectSetWidget, self).__init__(
            field, IterableSourceVocabulary(source, request), request)
    
class SourceOrderedMultiSelectWidget(OrderedMultiSelectWidget):
    """A multi-selection widget with ordering support."""
    
    def __init__(self, field, source, request):
        super(SourceOrderedMultiSelectWidget, self).__init__(
            field, IterableSourceVocabulary(source, request), request)
            
class SourceMultiCheckBoxWidget(MultiCheckBoxWidget):
    """Provide a list of checkboxes that provide the choice for the list."""
    
    def __init__(self, field, source, request):
        super(SourceMultiCheckBoxWidget, self).__init__(
            field, IterableSourceVocabulary(source, request), request)




More information about the Zope3-dev mailing list