search: don't search for engines on site indexation (#43228)

This commit is contained in:
Lauréline Guérin 2020-05-22 16:09:31 +02:00
parent 32e714709a
commit 518825f9a4
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
5 changed files with 44 additions and 22 deletions

View File

@ -33,6 +33,7 @@ class LatestPageUpdatesCell(CellBase):
_('Maximum number of entries'), default=10)
template_name = 'combo/latest-page-updates-cell.html'
exclude_from_search = True
class Meta:
verbose_name = _('Latest Page Updates')
@ -48,6 +49,3 @@ class LatestPageUpdatesCell(CellBase):
(x for x in pages.order_by('-last_update_timestamp') if x.is_visible(user=user)),
self.limit)
return extra_context
def render_for_search(self):
return ''

View File

@ -51,6 +51,7 @@ def get_root_page_and_children(service_slug):
class SearchCell(CellBase):
template_name = 'combo/search-cell.html'
manager_form_template = 'combo/manager/search-cell-form.html'
exclude_from_search = True
_search_services = JSONField(_('Search Services'), default=dict, blank=True)
autofocus = models.BooleanField(_('Autofocus'), default=False)
@ -60,7 +61,7 @@ class SearchCell(CellBase):
verbose_name = _('Search')
def is_visible(self, **kwargs):
if not self.search_services:
if not self._search_services.get('data'):
return False
return super(SearchCell, self).is_visible(**kwargs)

View File

@ -54,6 +54,9 @@ def index_site():
Page.objects
.prefetch_related(Prefetch('groups', to_attr='prefetched_groups')))}
for klass in cell_classes:
if getattr(klass, 'exclude_from_search', False) is True:
# do not load cells marked as excluded from search (example: MenuCell, SearchCell, ...)
continue
queryset = (
klass.objects
.filter(page__snapshot__isnull=True, page__sub_slug='')

View File

@ -1104,6 +1104,7 @@ class MenuCell(CellBase):
null=True, blank=True, verbose_name=_('Root Page'))
template_name = 'combo/menu-cell.html'
exclude_from_search = True
class Meta:
verbose_name = _('Menu')
@ -1120,9 +1121,6 @@ class MenuCell(CellBase):
ignore_visibility=False)
return ctx
def render_for_search(self):
return ''
@register_cell_class
class LinkCell(CellBase):
@ -1252,6 +1250,7 @@ class LinkListCell(CellBase):
template_name = 'combo/link-list-cell.html'
manager_form_template = 'combo/manager/link-list-cell-form.html'
children_placeholder_prefix = '_linkslist:'
exclude_from_search = True
invalid_reason_codes = {
'data_link_invalid': _('Invalid link'),
@ -1304,9 +1303,6 @@ class LinkListCell(CellBase):
from .forms import LinkListCellForm
return LinkListCellForm
def render_for_search(self):
return ''
def export_subobjects(self):
return {'links': json.loads(
serializers.serialize(

View File

@ -297,17 +297,15 @@ def test_search_custom_templates(app):
assert '<div>description A</div>' in resp.text
def test_search_cell_visibility(app):
page = Page(title='example page', slug='example-page')
page.save()
def test_search_cell_visibility(settings, app):
page = Page.objects.create(title='example page', slug='example-page')
settings.COMBO_SEARCH_SERVICES = SEARCH_SERVICES
with SearchServices(SEARCH_SERVICES):
cell = SearchCell(page=page, order=0)
assert not cell.is_visible()
cell = SearchCell(page=page, order=0)
assert not cell.is_visible()
cell._search_services = {'data': ['_text']}
del cell.search_services # clear cache
assert cell.is_visible()
cell._search_services = {'data': ['_text']}
assert cell.is_visible()
def test_search_contents():
@ -328,7 +326,8 @@ def test_search_contents():
# no indexation of menu cells
cell = MenuCell(page=page, order=0)
assert cell.render_for_search() == ''
assert cell.exclude_from_search is True
def test_search_contents_index():
page = Page(title='example page', slug='example-page')
@ -596,6 +595,7 @@ def test_manager_search_cell_order(settings, app, admin_user):
def test_manager_waiting_index_message(app, admin_user):
page = Page.objects.create(title='One', slug='one', template_name='standard')
cell = SearchCell.objects.create(page=page, placeholder='content', order=0)
TextCell.objects.create(page=page, placeholder='content', text='<p>foobar</p>', order=0)
app = login(app)
resp = app.get('/manage/pages/%s/' % page.pk)
assert 'Content indexing has been scheduled' not in resp.text
@ -775,7 +775,7 @@ def test_index_site_inactive_placeholder(app):
assert IndexedCell.objects.count() == 0
def test_index_site_num_queries(app):
def test_index_site_num_queries(settings, app):
group = Group.objects.create(name='plop')
for i in range(0, 10):
page = Page.objects.create(title='page %s' % i, slug='example-page-%s' % i)
@ -788,4 +788,28 @@ def test_index_site_num_queries(app):
assert IndexedCell.objects.count() == 50
with CaptureQueriesContext(connection) as ctx:
index_site()
assert len(ctx.captured_queries) == 225
assert len(ctx.captured_queries) == 221
SearchCell.objects.create(
page=page, placeholder='content', order=0,
_search_services={'data': ['search1']})
# search cells are not indexed
index_site()
assert IndexedCell.objects.count() == 50
@mock.patch('combo.apps.search.engines.get_engines')
def test_index_site_search_engines_load(get_engines_mock, settings, app):
# be sure that get_engines is not called during page indexation
get_engines_mock.side_effect = Exception
settings.COMBO_SEARCH_SERVICES = {}
page = Page.objects.create(title='page', slug='example-page')
SearchCell.objects.create(
page=page, placeholder='content', order=0,
_search_services={'data': ['search1']})
TextCell.objects.create(page=page, placeholder='content', text='<p>foobar</p>', order=0)
# no exception raised
index_site()