#
# Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
__docformat__ = 'restructuredtext'
# import standard library
# import interfaces
from pyams_thesaurus.interfaces.term import STATUS_ARCHIVED
from pyams_thesaurus.interfaces.thesaurus import IThesaurus, IThesaurusExtracts
# import packages
from pyams_utils.list import unique
from pyams_utils.registry import query_utility
from pyramid_rpc.jsonrpc import jsonrpc_method
[docs]@jsonrpc_method(endpoint='thesaurus')
def getTopTerms(request, thesaurus_name, extract_name=None):
"""Get top terms of given thesaurus"""
thesaurus = query_utility(IThesaurus, name=thesaurus_name)
if thesaurus is None:
return []
return ({'id': term.label,
'text': term.label} for term in thesaurus.getTopTerms(extract_name))
[docs]@jsonrpc_method(endpoint='thesaurus')
def findTerms(request, query, thesaurus_name, extract_name=None):
"""Find terms matching given query, returning their title"""
thesaurus = query_utility(IThesaurus, name=thesaurus_name)
if thesaurus is None:
return []
return [{'id': term.label,
'text': term.title}
for term in unique(thesaurus.find_terms(query, extract_name, exact=True, stemmed=True),
key=lambda x: x.title)
if term.status != STATUS_ARCHIVED]
[docs]@jsonrpc_method(endpoint='thesaurus')
def findTermsWithLabel(request, query, thesaurus_name, extract_name=None):
"""Find terms matching given query, returning their full label"""
thesaurus = query_utility(IThesaurus, name=thesaurus_name)
if thesaurus is None:
return []
return [{'id': term.label,
'text': term.label}
for term in unique(thesaurus.find_terms(query, extract_name, exact=True, stemmed=True),
key=lambda x: x.label)
if term.status != STATUS_ARCHIVED]