[Grok-dev] Values from another document before rendering

Kathy Manwaring kathy at perfectnotes.com.au
Fri Feb 12 05:38:30 EST 2010


Hi Sebastian,

You helped me out with this late last year. Unfortunately, I can't
currently work out how you get the value of gallery_id on context - in my
situation, context does not have any attributes, and I need it to (if I
can).
Can you please show me the call to the vocabulary as well?

Thanks

Kathy

Sebastian Ware wrote:
> This is what I do. I have a field defined in an interface:
>
>   gallery_id = schema.Choice(title=u"Gallery", vocabulary=u"Published
NewProductGalleries")
>
> The vocabulary looks like this (it is a bit dirty, but it might help you
in the right direction):
>
>
> import grok
> from zope import schema
> from zope.schema.vocabulary import SimpleVocabulary
> from boardsportsource import interfaces
> from boardsportsource import workflow
> from hurry import query
>
> class NewProductGalleriesSource(grok.GlobalUtility):
>     grok.implements(schema.interfaces.IVocabularyFactory)
>     grok.name('Published NewProductGalleries')
>     def __call__(self, context):
>         terms = self.get_published_galleries(context)
>         return  SimpleVocabulary(terms)
>
>     def get_published_galleries(self, context):
>         hasItem = []
>         if hasattr(context, 'gallery_id') and context.gallery_id is not
None:
>             # I allways need to have the current value in my vocabulary
>             # or choice widgets won't render.
>             hasItem.append(context.gallery_id)
>
>         # I use hurry.query to find the objects I want to use in
>         # my vocabulary.
>         theQuery = query.Eq(('workflow_catalog', 'workflow_state'),
workflow.PUBLISHED)
>         theQuery = theQuery & query.Eq(('workflow_catalog',
'object_type'), 'new_product_gallery')
>         dictResult = query.query.Query().searchResults(theQuery)
>
>         theList = []
>         for item in dictResult:
>             theObj = interfaces.INewProductGallery(item)
>             theList.append(SimpleVocabulary.createTerm(theObj.__name__,
theObj.__name__, '%s (%s)' % (theObj.title, 'default')))
>             if theObj.__name__ in hasItem:
>                 hasItem.remove(theObj.__name__)
>
>         for key in hasItem:
>             # If the current value wasn't found in the search I add it here
>             # with an appropriate message.
>             if grok.getSite()['default'].has_key(key):
>                 theObj =
interfaces.IImageOfTheDayGallery(grok.getSite()['default'][key])
>                 theList.append(SimpleVocabulary.createTerm(key, key, '%s
(not published!)' % theObj.title))
>             else:
>                 theList.append(SimpleVocabulary.createTerm(key, key, '%s
has been removed!' % key))
>         return theList
>
> Mvh Sebastian






More information about the Grok-dev mailing list