[Grok-dev] Vocabulary registration
Darryl Cousins
darryl at darrylcousins.net.nz
Mon Mar 12 20:32:22 EDT 2007
Hi,
I'm struggling a little here registering a vocabulary for use in a form.
I'm reusing code that I'd previously used and trying to twig it to work
with grok.
I can easily register a vocabulary as a global utility:
class I18nContextLanguageVocabulary(object):
"""Returns the vocabulary for a choice field of available languages
on the context"""
def __call__(self, context):
return
SimpleVocabulary.fromValues(context.getAvailableLanguages())
grok.global_utility(I18nContextLanguageVocabulary,
provides=schema.interfaces.IVocabularyFactory,
name=u'I18n Context Languages')
And I can get the utility as expected:
>>> rock = I18nRock(title=u'I wish I were a rock')
>>> rock.title
u'I wish I were a rock'
>>> rock.addLanguage('de',title=u'Ich bin ein Stein')
>>> rock.getAttribute('title','de')
u'Ich bin ein Stein'
>>> from zope.schema.interfaces import IVocabularyFactory
>>> vocab = component.getUtility(IVocabularyFactory, name='I18n
Context Languages')
>>> [t.token for t in vocab(rock)]
['de', 'en']
But when I call for the i18neditform (which uses the vocabulary):
>>> request = TestRequest()
>>> from zope import component
>>> view = component.getMultiAdapter((rock, request), name='edit')()
Traceback (most recent call last):
...
VocabularyRegistryError: unknown vocabulary: u'I18n Context
Languages'
So vocabularies registered as global utilities are not available in the
Vocabulary registry. This may be normal?
I'll go on to try setting up a root Application object and registering
the vocabulary as a local_utility to see if I can get to it that way.
Many thanks,
Darryl
More information about the Grok-dev
mailing list