[Grok-dev] Values from another document before rendering
Sebastian Ware
sebastian at urbantalk.se
Thu Nov 26 10:03:59 EST 2009
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
26 nov 2009 kl. 11.00 skrev Kathy Manwaring:
> Firstly, thank you very much for your quick responses to my last
> query -
> they put me on the right track. Now, I have another question...
>
> I have a class that needs to get values from another entry when it is
> created or edited. So, when creating a Person, the list of values
> that I
> can choose from for the type of Person are values that are editable on
> another entry.
>
> It LOOKS like I should be able to use a dynamically calculated
> SimpleVocabulary, but I can't get it to work - I think I need to use
> self.context on the interface definition of the schema field, but
> self is
> not defined.
>
> Alternatively, can I do it in setupWidgets before rendering? (Can't
> see
> enough information about setupWidgets to answer the question).
>
> Any help would be appreciated.
>
> Kathy
>
> _______________________________________________
> Grok-dev mailing list
> Grok-dev at zope.org
> https://mail.zope.org/mailman/listinfo/grok-dev
More information about the Grok-dev
mailing list