make external search async
This commit is contained in:
parent
a5a3d2f193
commit
ebc33ce352
6 changed files with 79 additions and 42 deletions
|
@ -5,3 +5,8 @@ if settings.SEARCH_BACKEND == 'MEILISEARCH':
|
|||
from .search.meilisearch import Indexer
|
||||
elif settings.SEARCH_BACKEND == 'TYPESENSE':
|
||||
from .search.typesense import Indexer
|
||||
else:
|
||||
class Indexer:
|
||||
@classmethod
|
||||
def update_model_indexable(self, cls):
|
||||
pass
|
||||
|
|
48
common/templates/common/external_search_result.html
Normal file
48
common/templates/common/external_search_result.html
Normal file
|
@ -0,0 +1,48 @@
|
|||
{% load static %}
|
||||
{% load i18n %}
|
||||
{% load l10n %}
|
||||
{% load humanize %}
|
||||
{% load admin_url %}
|
||||
{% load mastodon %}
|
||||
{% load oauth_token %}
|
||||
{% load truncate %}
|
||||
{% load highlight %}
|
||||
{% load thumb %}
|
||||
|
||||
{% for item in external_items %}
|
||||
<li class="entity-list__entity">
|
||||
<div class="entity-list__entity-img-wrapper">
|
||||
<a href="{{ item.link }}">
|
||||
<img src="{{ item.cover_url }}" alt="" class="entity-list__entity-img">
|
||||
</a>
|
||||
</div>
|
||||
<div class="entity-list__entity-text">
|
||||
<div class="entity-list__entity-title" style="font-style:italic;">
|
||||
<a href="{{ item.link }}" class="entity-list__entity-link">
|
||||
{% if request.GET.q %}
|
||||
{{ item.title | highlight:request.GET.q }}
|
||||
{% else %}
|
||||
{{ item.title }}
|
||||
{% endif %}
|
||||
</a>
|
||||
|
||||
{% if not request.GET.c or not request.GET.c in categories %}
|
||||
<span class="entity-list__entity-category">[{{item.verbose_category_name}}]</span>
|
||||
{% endif %}
|
||||
<a href="{{ item.source_url }}">
|
||||
<span class="source-label source-label__{{ item.source_site }}">{{ item.source_site.label }}</span>
|
||||
</a>
|
||||
</div>
|
||||
|
||||
<span class="entity-list__entity-info entity-list__entity-info--full-length">
|
||||
{{item.subtitle}}
|
||||
</span>
|
||||
<p class="entity-list__entity-brief">
|
||||
{{ item.brief }}
|
||||
</p>
|
||||
<div class="tag-collection">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</li>
|
||||
{% endfor %}
|
|
@ -16,6 +16,7 @@
|
|||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>{{ site_name }} - {% trans '搜索结果' %}</title>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
|
||||
<script src="https://unpkg.com/htmx.org@1.6.1"></script>
|
||||
<script src="{% static 'lib/js/rating-star.js' %}"></script>
|
||||
<script src="{% static 'js/rating-star-readonly.js' %}"></script>
|
||||
<link rel="stylesheet" href="{% static 'css/boofilsic.min.css' %}">
|
||||
|
@ -49,51 +50,18 @@
|
|||
{% trans '无站内条目匹配' %}
|
||||
</li>
|
||||
{% endfor %}
|
||||
|
||||
{% for item in external_items %}
|
||||
<li class="entity-list__entity">
|
||||
<div class="entity-list__entity-img-wrapper">
|
||||
<a href="{{ item.link }}">
|
||||
<img src="{{ item.cover_url }}" alt="" class="entity-list__entity-img">
|
||||
</a>
|
||||
</div>
|
||||
<div class="entity-list__entity-text">
|
||||
<div class="entity-list__entity-title" style="font-style:italic;">
|
||||
<a href="{{ item.link }}" class="entity-list__entity-link">
|
||||
{% if request.GET.q %}
|
||||
{{ item.title | highlight:request.GET.q }}
|
||||
{% else %}
|
||||
{{ item.title }}
|
||||
{% endif %}
|
||||
</a>
|
||||
|
||||
{% if not request.GET.c or not request.GET.c in categories %}
|
||||
<span class="entity-list__entity-category">[{{item.verbose_category_name}}]</span>
|
||||
{% endif %}
|
||||
<a href="{{ item.source_url }}">
|
||||
<span class="source-label source-label__{{ item.source_site }}">{{ item.source_site.label }}</span>
|
||||
</a>
|
||||
</div>
|
||||
|
||||
<span class="entity-list__entity-info entity-list__entity-info--full-length">
|
||||
{{item.subtitle}}
|
||||
</span>
|
||||
<p class="entity-list__entity-brief">
|
||||
{{ item.brief }}
|
||||
</p>
|
||||
<div class="tag-collection">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</li>
|
||||
{% endfor %}
|
||||
{% if request.GET.q %}
|
||||
<li class="entity-list__entity" hx-get="{% url 'common:external_search' %}?q={{ request.GET.q }}&c={{ request.GET.c }}&page={% if pagination.current_page %}{{ pagination.current_page }}{% else %}1{% endif %}" hx-trigger="revealed" hx-swap="outerHTML">
|
||||
{% trans '正在实时搜索站外条目' %}
|
||||
</li>
|
||||
{% endif %}
|
||||
</ul>
|
||||
</div>
|
||||
<div class="pagination" >
|
||||
|
||||
{% if pagination.has_prev %}
|
||||
<a href="?page=1&{% if request.GET.q %}q={{ request.GET.q }}{% elif request.GET.tag %}tag={{ request.GET.tag }}{% endif %}" class="pagination__nav-link pagination__nav-link">«</a>
|
||||
<a href="?page={{ items.previous_page_number }}&{% if request.GET.q %}q={{ request.GET.q }}{% elif request.GET.tag %}tag={{ request.GET.tag }}{% endif %}" class="pagination__nav-link pagination__nav-link--right-margin pagination__nav-link">‹</a>
|
||||
<a href="?page={{ pagination.previous_page }}&{% if request.GET.q %}q={{ request.GET.q }}{% elif request.GET.tag %}tag={{ request.GET.tag }}{% endif %}" class="pagination__nav-link pagination__nav-link--right-margin pagination__nav-link">‹</a>
|
||||
{% endif %}
|
||||
|
||||
{% for page in pagination.page_range %}
|
||||
|
@ -107,7 +75,7 @@
|
|||
{% endfor %}
|
||||
|
||||
{% if pagination.has_next %}
|
||||
<a href="?page={{ items.next_page_number }}&{% if request.GET.q %}q={{ request.GET.q }}{% elif request.GET.tag %}tag={{ request.GET.tag }}{% endif %}" class="pagination__nav-link pagination__nav-link--left-margin">›</a>
|
||||
<a href="?page={{ pagination.next_page }}&{% if request.GET.q %}q={{ request.GET.q }}{% elif request.GET.tag %}tag={{ request.GET.tag }}{% endif %}" class="pagination__nav-link pagination__nav-link--left-margin">›</a>
|
||||
<a href="?page={{ pagination.last_page }}&{% if request.GET.q %}q={{ request.GET.q }}{% elif request.GET.tag %}tag={{ request.GET.tag }}{% endif %}" class="pagination__nav-link">»</a>
|
||||
{% endif %}
|
||||
|
||||
|
|
|
@ -6,4 +6,5 @@ urlpatterns = [
|
|||
path('', home),
|
||||
path('home/', home, name='home'),
|
||||
path('search/', search, name='search'),
|
||||
path('external_search/', external_search, name='external_search'),
|
||||
]
|
||||
|
|
|
@ -10,6 +10,8 @@ class PageLinksGenerator:
|
|||
def __init__(self, length, current_page, total_pages):
|
||||
current_page = int(current_page)
|
||||
self.current_page = current_page
|
||||
self.previous_page = current_page - 1 if current_page > 1 else None
|
||||
self.next_page = current_page + 1 if current_page < total_pages else None
|
||||
self.start_page = None
|
||||
self.end_page = None
|
||||
self.page_range = None
|
||||
|
|
|
@ -35,6 +35,21 @@ def home(request):
|
|||
return user_home(request, request.user.id)
|
||||
|
||||
|
||||
@login_required
|
||||
def external_search(request):
|
||||
category = request.GET.get("c", default='all').strip().lower()
|
||||
if category == 'all':
|
||||
category = None
|
||||
keywords = request.GET.get("q", default='').strip()
|
||||
page_number = int(request.GET.get('page', default=1))
|
||||
return render(
|
||||
request,
|
||||
"common/external_search_result.html",
|
||||
{
|
||||
"external_items": ExternalSources.search(category, keywords, page_number) if keywords else [],
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
def search(request):
|
||||
|
@ -71,7 +86,6 @@ def search(request):
|
|||
{
|
||||
"items": result.items,
|
||||
"pagination": PageLinksGenerator(PAGE_LINK_NUMBER, page_number, result.num_pages),
|
||||
"external_items": ExternalSources.search(category, keywords, page_number),
|
||||
"categories": ['book', 'movie', 'music', 'game'],
|
||||
}
|
||||
)
|
||||
|
@ -348,7 +362,6 @@ def search2(request):
|
|||
"common/search_result.html",
|
||||
{
|
||||
"items": items,
|
||||
"external_items": ExternalSources.search(category, input_string, page_number),
|
||||
"categories": categories,
|
||||
}
|
||||
)
|
||||
|
|
Loading…
Add table
Reference in a new issue