[Grok-dev] Re: Recipe - How to create a vocabulary that allows the
display of non-ASCII characters
Sebastian Ware
sebastian at urbantalk.se
Mon Sep 24 15:24:34 EDT 2007
Works like a charm :)
Mvh Sebastian
24 sep 2007 kl. 18.23 skrev Philipp von Weitershausen:
> Sebastian Ware wrote:
>> I just want to pitch my idea of what a recipe might look like. In
>> this case showing how to create a vocabulary that displays unicode
>> titles.
>> Mvh Sebastian
>> ** Recipe - How to create a vocabulary that allows the display of
>> non-ASCII characters **
>> Keywords: Recipe, Vocabulary, Unicode, Global Utility
>> How to create a vocabulary that displays non-ASCII titles. Useful
>> for use with zope.schema.Choice fields. The title of each term
>> that is to be displayed can be Unicode strings.
>> [code:vocabulary.py]
>> from zope import schema
>> from zope.schema.vocabulary import SimpleVocabulary
>> from hurry import query
>> class VocabularySource(object):
>> def __call__(self, context):
>> # Get a list of objects (I am using hurry.query to search
>> a catalog)
>> theQuery = query.Eq(('workflow_catalog',
>> 'workflow_state'), interfaces.PUBLISHED)
>> result = query.query.Query().searchResults(theQuery)
>> # For each object, add it as a list of terms
>> (zope.schema.vocabulary.SimpleTerm)
>> # creating each term with SimpleVocabulary.createTerm
>> (value, token, title). This
>> # allows the title to be unicode, the token has to be
>> ASCII (and can be the
>> # same as value).
>> theTerms = []
>> for item in result:
>> theTerms.append(SimpleVocabulary.createTerm
>> (item.__name__,
>>
>> item.iso_code,
>> item.title))
>> return SimpleVocabulary(theTerms)
>> # Register the vocabulary as a global utility.
>> grok.global_utility(VocabularySource,
>> provides=schema.interfaces.IVocabularyFactory,
>> name=u'Published Objects')
>> [/code]
>
> This is weird. You're registering this as a utility providing
> IVocabularyFactory, but the object doesn't actually implement it.
> This can actually be implemented much more easily:
>
> class MyVocabularyFactory(grok.GlobalUtility):
> grok.implements(IVocabularyFactory)
> grok.name(u'Published Objects')
>
> def __call__(self, context):
> ...
>
> No further registration necessary.
>
>
> --
> http://worldcookery.com -- Professional Zope documentation and
> training
More information about the Grok-dev
mailing list