2020-11-18 22:54:13 +00:00
|
|
|
from django.contrib import messages
|
2020-11-17 23:42:57 +00:00
|
|
|
|
2020-11-18 22:54:13 +00:00
|
|
|
from archivebox.search import query_search_index
|
2020-11-17 23:42:57 +00:00
|
|
|
|
2021-04-06 03:21:07 +00:00
|
|
|
class SearchResultsAdminMixin:
|
|
|
|
def get_search_results(self, request, queryset, search_term: str):
|
|
|
|
"""Enhances the search queryset with results from the search backend"""
|
|
|
|
|
|
|
|
qs, use_distinct = super().get_search_results(request, queryset, search_term)
|
2020-11-17 23:42:57 +00:00
|
|
|
|
|
|
|
search_term = search_term.strip()
|
|
|
|
if not search_term:
|
|
|
|
return qs, use_distinct
|
2020-11-18 22:54:13 +00:00
|
|
|
try:
|
2020-11-19 23:19:33 +00:00
|
|
|
qsearch = query_search_index(search_term)
|
2021-04-06 03:21:07 +00:00
|
|
|
qs = qs | qsearch
|
2020-11-18 22:54:13 +00:00
|
|
|
except Exception as err:
|
2021-04-10 12:18:13 +00:00
|
|
|
print(f'[!] Error while using search backend: {err.__class__.__name__} {err}')
|
2020-11-18 22:54:13 +00:00
|
|
|
messages.add_message(request, messages.WARNING, f'Error from the search backend, only showing results from default admin search fields - Error: {err}')
|
2021-04-06 03:21:07 +00:00
|
|
|
|
|
|
|
return qs, use_distinct
|