validate search forms

This commit is contained in:
simon 2021-10-30 13:19:16 +07:00
parent a8ded25b35
commit 266d3703cd
6 changed files with 45 additions and 9 deletions

View File

@ -60,3 +60,27 @@ class ApplicationSettingsForm(forms.Form):
downloads_add_thumbnail = forms.ChoiceField( downloads_add_thumbnail = forms.ChoiceField(
widget=forms.Select, choices=THUMBNAIL_CHOICES, required=False widget=forms.Select, choices=THUMBNAIL_CHOICES, required=False
) )
class VideoSearchForm(forms.Form):
"""search videos form"""
searchInput = forms.CharField(
label="Search your videos",
widget=forms.TextInput(attrs={"autocomplete": "off"}),
)
class ChannelSearchForm(forms.Form):
"""search for channels"""
searchInput = forms.CharField(
label="",
widget=forms.TextInput(
attrs={
"oninput": "searchChannels(this.value)",
"autocomplete": "off",
"list": "resultBox",
}
),
)

View File

@ -20,11 +20,12 @@
<div class="search-form icon-text"> <div class="search-form icon-text">
<div class="search-icon"> <div class="search-icon">
<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 Channels</p> <p>Search your Channels</p>
</div> </div>
<form onSubmit="return channelRedirect();" id="search-box"> <form onSubmit="return channelRedirect();" id="search-box">
{% csrf_token %} {% csrf_token %}
<input name="videoSearch" list="resultBox" type="text" id="searchInput" autocomplete="off" oninput="searchChannels(this.value)"> <!-- <input name="videoSearch" list="resultBox" type="text" id="searchInput" autocomplete="off" oninput="searchChannels(this.value)"> -->
{{ search_form }}
<datalist id="resultBox"> <datalist id="resultBox">
</datalist> </datalist>
</form> </form>

View File

@ -34,7 +34,7 @@
</div> </div>
<form action="/" method="POST" id="search-box"> <form action="/" method="POST" id="search-box">
{% csrf_token %} {% csrf_token %}
<input name="videoSearch" list="resultBox" type="text" id="searchInput" autocomplete="off"> {{ search_form }}
</form> </form>
</div> </div>
</div> </div>

View File

@ -17,8 +17,10 @@ from django.utils.http import urlencode
from django.views import View from django.views import View
from home.forms import ( from home.forms import (
ApplicationSettingsForm, ApplicationSettingsForm,
ChannelSearchForm,
CustomAuthForm, CustomAuthForm,
UserSettingsForm, UserSettingsForm,
VideoSearchForm,
) )
from home.src.config import AppConfig from home.src.config import AppConfig
from home.src.download import ChannelSubscription, PendingList from home.src.download import ChannelSubscription, PendingList
@ -75,7 +77,10 @@ class HomeView(View):
videos_hits = search.get_data() videos_hits = search.get_data()
max_hits = search.max_hits max_hits = search.max_hits
pagination_handler.validate(max_hits) pagination_handler.validate(max_hits)
search_form = VideoSearchForm()
context = { context = {
"search_form": search_form,
"videos": videos_hits, "videos": videos_hits,
"pagination": pagination_handler.pagination, "pagination": pagination_handler.pagination,
"sort_by": view_config["sort_by"], "sort_by": view_config["sort_by"],
@ -157,10 +162,14 @@ class HomeView(View):
@staticmethod @staticmethod
def post(request): def post(request):
"""handle post from search form""" """handle post from search form"""
post_data = dict(request.POST) search_form = VideoSearchForm(data=request.POST)
search_query = post_data["videoSearch"][0] if search_form.is_valid():
search_url = "/?" + urlencode({"search": search_query}) search_query = request.POST.get("searchInput")
return redirect(search_url, permanent=True) print(search_query)
search_url = "/?" + urlencode({"search": search_query})
return redirect(search_url, permanent=True)
return redirect("home")
class LoginView(View): class LoginView(View):
@ -469,7 +478,9 @@ class ChannelView(View):
channel_hits = search.get_data() channel_hits = search.get_data()
max_hits = search.max_hits max_hits = search.max_hits
pagination_handler.validate(search.max_hits) pagination_handler.validate(search.max_hits)
search_form = ChannelSearchForm()
context = { context = {
"search_form": search_form,
"channels": channel_hits, "channels": channel_hits,
"max_hits": max_hits, "max_hits": max_hits,
"pagination": pagination_handler.pagination, "pagination": pagination_handler.pagination,

View File

@ -62,7 +62,7 @@ ul {
margin-left: 20px; margin-left: 20px;
} }
td, span { td, span, label {
font-family: Sen-Regular, sans-serif; font-family: Sen-Regular, sans-serif;
color: var(--main-font); color: var(--main-font);
} }

View File

@ -372,7 +372,7 @@ function showSearch() {
} else { } else {
searchBox.style.display = ""; searchBox.style.display = "";
} }
var inputBox = document.getElementById('searchInput'); var inputBox = document.getElementById('id_searchInput');
inputBox.focus(); inputBox.focus();
} }