#
# 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'
from hypatia.catalog import CatalogQuery
from hypatia.interfaces import ICatalog
from hypatia.query import Contains, Eq
from pyramid_rpc.jsonrpc import jsonrpc_method
from pyams_catalog.query import CatalogResultSet
from pyams_i18n.interfaces import INegotiator
from pyams_sequence.interfaces import ISequentialIntIds
from pyams_sequence.reference import get_last_version, get_sequence_dict
from pyams_utils.list import unique
from pyams_utils.registry import get_utility
[docs]@jsonrpc_method(endpoint='sequence')
def findReferences(request, query, content_type):
"""Find references matching given query"""
if not query:
return []
catalog = get_utility(ICatalog)
sequence = get_utility(ISequentialIntIds)
if query.startswith('+'):
params = Eq(catalog['oid'], sequence.get_full_oid(query))
else:
query_params = Eq(catalog['oid'], sequence.get_full_oid(query))
negotiator = get_utility(INegotiator)
for lang in {request.registry.settings.get('pyramid.default_locale_name', 'en'),
request.locale_name,
negotiator.server_language} | negotiator.offered_languages:
index_name = 'title:{0}'.format(lang)
if index_name in catalog:
index = catalog[index_name]
if index.check_query(query):
query_params |= Contains(index, ' and '.join((w + '*' for w in query.split())))
params = query_params
if content_type:
params &= Eq(catalog['content_type'], content_type)
return sorted([get_sequence_dict(result)
for result in unique(filter(lambda x: x is not None,
map(get_last_version,
CatalogResultSet(CatalogQuery(catalog).query(params)))))],
key=lambda x: x['text'])