[Grok-dev] Values from another document before rendering
Kathy Manwaring
kathy at perfectnotes.com.au
Fri Feb 12 06:08:54 EST 2010
Hi Sebastian,
Yes - it is an addform - will look at that (it's late on Fri night here,
so not really looking too hard at this at the moment!)
(I would set breakpoints if I had read that tutorial... I use paster with
debug.ini, but don't know how to get breakpoints happening in Grok!)
Thanks for the quick response (again!)
Kathy
> Is it an addform? In which case maybe you want to use some class
> attributes on your object with defaults.
>
> class MyObj(gork.Model):
>
> gallery_id = None
> title = None
> whatever = None
> a_crazy_list = None # never set a class level attribute to a list
> or object.
>
> Just on a side note. Are you setting breakpoints and dropping into
> interactive mode during your debugging sessions?
>
> Mvh Sebastian
>
> 12 feb 2010 kl. 11.38 skrev Kathy Manwaring:
>
>> 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