search: add a singleton so apps can register their search engines (#25620)

This commit is contained in:
Frédéric Péters 2018-08-09 17:01:28 +02:00 committed by Thomas NOEL
parent d4f0d28e6e
commit c0031f62d2
5 changed files with 53 additions and 5 deletions

View File

@ -17,6 +17,9 @@
import django.apps
from django.utils.translation import ugettext_lazy as _
from .engines import engines
class AppConfig(django.apps.AppConfig):
name = 'combo.apps.search'
verbose_name = _('Search')

View File

@ -0,0 +1,35 @@
# combo - content management system
# Copyright (C) 2014-2018 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from django.conf import settings
class Engines(object):
"""Singleton object that serves as a registry of classes providing search
engines."""
def __init__(self):
self.engines = {}
def register(self, key, **options):
self.engines[key] = options
def get(self, key):
if key in settings.COMBO_SEARCH_SERVICES:
return settings.COMBO_SEARCH_SERVICES[key]
return self.engines.get(key)
engines = Engines() # singleton object

View File

@ -29,6 +29,8 @@ from combo.data.models import CellBase
from combo.data.library import register_cell_class
from combo.utils import get_templated_url
from . import engines
@register_cell_class
class SearchCell(CellBase):
@ -55,10 +57,7 @@ class SearchCell(CellBase):
def search_services(self):
services = []
for service_slug in self._search_services.get('data') or []:
if service_slug == '_text':
service = {'url': reverse('api-search') + '?q=%(q)s', 'label': _('Page Contents')}
else:
service = settings.COMBO_SEARCH_SERVICES.get(service_slug)
service = engines.get(service_slug)
if service and service.get('url'):
service['slug'] = service_slug
services.append(service)

View File

@ -15,7 +15,18 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from django.apps import AppConfig
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _
class DataConfig(AppConfig):
name = 'combo.data'
verbose_name = 'data'
def ready(self):
# register built-in search engine for page contents
from combo.apps.search import engines
engines.register('_text',
url=reverse('api-search') + '?q=%(q)s',
label=_('Page Contents')
)

View File

@ -40,7 +40,7 @@ class SearchServices(object):
settings.COMBO_SEARCH_SERVICES = self.search_services
def __exit__(self, *args, **kwargs):
delattr(settings, 'COMBO_SEARCH_SERVICES')
settings.COMBO_SEARCH_SERVICES = {}
def test_search_cell(app):
with SearchServices(SEARCH_SERVICES):