use keywordsfield to entry points; use a single thesaurus for now

This commit is contained in:
Frédéric Péters 2013-04-14 16:27:29 +02:00
parent 49125d9d1a
commit 651166ddb6
9 changed files with 29 additions and 122 deletions

View File

@ -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)

View File

@ -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):
""" """

View File

@ -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):

View File

@ -1,9 +0,0 @@
<ul id="" class=""
tal:condition="view/displayItems"
tal:define="th_url python:'/'.join(view.context.absolute_url().split('/')[:-1]);"
tal:attributes="id view/id"
><li tal:repeat="value view/displayItems"><a href=""
tal:content="value/title"
tal:attributes="href value/href"
/></li
></ul>

View File

@ -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))

View File

@ -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.

View File

@ -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

View File

@ -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})

View File

@ -28,7 +28,4 @@
template="thesaurus-keyword-equivs-display.pt"
/>
<!-- thesaurus entry points widget, used in thesaurus schema -->
<adapter factory=".entrypointsfield.EntryPointsFieldWidget"/>
</configure>