0001-search-index-external-links-data-independantly-58269.patch
combo/apps/search/utils.py | ||
---|---|---|
48 | 48 |
# populate ContentType cache |
49 | 49 |
ContentType.objects.get_for_models(*cell_classes) |
50 | 50 |
IndexedCell.objects.all().delete() |
51 |
external_urls = {} |
|
52 | 51 |
validity_info_list = list(ValidityInfo.objects.select_related('content_type')) |
53 | 52 |
pages_by_pk = { |
54 | 53 |
p.pk: p for p in (Page.objects.prefetch_related(Prefetch('groups', to_attr='prefetched_groups'))) |
... | ... | |
90 | 89 | |
91 | 90 |
for link_data in cell.get_external_links_data(): |
92 | 91 |
# index external links |
93 |
indexed_cell = external_urls.get(link_data.get('url')) |
|
94 |
if indexed_cell is None: |
|
95 |
# create an entry for that link. |
|
96 |
indexed_cell = IndexedCell( |
|
97 |
cell_type=cell_type, |
|
98 |
cell_pk=cell.id, |
|
99 |
page_id=cell.page_id, |
|
100 |
public_access=bool(cell.page.public and cell.public), |
|
101 |
url=link_data['url'], |
|
102 |
title=link_data['title'], |
|
103 |
indexed_text=link_data.get('text') or '', |
|
104 |
) |
|
105 |
indexed_cell.save() |
|
106 |
set_cell_groups(indexed_cell, cell) |
|
107 |
external_urls[indexed_cell.url] = indexed_cell |
|
108 |
else: |
|
109 |
# if that link already exists, add detailed texts |
|
110 |
indexed_cell.indexed_text += ' ' + link_data['title'] |
|
111 |
indexed_cell.indexed_text += ' ' + (link_data.get('text') or '') |
|
112 |
indexed_cell.save() |
|
92 |
indexed_cell = IndexedCell( |
|
93 |
cell_type=cell_type, |
|
94 |
cell_pk=cell.id, |
|
95 |
page_id=cell.page_id, |
|
96 |
public_access=bool(cell.page.public and cell.public), |
|
97 |
url=link_data['url'], |
|
98 |
title=link_data['title'], |
|
99 |
indexed_text=link_data.get('text') or '', |
|
100 |
) |
|
101 |
indexed_cell.save() |
|
102 |
set_cell_groups(indexed_cell, cell) |
|
113 | 103 | |
114 | 104 | |
115 | 105 |
def search_site(request, query, pages=None, with_description=None): |
tests/test_search.py | ||
---|---|---|
1430 | 1430 |
assert hits[0]['text'] == page_of_interest.title |
1431 | 1431 |
assert hits[0]['url'] == f'/{page_of_interest.slug}/' |
1432 | 1432 |
assert hits[0]['rank'] > hits[1]['rank'] |
1433 | ||
1434 | ||
1435 |
def test_search_same_link_multiple_pages(settings, app): |
|
1436 |
settings.KNOWN_SERVICES = {} |
|
1437 | ||
1438 |
page = Page.objects.create(title='first page', slug='one') |
|
1439 |
LinkCell.objects.create( |
|
1440 |
title='foobar', url='http://example.net', page=page, placeholder='content', order=0 |
|
1441 |
) |
|
1442 |
second_page = Page.objects.create(title='second page', slug='two') |
|
1443 |
LinkCell.objects.create( |
|
1444 |
title='barfoo', url='http://example.net', page=second_page, placeholder='content', order=0 |
|
1445 |
) |
|
1446 | ||
1447 |
index_site() |
|
1448 | ||
1449 |
cell = SearchCell.objects.create( |
|
1450 |
page=page, placeholder='content', _search_services={'data': ['_text_page_one']}, order=1 |
|
1451 |
) |
|
1452 | ||
1453 |
resp = app.get('/ajax/search/%s/_text_page_one/?q=foobar' % cell.pk, status=200) |
|
1454 |
assert resp.text.count('<li') == 1 |
|
1455 | ||
1456 |
cell._search_services = {'data': ['_text_page_two']} |
|
1457 |
cell.save() |
|
1458 | ||
1459 |
resp = app.get('/ajax/search/%s/_text_page_two/?q=barfoo' % cell.pk, status=200) |
|
1460 |
assert resp.text.count('<li') == 1 |
|
1433 |
- |