[Grok-dev] Values from another document before rendering

Sebastian Ware sebastian at urbantalk.se
Fri Feb 12 07:45:21 EST 2010


the check isn't needed in the addform. It is because in an editform  
you might have a value that is different from that of the vocabulary  
list. Eventhough the value is illegal, you don't want it to stop you  
from rendering your form (if the current value isn't found in the  
vocabulary formlib will raise an exception). Hence the check and  
adding the current value to the list of values (presumably with a  
title "illegal value" or something more understandable).

I am an advocate of using an IDE and can warmly recomend Komodo IDE,  
haven't done development in paster mode, but it works like a charm  
with zopectl (sure it is the same in Paster). Anyway, if you go that  
route, I can send you instructions on how to setup your debugging, if  
this doesn't help:

   http://grok.zope.org/documentation/how-to/graphical-debugging-of-grok-with-komodo-ide

Mvh Sebastian

12 feb 2010 kl. 12.17 skrev Kathy Manwaring:

> OK. I had an issue with getting a vocabulary working, back in  
> November.
>
> The response that you sent me (quoted below) refers to a check for
> gallery_id:
>
> 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)
>
> Presumably the call to the vocabulary includes some way of setting the
> value of gallery_id - I can't get a value on my context, so I hope  
> you can
> send me the code for that bit as well!
>
> Hopefully that is clearer - thanks for your time, Sebastian!
>
> Kathy
>
>> I am pretty sure I misunderstood the question...
>>
>> Mvh Sebastian
>
>>>> 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