def make_terms(items):
""" Create zope.schema terms for vocab from tuples """
terms = [ SimpleTerm(value=pair[0], token=pair[0], title=pair[1]) for pair in items ]
return terms
@grok.provider(IContextSourceBinder)
def course_source(context):
"""
Populate vocabulary with values from portal_catalog.
@param context: z3c.form.Form context object (in our case site root)
@return: SimpleVocabulary containg all areas as terms.
"""
# Get site root from any content item using portal_url tool thru acquisition
root = context.portal_url.getPortalObject()
context = root.unrestrictedTraverse("courses")
# We need to include "Folder" in the query even if it's not any of the results -
# this is because the query criteria must match the root content item too
brains = query_items_in_natural_sort_order(context, query = { "portal_type" : ["xxx2011.app.courseinfo", "xxx2011.app.subjectgroup", "xxx2011.app.coursecategory", "Folder"] })
def filter(brain):
# Remove some unwanted items from the list
# XXX: Not needed anymore after new content types - remove
x = brain["Title"]
if "Carousel" in x:
return False
return True
# Create a list of tuples (UID, Title) of results
result = [ (brain["UID"], brain["Title"]) for brain in brains if filter(brain) == True ]
# Convert tuples to SimpleTerm objects
terms = make_terms(result)
return SimpleVocabulary(terms)