Skip to content

Instantly share code, notes, and snippets.

@jaroel
Created April 9, 2013 12:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaroel/5345403 to your computer and use it in GitHub Desktop.
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.
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