From d860e19b544cd3852e4decce3dd37fe2134dc63c Mon Sep 17 00:00:00 2001 From: krateng Date: Wed, 29 Mar 2023 15:31:04 +0200 Subject: [PATCH] Added albums to search --- maloja/apis/native_v1.py | 14 +++++++++++++- maloja/database/__init__.py | 3 +++ maloja/database/sqldb.py | 9 +++++++++ maloja/web/jinja/abstracts/base.jinja | 4 ++++ maloja/web/static/js/search.js | 25 +++++++++++++++++++++++-- 5 files changed, 52 insertions(+), 3 deletions(-) diff --git a/maloja/apis/native_v1.py b/maloja/apis/native_v1.py index 585fa11..d969115 100644 --- a/maloja/apis/native_v1.py +++ b/maloja/apis/native_v1.py @@ -592,6 +592,7 @@ def search(**keys): artists = database.db_search(query,type="ARTIST") tracks = database.db_search(query,type="TRACK") + albums = database.db_search(query,type="ALBUM") @@ -599,6 +600,7 @@ def search(**keys): # also, shorter is better (because longer titles would be easier to further specify) artists.sort(key=lambda x: ((0 if x.lower().startswith(query) else 1 if " " + query in x.lower() else 2),len(x))) tracks.sort(key=lambda x: ((0 if x["title"].lower().startswith(query) else 1 if " " + query in x["title"].lower() else 2),len(x["title"]))) + albums.sort(key=lambda x: ((0 if x["albumtitle"].lower().startswith(query) else 1 if " " + query in x["albumtitle"].lower() else 2),len(x["albumtitle"]))) # add links artists_result = [] @@ -619,7 +621,17 @@ def search(**keys): } tracks_result.append(result) - return {"artists":artists_result[:max_],"tracks":tracks_result[:max_]} + albums_result = [] + for al in albums: + result = { + 'album': al, + 'link': "/album?" + compose_querystring(internal_to_uri({"album":al})), + 'image': images.get_album_image(al) + } + if not result['album']['artists']: result['album']['displayArtist'] = malojaconfig["DEFAULT_ALBUM_ARTIST"] + albums_result.append(result) + + return {"artists":artists_result[:max_],"tracks":tracks_result[:max_],"albums":albums_result[:max_]} @api.post("newrule") diff --git a/maloja/database/__init__.py b/maloja/database/__init__.py index 978b569..37285db 100644 --- a/maloja/database/__init__.py +++ b/maloja/database/__init__.py @@ -580,4 +580,7 @@ def db_search(query,type=None): results = sqldb.search_artist(query) if type=="TRACK": results = sqldb.search_track(query) + if type=="ALBUM": + results = sqldb.search_album(query) + return results diff --git a/maloja/database/sqldb.py b/maloja/database/sqldb.py index b28e89a..26f2f62 100644 --- a/maloja/database/sqldb.py +++ b/maloja/database/sqldb.py @@ -1214,6 +1214,15 @@ def search_track(searchterm,dbconn=None): return [get_track(row.id,dbconn=dbconn) for row in result] +@cached_wrapper +@connection_provider +def search_album(searchterm,dbconn=None): + op = DB['albums'].select().where( + DB['albums'].c.albtitle_normalized.ilike(normalize_name(f"%{searchterm}%")) + ) + result = dbconn.execute(op).all() + + return [get_album(row.id,dbconn=dbconn) for row in result] ##### MAINTENANCE diff --git a/maloja/web/jinja/abstracts/base.jinja b/maloja/web/jinja/abstracts/base.jinja index 3ca1619..6d0b542 100644 --- a/maloja/web/jinja/abstracts/base.jinja +++ b/maloja/web/jinja/abstracts/base.jinja @@ -82,6 +82,10 @@ Tracks
+

+ Albums + +
diff --git a/maloja/web/static/js/search.js b/maloja/web/static/js/search.js index b5244dc..1dd60d5 100644 --- a/maloja/web/static/js/search.js +++ b/maloja/web/static/js/search.js @@ -23,11 +23,13 @@ function html_to_fragment(html) { var results_artists; var results_tracks; +var results_albums; var searchresultwrap; window.addEventListener("DOMContentLoaded",function(){ results_artists = document.getElementById("searchresults_artists"); results_tracks = document.getElementById("searchresults_tracks"); + results_albums = document.getElementById("searchresults_albums"); searchresultwrap = document.getElementById("resultwrap"); }); @@ -50,8 +52,9 @@ function searchresult() { // any older searches are now rendered irrelevant while (searches[0] != this) { searches.splice(0,1) } var result = JSON.parse(this.responseText); - var artists = result["artists"].slice(0,5) - var tracks = result["tracks"].slice(0,5) + var artists = result["artists"].slice(0,4) + var tracks = result["tracks"].slice(0,4) + var albums = result["albums"].slice(0,4) while (results_artists.firstChild) { results_artists.removeChild(results_artists.firstChild); @@ -59,6 +62,9 @@ function searchresult() { while (results_tracks.firstChild) { results_tracks.removeChild(results_tracks.firstChild); } + while (results_albums.firstChild) { + results_albums.removeChild(results_albums.firstChild); + } for (var i=0;i