From a5d73f8fe9de0047099934b6b8d8e2c6d23be5dd Mon Sep 17 00:00:00 2001 From: Krateng Date: Mon, 17 Jun 2019 14:28:16 +0200 Subject: [PATCH] Added basic pagination --- htmlmodules.py | 57 +++++++++++++++++++++++++++++++++--------------- urihandler.py | 11 ++++++++-- website/start.py | 2 +- 3 files changed, 49 insertions(+), 21 deletions(-) diff --git a/htmlmodules.py b/htmlmodules.py index 5b6775c..2617513 100644 --- a/htmlmodules.py +++ b/htmlmodules.py @@ -19,18 +19,20 @@ import math # artist=None,track=None,since=None,to=None,within=None,associated=False,max_=None,pictures=False -def module_scrobblelist(max_=None,pictures=False,shortTimeDesc=False,earlystop=False,**kwargs): +def module_scrobblelist(page=0,perpage=100,pictures=False,shortTimeDesc=False,earlystop=False,**kwargs): kwargs_filter = pickKeys(kwargs,"artist","track","associated") kwargs_time = pickKeys(kwargs,"timerange","since","to","within") + firstindex = page * perpage + lastindex = firstindex + perpage # if earlystop, we don't care about the actual amount and only request as many from the db # without, we request everything and filter on site - maxkey = {"max_":max_} if earlystop else {} + maxkey = {"max_":lastindex} if earlystop else {} scrobbles = database.get_scrobbles(**kwargs_time,**kwargs_filter,**maxkey) if pictures: - scrobbleswithpictures = scrobbles if max_ is None else scrobbles[:max_] + scrobbleswithpictures = [""] * firstindex + scrobbles[firstindex:lastindex] #scrobbleimages = [e.get("image") for e in getTracksInfo(scrobbleswithpictures)] #will still work with scrobble objects as they are a technically a subset of track objects #scrobbleimages = ["/image?title=" + urllib.parse.quote(t["title"]) + "&" + "&".join(["artist=" + urllib.parse.quote(a) for a in t["artists"]]) for t in scrobbleswithpictures] scrobbleimages = [getTrackImage(t["artists"],t["title"],fast=True) for t in scrobbleswithpictures] @@ -41,6 +43,9 @@ def module_scrobblelist(max_=None,pictures=False,shortTimeDesc=False,earlystop=F i = 0 html = "" for s in scrobbles: + if i" @@ -48,12 +53,10 @@ def module_scrobblelist(max_=None,pictures=False,shortTimeDesc=False,earlystop=F img = scrobbleimages[i] else: img = None html += entity_column(s,image=img) - # Alternative way: Do it in one cell - #html += "" html += "" i += 1 - if max_ is not None and i>=max_: + if i>=lastindex: break @@ -62,18 +65,21 @@ def module_scrobblelist(max_=None,pictures=False,shortTimeDesc=False,earlystop=F return (html,len(scrobbles),representative) -def module_pulse(max_=None,**kwargs): +def module_pulse(page=0,perpage=100,**kwargs): from doreah.timing import clock, clockp kwargs_filter = pickKeys(kwargs,"artist","track","associated") kwargs_time = pickKeys(kwargs,"since","to","within","timerange","step","stepn","trail") + firstindex = page * perpage + lastindex = firstindex + perpage + ranges = database.get_pulse(**kwargs_time,**kwargs_filter) - if max_ is not None: ranges = ranges[:max_] + ranges = ranges[firstindex:lastindex] # if time range not explicitly specified, only show from first appearance # if "since" not in kwargs: @@ -99,14 +105,17 @@ def module_pulse(max_=None,**kwargs): -def module_performance(max_=None,**kwargs): +def module_performance(page=0,perpage=100,**kwargs): kwargs_filter = pickKeys(kwargs,"artist","track") kwargs_time = pickKeys(kwargs,"since","to","within","timerange","step","stepn","trail") + firstindex = page * perpage + lastindex = firstindex + perpage + ranges = database.get_performance(**kwargs_time,**kwargs_filter) - if max_ is not None: ranges = ranges[:max_] + ranges = ranges[firstindex:lastindex] # if time range not explicitly specified, only show from first appearance # if "since" not in kwargs: @@ -135,11 +144,14 @@ def module_performance(max_=None,**kwargs): -def module_trackcharts(max_=None,**kwargs): +def module_trackcharts(page=0,perpage=100,**kwargs): kwargs_filter = pickKeys(kwargs,"artist","associated") kwargs_time = pickKeys(kwargs,"timerange","since","to","within") + firstindex = page * perpage + lastindex = firstindex + perpage + tracks = database.get_charts_tracks(**kwargs_filter,**kwargs_time) # last time range (to compare) @@ -167,13 +179,16 @@ def module_trackcharts(max_=None,**kwargs): i = 0 html = "
" + artistLinks(s["artists"]) + " — " + trackLink({"artists":s["artists"],"title":s["title"]}) + "
" for e in tracks: + if imax_: + if i>lastindex: break html += "" # rank - if i == 1 or e["scrobbles"] < prev["scrobbles"]: - html += "" + if i == firstindex+1 or e["scrobbles"] < prev["scrobbles"]: + html += "" else: html += "" # rank change @@ -199,11 +214,14 @@ def module_trackcharts(max_=None,**kwargs): return (html,representative) -def module_artistcharts(max_=None,**kwargs): +def module_artistcharts(page=0,perpage=100,**kwargs): kwargs_filter = pickKeys(kwargs,"associated") #not used right now kwargs_time = pickKeys(kwargs,"timerange","since","to","within") + firstindex = page * perpage + lastindex = firstindex + perpage + artists = database.get_charts_artists(**kwargs_filter,**kwargs_time) # last time range (to compare) @@ -231,13 +249,16 @@ def module_artistcharts(max_=None,**kwargs): i = 0 html = "
#" + str(i) + "#" + str(e["rank"]) + "
" for e in artists: + if imax_: + if i>lastindex: break html += "" # rank - if i == 1 or e["scrobbles"] < prev["scrobbles"]: - html += "" + if i == firstindex+1 or e["scrobbles"] < prev["scrobbles"]: + html += "" else: html += "" # rank change diff --git a/urihandler.py b/urihandler.py index ae08440..6342cdb 100644 --- a/urihandler.py +++ b/urihandler.py @@ -103,8 +103,11 @@ def uri_to_internal(keys,forceTrack=False,forceArtist=False): #4 - resultkeys4 = {"max_":300} - if "max" in keys: resultkeys4["max_"] = int(keys["max"]) + resultkeys4 = {"page":0,"perpage":100} +# if "max" in keys: resultkeys4["max_"] = int(keys["max"]) + if "max" in keys: resultkeys4["page"],resultkeys4["perpage"] = 0, int(keys["max"]) + if "page" in keys: resultkeys4["page"] = int(keys["page"]) + if "perpage" in keys: resultkeys4["perpage"] = int(keys["perpage"]) return resultkeys1, resultkeys2, resultkeys3, resultkeys4 @@ -148,6 +151,10 @@ def internal_to_uri(keys): # stuff if "max_" in keys: urikeys.append("max",str(keys["max_"])) + if "page" in keys: + urikeys.append("page",str(keys["page"])) + if "perpage" in keys: + urikeys.append("perpage",str(keys["perpage"])) return urikeys diff --git a/website/start.py b/website/start.py index 72e6b7d..91edbeb 100644 --- a/website/start.py +++ b/website/start.py @@ -42,7 +42,7 @@ def instructions(keys): # scrobbles - html_scrobbles, _, _ = module_scrobblelist(max_=15,shortTimeDesc=True,pictures=True,earlystop=True) + html_scrobbles, _, _ = module_scrobblelist(perpage=15,shortTimeDesc=True,pictures=True,earlystop=True) clockp("Scrobbles")
#" + str(i) + "#" + str(e["rank"]) + "