feat: server search

This commit is contained in:
eneller
2025-04-05 10:10:41 +02:00
parent 41f7836a48
commit 21d6d265ed
2 changed files with 14 additions and 23 deletions

View File

@@ -15,15 +15,10 @@ books = sorted(get_all_books(), key= lambda b: b.title)# TODO get from pickle
gbnetloc = urlparse(allbooks_url).netloc gbnetloc = urlparse(allbooks_url).netloc
def index(request: HttpRequest): def index(request: HttpRequest):
context = { localbooks = books
'title': 'epub2go',
'http_host': f'http://{ request.META['HTTP_HOST'] }',
'books': books,
'book_count': len(books),
'allbooks_url': allbooks_url,
}
targetParam = request.GET.get('t', None) targetParam = request.GET.get('t', None)
searchParam = request.GET.get('s', None)
if targetParam: if targetParam:
if validateUrl(targetParam): if validateUrl(targetParam):
# download file # download file
@@ -36,9 +31,18 @@ def index(request: HttpRequest):
response['Content-Disposition'] = f'attachment; filename="{fname}"' response['Content-Disposition'] = f'attachment; filename="{fname}"'
return response return response
else: return HttpResponseBadRequest('Input URL invalid.') else: return HttpResponseBadRequest('Input URL invalid.')
else: elif searchParam:
# return base view localbooks = [book for book in books if searchParam in book.title]
return render(request, 'home.html', context)
# return base view
context = {
'title': 'epub2go',
'http_host': f'http://{ request.META['HTTP_HOST'] }',
'books': localbooks,
'book_count': len(books),
'allbooks_url': allbooks_url,
}
return render(request, 'home.html', context)
def validateUrl(param)->bool : def validateUrl(param)->bool :

View File

@@ -15,7 +15,6 @@ document.addEventListener('keydown', (event)=>{
let searchParam = params.get('s'); let searchParam = params.get('s');
if (searchParam){ if (searchParam){
searchInput.value = searchParam; searchInput.value = searchParam;
search(searchParam);
} }
function submitSearch(event){ function submitSearch(event){
@@ -23,15 +22,3 @@ function submitSearch(event){
let path = window.location.pathname + "./?s="+encodeURIComponent(searchInput.value); let path = window.location.pathname + "./?s="+encodeURIComponent(searchInput.value);
window.location.href= path; window.location.href= path;
} }
function search(searchStr = searchInput.value){
searchStr= searchStr.toLowerCase();
function showMatch(tr){
// match search with list
let searchSuccess = Array.from(tr.getElementsByClassName('table-data')).map(e => e.textContent.toLowerCase())
.join(' ')
.indexOf(searchStr) > -1;
if (searchSuccess) tr.style.display = "";
else tr.style.display = "none";
}
table_r.map(showMatch, table_r);
}