view for keyword now uses implementation without intIds

This commit is contained in:
David Convent 2013-03-06 00:41:45 +01:00
parent 7416350c25
commit 0518dda031
11 changed files with 154 additions and 68 deletions

View File

@ -52,14 +52,21 @@
</dd></dl>
<tal:related define="widget python:view.widgets.get('related')">
<div id="dmskeywords-navigation-related-field"
class="field">
<label tal:content="widget/label" />
<br />
<div tal:content="structure widget/render" />
</div>
</tal:related>
<div class="field" id="dmskeywords-navigation-related-field">
<label tal:content="string:RT (Related Terms)" />
<br />
<tal:list condition="view/related">
<ul id="" class=""
tal:attributes="id string:form-widgets-related;
class string:contenttree-widget relatedthesauruskeywords-field;
"><li tal:repeat="value view/related"
><a href="#"
tal:content="value/label"
tal:attributes="href value/url"
/></li
></ul></tal:list>
<em tal:condition="not:view/related">none</em>
</div>
</div>
@ -71,13 +78,12 @@
tal:attributes="id string:form-widgets-children;
class string:contenttree-widget narrowerthesauruskeywords-field;
"><li tal:repeat="value view/children"
><a class="selected-option"
href="#"
><a href="#"
tal:content="value/label"
tal:attributes="href value/url"
/></li
></ul></tal:list>
<em tal:condition="not:view/children">(nothing)</em>
<em tal:condition="not:view/children">none</em>
</div>
</div>

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,5 +1,11 @@
<?xml version="1.0"?>
<object name="portal_catalog" meta_type="Plone Catalog Tool">
<column value="historical_note"/>
<column value="scope_note"/>
<column value="historical_note" />
<column value="scope_note" />
<index name="broaderThesaurusKeywords" meta_type="KeywordIndex">
<indexed_attr value="broader" />
</index>
<index name="relatedThesaurusKeywords" meta_type="KeywordIndex">
<indexed_attr value="related" />
</index>
</object>

View File

@ -26,11 +26,13 @@
><span class="selected-option"
tal:attributes="data-term-id item/value">
<span tal:content="item/content"
/> <span class="remove">×</span></span><tal:block condition="not:repeat/item/end">, </tal:block
/> <span class="remove">×</span></span
><tal:block condition="not:repeat/item/end">, </tal:block
></tal:block
></span>
<!--script type="text/javascript" tal:content="structure view/js"></script-->
<a href="#" class="kw_add_link">Add keyword</a>
<script type="text/javascript" tal:content="structure view/js"></script>
<style type="text/css">
span.selected-option {
@ -48,5 +50,4 @@ span.remove {
}
</style>
<button onclick="return false;">Add</button>
</div>

View File

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

View File

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