Projet

Général

Profil

0002-search-add-a-singleton-so-apps-can-register-their-se.patch

Thomas Noël, 27 août 2018 11:15

Télécharger (4,57 ko)

Voir les différences:

Subject: [PATCH 02/11] search: add a singleton so apps can register their
 search engines (#25620)

 combo/apps/search/__init__.py |  3 +++
 combo/apps/search/engines.py  | 35 +++++++++++++++++++++++++++++++++++
 combo/apps/search/models.py   |  7 +++----
 combo/data/apps.py            | 11 +++++++++++
 tests/test_search.py          |  2 +-
 5 files changed, 53 insertions(+), 5 deletions(-)
 create mode 100644 combo/apps/search/engines.py
combo/apps/search/__init__.py
17 17
import django.apps
18 18
from django.utils.translation import ugettext_lazy as _
19 19

  
20
from .engines import engines
21

  
22

  
20 23
class AppConfig(django.apps.AppConfig):
21 24
    name = 'combo.apps.search'
22 25
    verbose_name = _('Search')
combo/apps/search/engines.py
1
# combo - content management system
2
# Copyright (C) 2014-2018  Entr'ouvert
3
#
4
# This program is free software: you can redistribute it and/or modify it
5
# under the terms of the GNU Affero General Public License as published
6
# by the Free Software Foundation, either version 3 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU Affero General Public License for more details.
13
#
14
# You should have received a copy of the GNU Affero General Public License
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16

  
17
from django.conf import settings
18

  
19

  
20
class Engines(object):
21
    """Singleton object that serves as a registry of classes providing search
22
    engines."""
23

  
24
    def __init__(self):
25
        self.engines = {}
26

  
27
    def register(self, key, **options):
28
        self.engines[key] = options
29

  
30
    def get(self, key):
31
        if key in settings.COMBO_SEARCH_SERVICES:
32
            return settings.COMBO_SEARCH_SERVICES[key]
33
        return self.engines.get(key)
34

  
35
engines = Engines()  # singleton object
combo/apps/search/models.py
29 29
from combo.data.library import register_cell_class
30 30
from combo.utils import get_templated_url
31 31

  
32
from . import engines
33

  
32 34

  
33 35
@register_cell_class
34 36
class SearchCell(CellBase):
......
55 57
    def search_services(self):
56 58
        services = []
57 59
        for service_slug in self._search_services.get('data') or []:
58
            if service_slug == '_text':
59
                service = {'url': reverse('api-search') + '?q=%(q)s', 'label': _('Page Contents')}
60
            else:
61
                service = settings.COMBO_SEARCH_SERVICES.get(service_slug)
60
            service = engines.get(service_slug)
62 61
            if service and service.get('url'):
63 62
                service['slug'] = service_slug
64 63
                services.append(service)
combo/data/apps.py
15 15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 16

  
17 17
from django.apps import AppConfig
18
from django.core.urlresolvers import reverse
19
from django.utils.translation import ugettext_lazy as _
20

  
18 21

  
19 22
class DataConfig(AppConfig):
20 23
    name = 'combo.data'
21 24
    verbose_name = 'data'
25

  
26
    def ready(self):
27
        # register built-in search engine for page contents
28
        from combo.apps.search import engines
29
        engines.register('_text',
30
            url=reverse('api-search') + '?q=%(q)s',
31
            label=_('Page Contents')
32
        )
tests/test_search.py
40 40
        settings.COMBO_SEARCH_SERVICES = self.search_services
41 41

  
42 42
    def __exit__(self, *args, **kwargs):
43
        delattr(settings, 'COMBO_SEARCH_SERVICES')
43
        settings.COMBO_SEARCH_SERVICES = {}
44 44

  
45 45
def test_search_cell(app):
46 46
    with SearchServices(SEARCH_SERVICES):
47
-