1 | from Products.CPSSchemas.VocabulariesTool import LOCAL_VOCABULARY_CONTAINER_ID |
---|
2 | def getVocabularyFor(self, context, voc_id): |
---|
3 | """Returns vocabulary for given context.""" |
---|
4 | |
---|
5 | def hasLocalVocContainer(context): |
---|
6 | if getattr(context, 'hasObject', None) is None: |
---|
7 | return False |
---|
8 | return context.hasObject(LOCAL_VOCABULARY_CONTAINER_ID) |
---|
9 | |
---|
10 | def getLocalVocContainer(context): |
---|
11 | return context._getOb(LOCAL_VOCABULARY_CONTAINER_ID, None) |
---|
12 | |
---|
13 | getParentNode = lambda node: getattr(getattr(node, 'aq_inner', None), |
---|
14 | 'aq_parent', None) |
---|
15 | |
---|
16 | if not hasLocalVocContainer(context): |
---|
17 | parent = getParentNode(context) |
---|
18 | while parent is not None: |
---|
19 | if hasLocalVocContainer(parent): |
---|
20 | return getLocalVocContainer(parent).getVocabulary(voc_id) |
---|
21 | parent = getParentNode(parent) |
---|
22 | # no local vocabulary container found for given context |
---|
23 | globvoc = self._getOb(voc_id, None) |
---|
24 | if globvoc is None: |
---|
25 | raise KeyError, 'No vocabulary by id %s' % voc_id |
---|
26 | return globvoc |
---|
27 | else: |
---|
28 | return getLocalVocContainer(context).getVocabulary(voc_id) |
---|
29 | from Products.CPSSchemas.VocabulariesTool import VocabulariesTool |
---|
30 | VocabulariesTool.getVocabularyFor = getVocabularyFor |
---|