search: don't index cells with an inactive placeholder (#40252)

This commit is contained in:
Lauréline Guérin 2020-03-27 17:58:23 +01:00
parent ee82f0e26e
commit 5bc694d32d
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
2 changed files with 36 additions and 17 deletions

View File

@ -61,10 +61,13 @@ def index_site():
.prefetch_related(
Prefetch('groups', to_attr='prefetched_groups')))
for cell in queryset:
cell.page = pages_by_pk.get(cell.page_id)
# exclude cells with an inactive placeholder
if not cell.is_placeholder_active():
continue
cell.prefetched_validity_info = [
v for v in validity_info_list
if v.object_id == cell.pk and v.content_type.model_class() == cell.__class__]
cell.page = pages_by_pk.get(cell.page_id)
cell_type = ContentType.objects.get_for_model(cell)
indexed_cell = IndexedCell(cell_type=cell_type, cell_pk=cell.id)
try:

View File

@ -251,7 +251,7 @@ def test_search_contents_index():
page.public = True
page.save()
cell = TextCell(page=page, text='<p>foobar</p>', order=0)
cell = TextCell(page=page, placeholder='content', text='<p>foobar</p>', order=0)
cell.save()
request = RequestFactory().get('/')
@ -268,7 +268,7 @@ def test_search_contents_technical_placeholder():
page.save()
TextCell(page=page, text='<p>foobar</p>', order=0, placeholder='_off').save()
TextCell(page=page, text='<p>barfoo</p>', order=0, placeholder='on').save()
TextCell(page=page, text='<p>barfoo</p>', order=0, placeholder='content').save()
request = RequestFactory().get('/')
request.user = AnonymousUser()
@ -283,17 +283,17 @@ def test_search_api(app):
page = Page(title='example page', slug='example-page')
page.save()
cell = TextCell(page=page, text='<p>foobar baz</p>', order=0)
cell = TextCell(page=page, placeholder='content', text='<p>foobar baz</p>', order=0)
cell.save()
second_page = Page(title='second page', slug='second-page')
second_page.save()
cell = TextCell(page=second_page, text='<p>other baz</p>', order=0)
cell = TextCell(page=second_page, placeholder='content', text='<p>other baz</p>', order=0)
cell.save()
index_site()
cell = SearchCell(page=page, _search_services={'data': ['_text']}, order=0)
cell = SearchCell(page=page, placeholder='content', _search_services={'data': ['_text']}, order=0)
cell.save()
resp = app.get('/ajax/search/%s/_text/?q=foobar' % cell.id, status=200)
@ -315,7 +315,7 @@ def test_search_external_links(app):
page = Page(title='example page', slug='example-page')
page.save()
cell = SearchCell(page=page, _search_services={'data': ['_text']}, order=0)
cell = SearchCell(page=page, placeholder='content', _search_services={'data': ['_text']}, order=0)
cell.save()
index_site()
@ -324,7 +324,7 @@ def test_search_external_links(app):
hits = search_site(request, 'foobar')
assert len(hits) == 0
LinkCell(title='foobar', url='http://example.net', page=page, order=0).save()
LinkCell(title='foobar', url='http://example.net', page=page, placeholder='content', order=0).save()
index_site()
hits = search_site(request, 'foobar')
@ -333,7 +333,7 @@ def test_search_external_links(app):
assert hits[0]['url'] == 'http://example.net'
# second link with same target
LinkCell(title='baz', url='http://example.net', page=page, order=0).save()
LinkCell(title='baz', url='http://example.net', page=page, placeholder='content', order=0).save()
index_site()
# add a second link with the same target
@ -438,8 +438,8 @@ def test_private_search(app):
page = Page(title='example page', slug='example-page')
page.save()
TextCell(page=page, text='<p>foobar</p>', order=0, public=False).save()
TextCell(page=page, text='<p>barfoo</p>', order=0, public=True).save()
TextCell(page=page, placeholder='content', text='<p>foobar</p>', order=0, public=False).save()
TextCell(page=page, placeholder='content', text='<p>barfoo</p>', order=0, public=True).save()
request = RequestFactory().get('/')
request.user = AnonymousUser()
@ -463,10 +463,10 @@ def test_restricted_search(app):
page = Page(title='example page', slug='example-page')
page.save()
cell = TextCell(page=page, text='<p>foobar</p>', order=0, public=False)
cell = TextCell(page=page, placeholder='content', text='<p>foobar</p>', order=0, public=False)
cell.save()
cell.groups.set([group])
TextCell(page=page, text='<p>barfoo</p>', order=0, public=False).save()
TextCell(page=page, placeholder='content', text='<p>barfoo</p>', order=0, public=False).save()
index_site()
# first cell is restricted, it's not found
@ -507,11 +507,11 @@ def test_restricted_search(app):
def test_no_sub_slug_search(app):
page = Page(title='example page', slug='example-page')
page.save()
TextCell(page=page, text='<p>foobar</p>', order=0, public=True).save()
TextCell(page=page, placeholder='content', text='<p>foobar</p>', order=0, public=True).save()
page = Page(title='example page with sub_slug', slug='sub-slugged-page',
sub_slug='(?P<foo>\d+)')
page.save()
TextCell(page=page, text='<p>barfoo</p>', order=0, public=True).save()
TextCell(page=page, placeholder='content', text='<p>barfoo</p>', order=0, public=True).save()
request = RequestFactory().get('/')
request.user = AnonymousUser()
@ -522,16 +522,32 @@ def test_no_sub_slug_search(app):
assert len(hits) == 0
def test_index_site_inactive_placeholder(app):
page = Page.objects.create(title='page', slug='example-page')
cell = TextCell.objects.create(page=page, placeholder='content', text='<p>foobar</p>', order=0)
assert cell.is_placeholder_active() is True
index_site()
assert IndexedCell.objects.count() == 1
cell.placeholder = ''
cell.save()
assert cell.is_placeholder_active() is False
index_site()
assert IndexedCell.objects.count() == 0
def test_index_site_num_queries(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)
page.groups.set([group])
for j in range(0, 5):
cell = TextCell.objects.create(page=page, text='<p>foobar %s</p>' % j, order=j, public=False)
cell = TextCell.objects.create(page=page, placeholder='content', text='<p>foobar %s</p>' % j, order=j, public=False)
cell.groups.set([group])
index_site() # populate cache
assert IndexedCell.objects.count() == 50
with CaptureQueriesContext(connection) as ctx:
index_site()
assert len(ctx.captured_queries) == 195
assert len(ctx.captured_queries) == 225