From 5fedde5e9f60a0198f3dd582701556856b572dec Mon Sep 17 00:00:00 2001 From: Krateng Date: Mon, 17 Jun 2019 14:59:30 +0200 Subject: [PATCH] Added graphical pagination --- htmlmodules.py | 78 ++++++++++++++++++++++++++++++++++++++---- urihandler.py | 5 +-- website/css/maloja.css | 5 +++ website/start.py | 2 +- 4 files changed, 81 insertions(+), 9 deletions(-) diff --git a/htmlmodules.py b/htmlmodules.py index 2617513..b5cbc03 100644 --- a/htmlmodules.py +++ b/htmlmodules.py @@ -18,12 +18,14 @@ import math # result.append(element.get("image")) -# artist=None,track=None,since=None,to=None,within=None,associated=False,max_=None,pictures=False -def module_scrobblelist(page=0,perpage=100,pictures=False,shortTimeDesc=False,earlystop=False,**kwargs): +#max_ indicates that no pagination should occur (because this is not the primary module) +def module_scrobblelist(page=0,perpage=100,max_=None,pictures=False,shortTimeDesc=False,earlystop=False,**kwargs): kwargs_filter = pickKeys(kwargs,"artist","track","associated") kwargs_time = pickKeys(kwargs,"timerange","since","to","within") + if max_ is not None: perpage,page=max_,0 + firstindex = page * perpage lastindex = firstindex + perpage @@ -37,6 +39,8 @@ def module_scrobblelist(page=0,perpage=100,pictures=False,shortTimeDesc=False,ea #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] + pages = math.ceil(len(scrobbles) / perpage) + representative = scrobbles[0] if len(scrobbles) is not 0 else None # build list @@ -62,22 +66,27 @@ def module_scrobblelist(page=0,perpage=100,pictures=False,shortTimeDesc=False,ea html += "" + if max_ is None: html += module_paginate(page=page,pages=pages,**kwargs) + return (html,len(scrobbles),representative) -def module_pulse(page=0,perpage=100,**kwargs): +def module_pulse(page=0,perpage=100,max_=None,**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") + if max_ is not None: perpage,page=max_,0 + firstindex = page * perpage lastindex = firstindex + perpage ranges = database.get_pulse(**kwargs_time,**kwargs_filter) + pages = math.ceil(len(ranges) / perpage) ranges = ranges[firstindex:lastindex] @@ -100,21 +109,26 @@ def module_pulse(page=0,perpage=100,**kwargs): html += "" html += "" + if max_ is None: html += module_paginate(page=page,pages=pages,**kwargs) return html -def module_performance(page=0,perpage=100,**kwargs): +def module_performance(page=0,perpage=100,max_=None,**kwargs): kwargs_filter = pickKeys(kwargs,"artist","track") kwargs_time = pickKeys(kwargs,"since","to","within","timerange","step","stepn","trail") + if max_ is not None: perpage,page=max_,0 + firstindex = page * perpage lastindex = firstindex + perpage ranges = database.get_performance(**kwargs_time,**kwargs_filter) + pages = math.ceil(len(ranges) / perpage) + ranges = ranges[firstindex:lastindex] # if time range not explicitly specified, only show from first appearance @@ -139,21 +153,26 @@ def module_performance(page=0,perpage=100,**kwargs): html += "" html += "" + if max_ is None: html += module_paginate(page=page,pages=pages,**kwargs) return html -def module_trackcharts(page=0,perpage=100,**kwargs): +def module_trackcharts(page=0,perpage=100,max_=None,**kwargs): kwargs_filter = pickKeys(kwargs,"artist","associated") kwargs_time = pickKeys(kwargs,"timerange","since","to","within") + if max_ is not None: perpage,page=max_,0 + firstindex = page * perpage lastindex = firstindex + perpage tracks = database.get_charts_tracks(**kwargs_filter,**kwargs_time) + pages = math.ceil(len(tracks) / perpage) + # last time range (to compare) try: trackslast = database.get_charts_tracks(**kwargs_filter,timerange=kwargs_time["timerange"].next(step=-1)) @@ -211,19 +230,25 @@ def module_trackcharts(page=0,perpage=100,**kwargs): prev = e html += "" + if max_ is None: html += module_paginate(page=page,pages=pages,**kwargs) + return (html,representative) -def module_artistcharts(page=0,perpage=100,**kwargs): +def module_artistcharts(page=0,perpage=100,max_=None,**kwargs): kwargs_filter = pickKeys(kwargs,"associated") #not used right now kwargs_time = pickKeys(kwargs,"timerange","since","to","within") + if max_ is not None: perpage,page=max_,0 + firstindex = page * perpage lastindex = firstindex + perpage artists = database.get_charts_artists(**kwargs_filter,**kwargs_time) + pages = math.ceil(len(artists) / perpage) + # last time range (to compare) try: #from malojatime import _get_next @@ -283,6 +308,8 @@ def module_artistcharts(page=0,perpage=100,**kwargs): html += "" + if max_ is None: html += module_paginate(page=page,pages=pages,**kwargs) + return (html, representative) @@ -499,6 +526,44 @@ def module_trackcharts_tiles(**kwargs): return html + +def module_paginate(page,pages,**keys): + + unchangedkeys = internal_to_uri({**keys}) + + html = "
" + + if page > 1: + html += "" + "1" + "" + html += " | " + + if page > 2: + html += " ... | " + + if page > 0: + html += "" + str(page) + "" + html += " « " + + html += "" + str(page+1) + "" + + if page < pages-1: + html += " » " + html += "" + str(page+2) + "" + + if page < pages-3: + html += " | ... " + + if page < pages-2: + html += " | " + html += "" + str(pages) + "" + + + html += "
" + + return html + + + # THIS FUNCTION USES THE ORIGINAL URI KEYS!!! def module_filterselection(keys,time=True,delimit=False): @@ -510,6 +575,7 @@ def module_filterselection(keys,time=True,delimit=False): html = "" + if time: # all other keys that will not be changed by clicking another filter #keystr = "?" + compose_querystring(keys,exclude=["since","to","in"]) diff --git a/urihandler.py b/urihandler.py index 6342cdb..9ed7943 100644 --- a/urihandler.py +++ b/urihandler.py @@ -106,6 +106,7 @@ def uri_to_internal(keys,forceTrack=False,forceArtist=False): 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"]) + #different max than the internal one! the user doesn't get to disable pagination if "page" in keys: resultkeys4["page"] = int(keys["page"]) if "perpage" in keys: resultkeys4["perpage"] = int(keys["perpage"]) @@ -149,8 +150,8 @@ def internal_to_uri(keys): urikeys.append("trail",str(keys["trail"])) # stuff - if "max_" in keys: - urikeys.append("max",str(keys["max_"])) + #if "max_" in keys: + # urikeys.append("max",str(keys["max_"])) if "page" in keys: urikeys.append("page",str(keys["page"])) if "perpage" in keys: diff --git a/website/css/maloja.css b/website/css/maloja.css index 56f4db0..8cc92a1 100644 --- a/website/css/maloja.css +++ b/website/css/maloja.css @@ -268,6 +268,11 @@ p.desc a { */ +.paginate { + text-align: center; + padding:30px; +} + .stats { diff --git a/website/start.py b/website/start.py index 91edbeb..72e6b7d 100644 --- a/website/start.py +++ b/website/start.py @@ -42,7 +42,7 @@ def instructions(keys): # scrobbles - html_scrobbles, _, _ = module_scrobblelist(perpage=15,shortTimeDesc=True,pictures=True,earlystop=True) + html_scrobbles, _, _ = module_scrobblelist(max_=15,shortTimeDesc=True,pictures=True,earlystop=True) clockp("Scrobbles")