Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jaroel
Last active December 16, 2015 00:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaroel/5345392 to your computer and use it in GitHub Desktop.
Save jaroel/5345392 to your computer and use it in GitHub Desktop.
Parameterised dynamic vocabulary. Shows content of catalog using query with object_provides. Pass in the interface in the schema.
Definition:
===========
from zope.schema.interfaces import IContextSourceBinder, IBaseVocabulary
class CatalogObjectProvidesSource(object):
grok.implements(IContextSourceBinder, IBaseVocabulary)
def __init__(self, interface):
self.interface = interface
def __call__(self, context):
catalog = getToolByName(context, 'portal_catalog')
brains = catalog(object_provides=self.interface.__identifier__)
terms = [SimpleVocabulary.createTerm(x.getId, x.getId, x.Title) for x in brains]
return SimpleVocabulary(terms)
Usage:
======
from my.contenttype.complex import IHoofdComplex
class ITableRowFilter(form.Schema):
hoofd_complex = schema.Choice(
title=u'Hoofd Complex',
vocabulary=CatalogObjectProvidesSource(IHoofdComplex),
required=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment