Created
April 9, 2013 12:42
-
-
Save jaroel/5345403 to your computer and use it in GitHub Desktop.
Parameterised dynamic vocabulary.
Shows unique values for catalog index.
Pass in the index name in the schema.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Definition: | |
=========== | |
from zope.schema.interfaces import IContextSourceBinder, IBaseVocabulary | |
class CatalogIndexValuesSource(object): | |
grok.implements(IContextSourceBinder, IBaseVocabulary) | |
def __init__(self, index_name): | |
self.index_name = index_name | |
def __call__(self, context): | |
catalog = getToolByName(context, 'portal_catalog') | |
if self.index_name not in catalog.indexes(): | |
return SimpleVocabulary([]) | |
terms = [SimpleVocabulary.createTerm(x, x, x) for x in catalog.uniqueValuesFor(self.index_name)] | |
return SimpleVocabulary(terms) | |
Usage: | |
====== | |
class ITableRowFilter(form.Schema): | |
title = schema.Choice( | |
title=u'VHE Nummer', | |
vocabulary=CatalogIndexValuesSource('getId'), | |
required=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment