search: don't index invalid cells (#52418)
This commit is contained in:
parent
d66fb50680
commit
1cfd7e90c4
|
@ -71,6 +71,8 @@ def index_site():
|
|||
for v in validity_info_list
|
||||
if v.object_id == cell.pk and v.content_type.model_class() == cell.__class__
|
||||
]
|
||||
if cell.is_hidden_because_invalid():
|
||||
continue
|
||||
cell_type = ContentType.objects.get_for_model(cell)
|
||||
indexed_cell = IndexedCell(cell_type=cell_type, cell_pk=cell.id)
|
||||
try:
|
||||
|
|
|
@ -1083,15 +1083,17 @@ class CellBase(six.with_metaclass(CellMeta, models.Model)):
|
|||
return True
|
||||
return False
|
||||
|
||||
def is_hidden_because_invalid(self):
|
||||
validity_info = self.get_validity_info()
|
||||
return (
|
||||
validity_info is not None
|
||||
and validity_info.invalid_datetime
|
||||
and validity_info.invalid_datetime <= now()
|
||||
)
|
||||
|
||||
def is_visible(self, user=None, check_validity_info=True):
|
||||
if check_validity_info:
|
||||
validity_info = self.get_validity_info()
|
||||
if (
|
||||
validity_info is not None
|
||||
and validity_info.invalid_datetime
|
||||
and validity_info.invalid_datetime <= now()
|
||||
):
|
||||
return False
|
||||
if check_validity_info and self.is_hidden_because_invalid():
|
||||
return False
|
||||
return element_is_visible(self, user=user)
|
||||
|
||||
def is_relevant(self, context):
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import datetime
|
||||
import json
|
||||
import os
|
||||
import pytest
|
||||
|
@ -12,6 +13,7 @@ from django.test.client import RequestFactory
|
|||
from django.test.utils import CaptureQueriesContext
|
||||
from django.urls import reverse
|
||||
from django.utils.http import urlencode
|
||||
from django.utils.timezone import now
|
||||
|
||||
from requests.exceptions import ConnectionError
|
||||
from requests.models import Response
|
||||
|
@ -1155,6 +1157,28 @@ def test_index_site_inactive_placeholder(app):
|
|||
assert IndexedCell.objects.count() == 0
|
||||
|
||||
|
||||
def test_index_site_invalid_cell(app):
|
||||
page = Page.objects.create(title='page', slug='example-page')
|
||||
cell = TextCell.objects.create(page=page, placeholder='content', text='<p>foobar</p>', order=0)
|
||||
|
||||
index_site()
|
||||
assert IndexedCell.objects.count() == 1
|
||||
|
||||
# invalid cell since just now
|
||||
cell.mark_as_invalid(reason_code='foobar')
|
||||
assert cell.is_visible() is True
|
||||
index_site()
|
||||
assert IndexedCell.objects.count() == 1
|
||||
|
||||
# invalid cell since two days
|
||||
validity_info = cell.get_validity_info()
|
||||
validity_info.invalid_since = now() - datetime.timedelta(days=2)
|
||||
validity_info.save()
|
||||
assert cell.is_visible() is False
|
||||
index_site()
|
||||
assert IndexedCell.objects.count() == 0
|
||||
|
||||
|
||||
def test_index_site_num_queries(settings, app):
|
||||
group = Group.objects.create(name='plop')
|
||||
for i in range(0, 10):
|
||||
|
|
Loading…
Reference in New Issue