implement playlist earch form

This commit is contained in:
simon 2021-11-15 13:08:32 +07:00
parent 2fe8936e85
commit d2414434fb
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4
3 changed files with 75 additions and 16 deletions

View File

@ -86,6 +86,15 @@ class ChannelSearchForm(forms.Form):
)
class PlaylistSearchForm(forms.Form):
"""search for playlists"""
searchInput = forms.CharField(
label="",
widget=forms.TextInput(attrs={"autocomplete": "off"}),
)
class AddToQueueForm(forms.Form):
"""text area form to add to downloads"""

View File

@ -25,11 +25,9 @@
<img src="{% static 'img/icon-search.svg' %}" alt="search-icon" onclick="showSearch()">
<p>Search your Playlists</p>
</div>
<form onSubmit="return playlistRedirect();" id="search-box">
<form action="/playlist/" method="POST" id="search-box">
{% csrf_token %}
{{ search_form }}
<datalist id="resultBox">
</datalist>
</form>
</div>
</div>

View File

@ -20,6 +20,7 @@ from home.forms import (
ApplicationSettingsForm,
ChannelSearchForm,
CustomAuthForm,
PlaylistSearchForm,
SubscribeToChannelForm,
UserSettingsForm,
VideoSearchForm,
@ -717,10 +718,39 @@ class PlaylistView(View):
"""handle http get requests"""
user_id = request.user.id
view_config = self.read_config(user_id=user_id)
# handle search
search_get = request.GET.get("search", False)
if search_get:
search_encoded = urllib.parse.quote(search_get)
else:
search_encoded = False
# define page size
page_get = int(request.GET.get("page", 0))
pagination_handler = Pagination(page_get, user_id)
pagination_handler = Pagination(
page_get, user_id, search_get=search_encoded
)
url = view_config["es_url"] + "/ta_playlist/_search"
data = self.build_data(pagination_handler, search_get)
search = SearchHandler(url, data)
playlist_hits = search.get_data()
pagination_handler.validate(search.max_hits)
search_form = PlaylistSearchForm()
context = {
"search_form": search_form,
"title": "Playlists",
"colors": view_config["colors"],
"pagination": pagination_handler.pagination,
"playlists": playlist_hits,
"view_style": view_config["view_style"],
}
return render(request, "home/playlist.html", context)
@staticmethod
def build_data(pagination_handler, search_get):
"""build data object for query"""
data = {
"size": pagination_handler.pagination["page_size"],
"from": pagination_handler.pagination["page_from"],
@ -729,18 +759,28 @@ class PlaylistView(View):
},
"sort": [{"playlist_name.keyword": {"order": "asc"}}],
}
search = SearchHandler(url, data)
playlist_hits = search.get_data()
pagination_handler.validate(search.max_hits)
context = {
"title": "Playlists",
"colors": view_config["colors"],
"pagination": pagination_handler.pagination,
"playlists": playlist_hits,
"view_style": view_config["view_style"],
}
return render(request, "home/playlist.html", context)
if search_get:
data["query"] = {
"bool": {
"should": [
{
"multi_match": {
"query": search_get,
"fields": [
"playlist_channel_id",
"playlist_channel",
"playlist_name",
],
}
}
],
"filter": [
{"term": {"playlist_entries.downloaded": True}}
],
"minimum_should_match": 1,
}
}
return data
@staticmethod
def read_config(user_id):
@ -758,6 +798,18 @@ class PlaylistView(View):
}
return view_config
@staticmethod
def post(request):
"""handle post from search form"""
search_form = PlaylistSearchForm(data=request.POST)
if search_form.is_valid():
search_query = request.POST.get("searchInput")
print(search_query)
search_url = "/playlist/?" + urlencode({"search": search_query})
return redirect(search_url, permanent=True)
return redirect("playlist")
class VideoView(View):
"""resolves to /video/<video-id>/