From 0518dda031c1a29608d0a8582df29f7cd41b28a4 Mon Sep 17 00:00:00 2001 From: David Convent Date: Wed, 6 Mar 2013 00:41:45 +0100 Subject: [PATCH] view for keyword now uses implementation without intIds --- .../dms/thesaurus/browser/dmskeyword_view.pt | 28 ++++--- .../dms/thesaurus/browser/thesaurusview.py | 16 ++++ src/collective/dms/thesaurus/browser/view.py | 76 +++++++++++-------- src/collective/dms/thesaurus/dmskeyword.py | 24 +++++- src/collective/dms/thesaurus/dmsthesaurus.py | 9 +++ src/collective/dms/thesaurus/indexers.py | 2 +- src/collective/dms/thesaurus/keywordsfield.py | 32 +++++--- .../thesaurus/profiles/default/catalog.xml | 10 ++- .../dms/thesaurus/thesauruskeywords_input.pt | 7 +- src/collective/dms/thesaurus/utils.py | 4 - src/collective/dms/thesaurus/vocabulary.py | 14 +++- 11 files changed, 154 insertions(+), 68 deletions(-) create mode 100644 src/collective/dms/thesaurus/browser/thesaurusview.py diff --git a/src/collective/dms/thesaurus/browser/dmskeyword_view.pt b/src/collective/dms/thesaurus/browser/dmskeyword_view.pt index 06464b4..856771a 100644 --- a/src/collective/dms/thesaurus/browser/dmskeyword_view.pt +++ b/src/collective/dms/thesaurus/browser/dmskeyword_view.pt @@ -52,14 +52,21 @@ - - @@ -71,13 +78,12 @@ tal:attributes="id string:form-widgets-children; class string:contenttree-widget narrowerthesauruskeywords-field; ">
  • - (nothing) + none diff --git a/src/collective/dms/thesaurus/browser/thesaurusview.py b/src/collective/dms/thesaurus/browser/thesaurusview.py new file mode 100644 index 0000000..e15768c --- /dev/null +++ b/src/collective/dms/thesaurus/browser/thesaurusview.py @@ -0,0 +1,16 @@ +from zope.component import getUtility + +from zope.app.intid.interfaces import IIntIds + +from zc.relation.interfaces import ICatalog + +from plone.dexterity.browser.view import DefaultView + +#from plone.dexterity.interfaces import IDexterityFTI +#from plone.dexterity.utils import getAdditionalSchemata + + +class DmsThesaurusView(DefaultView): + """The default view for DMSThesaurus. + """ + diff --git a/src/collective/dms/thesaurus/browser/view.py b/src/collective/dms/thesaurus/browser/view.py index f378b02..843a421 100644 --- a/src/collective/dms/thesaurus/browser/view.py +++ b/src/collective/dms/thesaurus/browser/view.py @@ -1,41 +1,57 @@ -from zope.component import getUtility - -from zope.app.intid.interfaces import IIntIds - -from zc.relation.interfaces import ICatalog - +from Products.CMFCore.utils import getToolByName from plone.dexterity.browser.view import DefaultView -#from plone.dexterity.interfaces import IDexterityFTI -#from plone.dexterity.utils import getAdditionalSchemata - - class DmsKeywordView(DefaultView): """The default view for DMSKeyword. """ @property def children(self): - return [] """ - intids = getUtility(IIntIds) - catalog = getUtility(ICatalog) - value = [] - try: - doc_intid = intids.getId(self.context) - except KeyError: - pass - else: - for ref in catalog.findRelations( - {'to_id': doc_intid, 'from_attribute': 'broader'}): - tp = dict( - url=ref.from_path, - label=ref.from_object.Title(), - hn=ref.from_object.historical_note, - sn=ref.from_object.scope_note - ) - if tp not in value: - value.append(tp) - return value + Search in catalog for keywords that have context as broader term. + Query is restricted to same thesaurus. + Returns a list of dicts with ``url`` and ``label`` attributes. + """ + thesaurus_path = '/'.join(self.context.thesaurusPath()) + catalog = getToolByName(self.context, 'portal_catalog') + brains = catalog.searchResults(portal_type='dmskeyword', + path={'query': thesaurus_path,'depth': 1}, + broaderThesaurusKeywords=self.context.id) + refs = [] + for ref in brains: + if ref.id != self.context.id: + refs.append({'url':ref.getPath(), 'label':ref.Title}) + def cmp_ref(x, y): + return cmp(x['label'], y['label']) + refs.sort(cmp_ref) + return refs + @property + def related(self): """ + Search in catalog for keywords that have context as broader term. + Query is restricted to same thesaurus. + Returns a list of dicts with ``url`` and ``label`` attributes. + """ + refs = [] + related = self.context.related + thesaurus = self.context.thesaurus() + thesaurus_path = '/'.join(thesaurus.getPhysicalPath()) + for ref in related: + kw = getattr(thesaurus, ref) + refs.append({'url': '/'.join(kw.getPhysicalPath()), + 'label': kw.Title()}) + catalog = getToolByName(self.context, 'portal_catalog') + brains = catalog.searchResults( + portal_type='dmskeyword', + path={'query': thesaurus_path,'depth': 1}, + relatedThesaurusKeywords=self.context.id + ) + for brain in brains: + ref = {'url':brain.getPath(), 'label':brain.Title} + if brain.id != self.context.id and ref not in refs: + refs.append(ref) + def cmp_ref(x, y): + return cmp(x['label'], y['label']) + refs.sort(cmp_ref) + return refs diff --git a/src/collective/dms/thesaurus/dmskeyword.py b/src/collective/dms/thesaurus/dmskeyword.py index 76465a1..beb56b7 100644 --- a/src/collective/dms/thesaurus/dmskeyword.py +++ b/src/collective/dms/thesaurus/dmskeyword.py @@ -1,4 +1,5 @@ #import datetime +from Acquisition import aq_parent from zope.interface import implements from zope import schema @@ -11,6 +12,7 @@ from plone.supermodel import model from . import _ from .keywordsfield import ThesaurusKeywords from .equivalencesfield import ThesaurusKeywordEquivalences +from .dmsthesaurus import NoThesaurusFound class IDmsKeyword(model.Schema): """ """ @@ -25,7 +27,7 @@ class IDmsKeyword(model.Schema): broader = ThesaurusKeywords( title=_(u"BT (Broader Terms)"), required=False, - vocabulary=u'dms.thesaurus.samesource' + vocabulary=u'dms.thesaurus.internalrefs' ) # RT: related term @@ -33,7 +35,7 @@ class IDmsKeyword(model.Schema): title=_(u"RT (Related Terms)"), required=False, display_backrefs=True, - vocabulary=u'dms.thesaurus.samesource' + vocabulary=u'dms.thesaurus.internalrefs' ) # HN: historical note @@ -48,11 +50,29 @@ 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 ad471df..6b27e9a 100644 --- a/src/collective/dms/thesaurus/dmsthesaurus.py +++ b/src/collective/dms/thesaurus/dmsthesaurus.py @@ -14,6 +14,10 @@ from . import _ #from plone.autoform import directives as form #from plone.directives.form import default_value +class NoThesaurusFound(Exception): + """No thesaurus found""" + + class IDmsThesaurus(model.Schema): """ """ @@ -21,6 +25,11 @@ class DmsThesaurus(Container): """ """ implements(IDmsThesaurus) + @property + def nav_entry_points(self): + entry_ids = ["001157243"] + return entry_ids + class DmsThesaurusSchemaPolicy(DexteritySchemaPolicy): """ """ diff --git a/src/collective/dms/thesaurus/indexers.py b/src/collective/dms/thesaurus/indexers.py index 7ba8bd8..8d7ab3c 100644 --- a/src/collective/dms/thesaurus/indexers.py +++ b/src/collective/dms/thesaurus/indexers.py @@ -5,7 +5,7 @@ from plone.indexer import indexer class IDmsKeywordIndexer(Interface): """Dexterity behavior interface for enabling the dynamic SearchableText - indexer on Document objecgs.""" + indexer on Document objects.""" @indexer(IDmsKeywordIndexer) diff --git a/src/collective/dms/thesaurus/keywordsfield.py b/src/collective/dms/thesaurus/keywordsfield.py index b2b4904..fcdf957 100644 --- a/src/collective/dms/thesaurus/keywordsfield.py +++ b/src/collective/dms/thesaurus/keywordsfield.py @@ -13,10 +13,8 @@ from z3c.form.widget import FieldWidget, SequenceWidget #from z3c.form.widget import Widget #from z3c.form.browser.select import SelectWidget - from . import _ - class IThesaurusKeywords(ISet): pass @@ -35,10 +33,19 @@ class IThesaurusKeywordsWidget(ISequenceWidget): pass -class NoThesaurusFound(Exception): - """No thesaurus found""" +JS_TEMPLATE = """ +$(document).ready(function() { + $('a.kw_add_link').prepOverlay({ + subtype: 'ajax', + filter: '#content>*', + urlmatch: '.*', + urlreplace: '%(thesaurus_url)s' + }); +}); +""" + class ThesaurusKeywordsWidget(SequenceWidget): implements(IThesaurusKeywordsWidget) @@ -46,6 +53,13 @@ class ThesaurusKeywordsWidget(SequenceWidget): self.display_backrefs = display_backrefs super(ThesaurusKeywordsWidget, self).__init__(request) + @property + def js(self): + thesaurus_path = '/'.join(self.context.thesaurusPath()) + return JS_TEMPLATE % dict( + thesaurus_url=thesaurus_path + ) + def items(self): value = [] for token in self.value: @@ -59,25 +73,21 @@ class ThesaurusKeywordsWidget(SequenceWidget): return value def displayItems(self): - thesaurus = self.context - - # XXX attention on remonte a partir d'un document ou d'un kw - + thesaurus_path = '/'.join(self.context.thesaurusPath()) 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': '#dummy', + 'href': thesaurus_path + '/' + term.value, }) return value + @adapter(IThesaurusKeywords, IFormLayer) @implementer(IFieldWidget) def ThesaurusKeywordsFieldWidget(field, request): diff --git a/src/collective/dms/thesaurus/profiles/default/catalog.xml b/src/collective/dms/thesaurus/profiles/default/catalog.xml index 1d1a0b6..f932f12 100644 --- a/src/collective/dms/thesaurus/profiles/default/catalog.xml +++ b/src/collective/dms/thesaurus/profiles/default/catalog.xml @@ -1,5 +1,11 @@ - - + + + + + + + + diff --git a/src/collective/dms/thesaurus/thesauruskeywords_input.pt b/src/collective/dms/thesaurus/thesauruskeywords_input.pt index 0244e1c..8b7357e 100644 --- a/src/collective/dms/thesaurus/thesauruskeywords_input.pt +++ b/src/collective/dms/thesaurus/thesauruskeywords_input.pt @@ -26,11 +26,13 @@ > ×, ×, - +Add keyword + - diff --git a/src/collective/dms/thesaurus/utils.py b/src/collective/dms/thesaurus/utils.py index 7b24d0b..2097e43 100644 --- a/src/collective/dms/thesaurus/utils.py +++ b/src/collective/dms/thesaurus/utils.py @@ -30,10 +30,6 @@ class ImportJson(BrowserView): title=term.get('title')) object = getattr(self.context, term_id) notify(ObjectAddedEvent(object)) - #try: - # term_intids[term_id] = self.intids.getId(object) - #except KeyError: - # self.intids.register(object) object.title = term.get('title') object.historical_note = term.get('historical_note') object.scope_note = term.get('scope_note') diff --git a/src/collective/dms/thesaurus/vocabulary.py b/src/collective/dms/thesaurus/vocabulary.py index d6d0e2e..524828b 100644 --- a/src/collective/dms/thesaurus/vocabulary.py +++ b/src/collective/dms/thesaurus/vocabulary.py @@ -7,7 +7,6 @@ from zope.schema.interfaces import IVocabularyFactory #from zope.schema.interfaces import ISource, IContextSourceBinder from zope.schema.vocabulary import SimpleVocabulary #from zope.schema.vocabulary import SimpleTerm - from Products.CMFCore.utils import getToolByName @@ -40,16 +39,23 @@ grok.global_utility(GlobalThesaurusSource, name=u'dms.thesaurus.global') 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): - #XXX: need to add url filter + thesaurus_path = '/'.join(context.thesaurusPath()) catalog = getToolByName(context, 'portal_catalog') - results = catalog(portal_type='dmskeyword') + results = catalog(portal_type='dmskeyword', + path={'query': thesaurus_path,'depth': 1}) keywords = [x.getObject() for x in results] def cmp_keyword(x, y): return cmp(x.title, y.title) keywords.sort(cmp_keyword) + keyword_ids = [x.id for x in keywords] keyword_terms = [SimpleVocabulary.createTerm( x.id, x.id, x.title) for x in keywords] @@ -61,4 +67,4 @@ class KeywordFromSameThesaurusSource(object): grok.global_utility(KeywordFromSameThesaurusSource, - name=u'dms.thesaurus.samesource') + name=u'dms.thesaurus.internalrefs')