From 651166ddb6c3f821660465678f63b1b8f06c4ce2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Sun, 14 Apr 2013 16:27:29 +0200 Subject: [PATCH] use keywordsfield to entry points; use a single thesaurus for now --- src/collective/dms/thesaurus/browser/view.py | 7 +- src/collective/dms/thesaurus/dmskeyword.py | 18 ---- src/collective/dms/thesaurus/dmsthesaurus.py | 5 +- .../dms/thesaurus/entrypoints_display.pt | 9 -- .../dms/thesaurus/entrypointsfield.py | 82 ------------------- src/collective/dms/thesaurus/keywordsfield.py | 9 +- src/collective/dms/thesaurus/utils.py | 7 ++ src/collective/dms/thesaurus/vocabulary.py | 11 ++- src/collective/dms/thesaurus/widgets.zcml | 3 - 9 files changed, 29 insertions(+), 122 deletions(-) delete mode 100644 src/collective/dms/thesaurus/entrypoints_display.pt delete mode 100644 src/collective/dms/thesaurus/entrypointsfield.py diff --git a/src/collective/dms/thesaurus/browser/view.py b/src/collective/dms/thesaurus/browser/view.py index 843a421..77a47f2 100644 --- a/src/collective/dms/thesaurus/browser/view.py +++ b/src/collective/dms/thesaurus/browser/view.py @@ -1,6 +1,8 @@ from Products.CMFCore.utils import getToolByName from plone.dexterity.browser.view import DefaultView +from collective.dms.thesaurus import utils + class DmsKeywordView(DefaultView): """The default view for DMSKeyword. """ @@ -12,7 +14,8 @@ class DmsKeywordView(DefaultView): Query is restricted to same thesaurus. Returns a list of dicts with ``url`` and ``label`` attributes. """ - thesaurus_path = '/'.join(self.context.thesaurusPath()) + thesaurus = utils.get_thesaurus_object(self.context) + thesaurus_path = '/'.join(thesaurus.getPhysicalPath()) catalog = getToolByName(self.context, 'portal_catalog') brains = catalog.searchResults(portal_type='dmskeyword', path={'query': thesaurus_path,'depth': 1}, @@ -35,7 +38,7 @@ class DmsKeywordView(DefaultView): """ refs = [] related = self.context.related - thesaurus = self.context.thesaurus() + thesaurus = utils.get_thesaurus_object(self.context) thesaurus_path = '/'.join(thesaurus.getPhysicalPath()) for ref in related: kw = getattr(thesaurus, ref) diff --git a/src/collective/dms/thesaurus/dmskeyword.py b/src/collective/dms/thesaurus/dmskeyword.py index da88e1a..5c9eb03 100644 --- a/src/collective/dms/thesaurus/dmskeyword.py +++ b/src/collective/dms/thesaurus/dmskeyword.py @@ -54,29 +54,11 @@ class IDmsKeyword(model.Schema): required=False, ) - def thesaurus(): - """get parent thesaurus""" - - def thesaurusPath(): - """get parent thesaurus physical path""" - class DmsKeyword(Item): """ """ implements(IDmsKeyword) - def thesaurus(self): - thesaurus = self - while thesaurus.portal_type != "dmsthesaurus": - thesaurus = aq_parent(thesaurus) - if not hasattr(thesaurus, 'portal_type') or getattr(thesaurus, 'portal_type', None) is None: - raise NoThesaurusFound - return thesaurus - - def thesaurusPath(self): - return self.thesaurus().getPhysicalPath() - - class DmsKeywordSchemaPolicy(DexteritySchemaPolicy): """ """ diff --git a/src/collective/dms/thesaurus/dmsthesaurus.py b/src/collective/dms/thesaurus/dmsthesaurus.py index 1b551d1..f5eab52 100644 --- a/src/collective/dms/thesaurus/dmsthesaurus.py +++ b/src/collective/dms/thesaurus/dmsthesaurus.py @@ -7,14 +7,15 @@ from plone.supermodel import model from . import _ -from .entrypointsfield import EntryPoints +from .keywordsfield import ThesaurusKeywords class IDmsThesaurus(model.Schema): """ """ - entry_points = EntryPoints( + entry_points = ThesaurusKeywords( title=_(u"Entry Points"), description=_(u"First level of navigation for this Thesaurus"), + vocabulary=u'dms.thesaurus.internalrefs', required=False) class DmsThesaurus(Container): diff --git a/src/collective/dms/thesaurus/entrypoints_display.pt b/src/collective/dms/thesaurus/entrypoints_display.pt deleted file mode 100644 index 43ce052..0000000 --- a/src/collective/dms/thesaurus/entrypoints_display.pt +++ /dev/null @@ -1,9 +0,0 @@ - \ No newline at end of file diff --git a/src/collective/dms/thesaurus/entrypointsfield.py b/src/collective/dms/thesaurus/entrypointsfield.py deleted file mode 100644 index 6df31db..0000000 --- a/src/collective/dms/thesaurus/entrypointsfield.py +++ /dev/null @@ -1,82 +0,0 @@ -from zope.interface import implements, implementer -from zope.component import adapter - -from zope import schema -from zope.schema.interfaces import IChoice -from zope.schema.interfaces import ISet -from zope.schema.interfaces import IFromUnicode - -from zope.browserpage.viewpagetemplatefile import ViewPageTemplateFile - -from z3c.form.interfaces import IFormLayer, IFieldWidget -from z3c.form.widget import FieldWidget - -from plone.formwidget.autocomplete.widget import AutocompleteMultiSelectionWidget - -#from . import _ - -#from collective.dms.thesaurus.vocabulary import InternalThesaurusSource - -class IEntryPointChoice(IChoice): - """Thesaurus Entry Point Choice Item. - """ - -class EntryPointChoice(schema.Choice): - implements(IEntryPointChoice, IFromUnicode) - -class IEntryPoints(ISet): - """Thesaurus Entry Point List. - """ - -class EntryPoints(schema.Set): - implements(IEntryPoints, IFromUnicode) - - def __init__(self, **kw): - vt = kw.pop('value_type', - schema.Choice( - required=False, - vocabulary=u'dms.thesaurus.internalrefs') - ) - super(EntryPoints, self).__init__(value_type=vt, **kw) - - -class EntryPointsWidget(AutocompleteMultiSelectionWidget): - - klass = u"entrypoints-widget" - display_template = ViewPageTemplateFile('entrypoints_display.pt') - maxResults = 50 - - #def __init__(self, request): - # super(EntryPointsWidget, self).__init__(request) - - def items(self): - value = [] - for token in self.value: - # Ignore no value entries. They are in the request only. - if token == self.noValueToken: - continue - term = self.terms.getTermByToken(token) - value.append({'id': token, 'value': term.value, - 'content': term.title, 'selected': True - }) - return value - - def displayItems(self): - path = '/'.join(self.context.getPhysicalPath()) - value = [] - for token in self.value: - # Ignore no value entries. They are in the request only. - if token == self.noValueToken: - continue - term = self.terms.getTermByToken(token) - value.append( - {'title': term.title, - 'href': '/'.join((path, term.value)) - }) - return value - -@adapter(IEntryPoints, IFormLayer) -@implementer(IFieldWidget) -def EntryPointsFieldWidget(field, request): - return FieldWidget(field, EntryPointsWidget(request)) - diff --git a/src/collective/dms/thesaurus/keywordsfield.py b/src/collective/dms/thesaurus/keywordsfield.py index 7ca9f70..b269a9e 100644 --- a/src/collective/dms/thesaurus/keywordsfield.py +++ b/src/collective/dms/thesaurus/keywordsfield.py @@ -9,6 +9,9 @@ from z3c.form.widget import FieldWidget, SequenceWidget from . import _ +import utils + + class IThesaurusKeywords(ISet): pass @@ -48,7 +51,8 @@ class ThesaurusKeywordsWidget(SequenceWidget): @property def js(self): - thesaurus_path = '/'.join(self.context.thesaurusPath()) + thesaurus = utils.get_thesaurus_object(self.context) + thesaurus_path = '/'.join(thesaurus.getPhysicalPath()) return JS_TEMPLATE % dict( thesaurus_url=thesaurus_path ) @@ -66,7 +70,8 @@ class ThesaurusKeywordsWidget(SequenceWidget): return value def displayItems(self): - thesaurus_path = '/'.join(self.context.thesaurusPath()) + thesaurus = utils.get_thesaurus_object(self.context) + thesaurus_path = '/'.join(thesaurus.getPhysicalPath()) value = [] for token in self.value: # Ignore no value entries. They are in the request only. diff --git a/src/collective/dms/thesaurus/utils.py b/src/collective/dms/thesaurus/utils.py index f042efd..48cdbcf 100644 --- a/src/collective/dms/thesaurus/utils.py +++ b/src/collective/dms/thesaurus/utils.py @@ -9,6 +9,7 @@ from zope.schema.interfaces import IVocabularyFactory from zope.event import notify from zope.lifecycleevent import ObjectAddedEvent, ObjectModifiedEvent +from Products.CMFCore.utils import getToolByName class ImportJson(BrowserView): def __call__(self): @@ -41,3 +42,9 @@ class ImportJson(BrowserView): notify(ObjectModifiedEvent(object)) return 'OK' + + +def get_thesaurus_object(context): + catalog = getToolByName(context, 'portal_catalog') + thesaurus = catalog(portal_type='dmsthesaurus')[0].getObject() + return thesaurus diff --git a/src/collective/dms/thesaurus/vocabulary.py b/src/collective/dms/thesaurus/vocabulary.py index adbc4e7..5c906e4 100644 --- a/src/collective/dms/thesaurus/vocabulary.py +++ b/src/collective/dms/thesaurus/vocabulary.py @@ -6,6 +6,8 @@ from zope.schema.vocabulary import SimpleVocabulary from Products.CMFCore.utils import getToolByName +import utils + class NoThesaurusFound(Exception): """No thesaurus found""" @@ -14,7 +16,7 @@ class IMainThesaurus(Interface): """ Marker interface for main thesaurus container """ -class SimpleThesaurusSource(object): +class GlobalThesaurusSource(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. @@ -40,8 +42,8 @@ class SimpleThesaurusSource(object): yield u'DO NOT TOUCH' -grok.global_utility(SimpleThesaurusSource, - name=u'dms.thesaurus.simple') +grok.global_utility(GlobalThesaurusSource, + name=u'dms.thesaurus.global') class KeywordFromSameThesaurusSource(object): @@ -55,7 +57,8 @@ class KeywordFromSameThesaurusSource(object): if context.portal_type == 'dmsthesaurus': thesaurus_path = '/'.join(context.getPhysicalPath()) else: - thesaurus_path = '/'.join(context.thesaurusPath()) + thesaurus = utils.get_thesaurus_object(context) + thesaurus_path = '/'.join(thesaurus.getPhysicalPath()) catalog = getToolByName(context, 'portal_catalog') results = catalog(portal_type='dmskeyword', path={'query': thesaurus_path,'depth': 1}) diff --git a/src/collective/dms/thesaurus/widgets.zcml b/src/collective/dms/thesaurus/widgets.zcml index 1e97231..dbc3bf9 100644 --- a/src/collective/dms/thesaurus/widgets.zcml +++ b/src/collective/dms/thesaurus/widgets.zcml @@ -28,7 +28,4 @@ template="thesaurus-keyword-equivs-display.pt" /> - - -