search: add a singleton so apps can register their search engines (#25620)
This commit is contained in:
parent
d4f0d28e6e
commit
c0031f62d2
|
@ -17,6 +17,9 @@
|
||||||
import django.apps
|
import django.apps
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
|
||||||
|
from .engines import engines
|
||||||
|
|
||||||
|
|
||||||
class AppConfig(django.apps.AppConfig):
|
class AppConfig(django.apps.AppConfig):
|
||||||
name = 'combo.apps.search'
|
name = 'combo.apps.search'
|
||||||
verbose_name = _('Search')
|
verbose_name = _('Search')
|
||||||
|
|
|
@ -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
|
|
@ -29,6 +29,8 @@ from combo.data.models import CellBase
|
||||||
from combo.data.library import register_cell_class
|
from combo.data.library import register_cell_class
|
||||||
from combo.utils import get_templated_url
|
from combo.utils import get_templated_url
|
||||||
|
|
||||||
|
from . import engines
|
||||||
|
|
||||||
|
|
||||||
@register_cell_class
|
@register_cell_class
|
||||||
class SearchCell(CellBase):
|
class SearchCell(CellBase):
|
||||||
|
@ -55,10 +57,7 @@ class SearchCell(CellBase):
|
||||||
def search_services(self):
|
def search_services(self):
|
||||||
services = []
|
services = []
|
||||||
for service_slug in self._search_services.get('data') or []:
|
for service_slug in self._search_services.get('data') or []:
|
||||||
if service_slug == '_text':
|
service = engines.get(service_slug)
|
||||||
service = {'url': reverse('api-search') + '?q=%(q)s', 'label': _('Page Contents')}
|
|
||||||
else:
|
|
||||||
service = settings.COMBO_SEARCH_SERVICES.get(service_slug)
|
|
||||||
if service and service.get('url'):
|
if service and service.get('url'):
|
||||||
service['slug'] = service_slug
|
service['slug'] = service_slug
|
||||||
services.append(service)
|
services.append(service)
|
||||||
|
|
|
@ -15,7 +15,18 @@
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
from django.apps import AppConfig
|
from django.apps import AppConfig
|
||||||
|
from django.core.urlresolvers import reverse
|
||||||
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
|
||||||
|
|
||||||
class DataConfig(AppConfig):
|
class DataConfig(AppConfig):
|
||||||
name = 'combo.data'
|
name = 'combo.data'
|
||||||
verbose_name = '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')
|
||||||
|
)
|
||||||
|
|
|
@ -40,7 +40,7 @@ class SearchServices(object):
|
||||||
settings.COMBO_SEARCH_SERVICES = self.search_services
|
settings.COMBO_SEARCH_SERVICES = self.search_services
|
||||||
|
|
||||||
def __exit__(self, *args, **kwargs):
|
def __exit__(self, *args, **kwargs):
|
||||||
delattr(settings, 'COMBO_SEARCH_SERVICES')
|
settings.COMBO_SEARCH_SERVICES = {}
|
||||||
|
|
||||||
def test_search_cell(app):
|
def test_search_cell(app):
|
||||||
with SearchServices(SEARCH_SERVICES):
|
with SearchServices(SEARCH_SERVICES):
|
||||||
|
|
Loading…
Reference in New Issue