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): class AddToQueueForm(forms.Form):
"""text area form to add to downloads""" """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()"> <img src="{% static 'img/icon-search.svg' %}" alt="search-icon" onclick="showSearch()">
<p>Search your Playlists</p> <p>Search your Playlists</p>
</div> </div>
<form onSubmit="return playlistRedirect();" id="search-box"> <form action="/playlist/" method="POST" id="search-box">
{% csrf_token %} {% csrf_token %}
{{ search_form }} {{ search_form }}
<datalist id="resultBox">
</datalist>
</form> </form>
</div> </div>
</div> </div>

View File

@ -20,6 +20,7 @@ from home.forms import (
ApplicationSettingsForm, ApplicationSettingsForm,
ChannelSearchForm, ChannelSearchForm,
CustomAuthForm, CustomAuthForm,
PlaylistSearchForm,
SubscribeToChannelForm, SubscribeToChannelForm,
UserSettingsForm, UserSettingsForm,
VideoSearchForm, VideoSearchForm,
@ -717,10 +718,39 @@ class PlaylistView(View):
"""handle http get requests""" """handle http get requests"""
user_id = request.user.id user_id = request.user.id
view_config = self.read_config(user_id=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)) 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" 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 = { data = {
"size": pagination_handler.pagination["page_size"], "size": pagination_handler.pagination["page_size"],
"from": pagination_handler.pagination["page_from"], "from": pagination_handler.pagination["page_from"],
@ -729,18 +759,28 @@ class PlaylistView(View):
}, },
"sort": [{"playlist_name.keyword": {"order": "asc"}}], "sort": [{"playlist_name.keyword": {"order": "asc"}}],
} }
search = SearchHandler(url, data) if search_get:
playlist_hits = search.get_data() data["query"] = {
pagination_handler.validate(search.max_hits) "bool": {
"should": [
context = { {
"title": "Playlists", "multi_match": {
"colors": view_config["colors"], "query": search_get,
"pagination": pagination_handler.pagination, "fields": [
"playlists": playlist_hits, "playlist_channel_id",
"view_style": view_config["view_style"], "playlist_channel",
} "playlist_name",
return render(request, "home/playlist.html", context) ],
}
}
],
"filter": [
{"term": {"playlist_entries.downloaded": True}}
],
"minimum_should_match": 1,
}
}
return data
@staticmethod @staticmethod
def read_config(user_id): def read_config(user_id):
@ -758,6 +798,18 @@ class PlaylistView(View):
} }
return view_config 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): class VideoView(View):
"""resolves to /video/<video-id>/ """resolves to /video/<video-id>/