This repository has been archived on 2023-02-21. You can view files and clone it, but cannot push or open issues or pull requests.
collective.dms.thesaurus/src/collective/dms/thesaurus/vocabulary.py

80 lines
2.7 KiB
Python
Raw Normal View History

from zope.interface import Interface
from five import grok
from zope.schema.interfaces import IVocabularyFactory
from zope.schema.vocabulary import SimpleVocabulary
from Products.CMFCore.utils import getToolByName
import utils
class NoThesaurusFound(Exception):
"""No thesaurus found"""
class IMainThesaurus(Interface):
""" Marker interface for main thesaurus container
"""
class SimpleThesaurusSource(object):
"""This basic vocabulary is here mainly for demo purpose.
It is not meant to be used when a Plone site contains more than one
thesaurus.
"""
grok.implements(IVocabularyFactory)
2013-04-13 13:39:56 +02:00
def __call__(self, context):
# build vocab from first thesaurus returned by catalog
2013-04-14 16:56:28 +02:00
thesaurus = utils.get_thesaurus_object(context)
2013-04-14 17:33:36 +02:00
if thesaurus is None:
return SimpleVocabulary([])
2013-04-14 16:56:28 +02:00
thesaurus_path = '/'.join(thesaurus.getPhysicalPath())
catalog = getToolByName(context, 'portal_catalog')
results = catalog(portal_type='dmskeyword',
path={'query': thesaurus_path,'depth': 1})
keywords = [x.getObject() for x in results]
keyword_terms = [SimpleVocabulary.createTerm(
x.id, x.id, x.title) for x in keywords]
return SimpleVocabulary(keyword_terms)
def __iter__(self):
# hack to let schema editor handle the field
yield u'DO NOT TOUCH'
grok.global_utility(SimpleThesaurusSource,
name=u'dms.thesaurus.simple')
class KeywordFromSameThesaurusSource(object):
"""This vocabulary is used for keywords that reference one another
inside the same thesaurus. It should not be used for referencing
keywords from other content types.
"""
grok.implements(IVocabularyFactory)
def __call__(self, context):
if context.portal_type == 'dmsthesaurus':
thesaurus_path = '/'.join(context.getPhysicalPath())
else:
thesaurus = utils.get_thesaurus_object(context)
2013-04-14 17:33:36 +02:00
if thesaurus is None:
return SimpleVocabulary([])
thesaurus_path = '/'.join(thesaurus.getPhysicalPath())
catalog = getToolByName(context, 'portal_catalog')
results = catalog(portal_type='dmskeyword',
path={'query': thesaurus_path,'depth': 1})
keywords = [x.getObject() for x in results]
keyword_terms = [SimpleVocabulary.createTerm(
x.id, x.id, x.title) for x in keywords]
return SimpleVocabulary(keyword_terms)
def __iter__(self):
# hack to let schema editor handle the field
yield u'DO NOT TOUCH'
grok.global_utility(KeywordFromSameThesaurusSource,
name=u'dms.thesaurus.internalrefs')