mirror of
https://github.com/krateng/maloja.git
synced 2023-08-10 21:12:55 +03:00
Modularizing HTML generation, Part II
This commit is contained in:
parent
3cfa8428ff
commit
3868d8532d
@ -610,6 +610,8 @@ def db_query(artists=None,title=None,track=None,since=None,to=None,associated=Fa
|
||||
# if a title is specified, we assume that a specific track (with the exact artist combination) is requested
|
||||
# if not, duplicate artist arguments are ignored
|
||||
|
||||
artist = None
|
||||
|
||||
# artists to numbers
|
||||
artists = set([(ARTISTS.index(a) if isinstance(a,str) else a) for a in artists])
|
||||
|
||||
@ -622,8 +624,6 @@ def db_query(artists=None,title=None,track=None,since=None,to=None,associated=Fa
|
||||
# we only need one artist
|
||||
elif track==None and len(artists) != 0:
|
||||
artist = artists.pop()
|
||||
else:
|
||||
artist = None
|
||||
|
||||
|
||||
# right now we always request everything by name, maybe we don't actually need the request by number, but i'll leave it in for now
|
||||
|
@ -4,6 +4,28 @@
|
||||
def artistLink(name):
|
||||
import urllib
|
||||
return "<a href='/artist?artist=" + urllib.parse.quote(name) + "'>" + name + "</a>"
|
||||
|
||||
def artistLinks(artists):
|
||||
return ", ".join([artistLink(a) for a in artists])
|
||||
|
||||
#def trackLink(artists,title):
|
||||
def trackLink(track):
|
||||
artists,title = track["artists"],track["title"]
|
||||
import urllib
|
||||
return "<a href='/track?title=" + urllib.parse.quote(title) + "&" + "&".join(["artist=" + urllib.parse.quote(a) for a in artists]) + "'>" + title + "</a>"
|
||||
|
||||
#def scrobblesTrackLink(artists,title,timekeys,amount=None,pixels=None):
|
||||
def scrobblesTrackLink(track,timekeys,amount=None,pixels=None):
|
||||
artists,title = track["artists"],track["title"]
|
||||
import urllib
|
||||
inner = str(amount) if amount is not None else "<div style='width:" + str(pixels) + "%;'></div>"
|
||||
return "<a href='/scrobbles?" + "&".join(["artist=" + urllib.parse.quote(a) for a in artists]) + "&title=" + urllib.parse.quote(title) + "&" + keysToUrl(timekeys) + "'>" + inner + "</a>"
|
||||
|
||||
def scrobblesArtistLink(artist,timekeys,amount=None,pixels=None,associated=False):
|
||||
import urllib
|
||||
inner = str(amount) if amount is not None else "<div style='width:" + str(pixels) + "%;'></div>"
|
||||
askey = "&associated" if associated else ""
|
||||
return "<a href='/scrobbles?artist=" + urllib.parse.quote(artist) + "&" + keysToUrl(timekeys) + askey + "'>" + inner + "</a>"
|
||||
|
||||
# necessary because urllib.parse.urlencode doesnt handle multidicts
|
||||
def keysToUrl(*dicts):
|
||||
@ -12,7 +34,7 @@ def keysToUrl(*dicts):
|
||||
keys = removeIdentical(*dicts)
|
||||
for k in keys:
|
||||
values = keys.getall(k)
|
||||
st += "&".join([urllib.parse.urlencode({k:v}) for v in values])
|
||||
st += "&".join([urllib.parse.urlencode({k:v},safe="/") for v in values])
|
||||
st += "&"
|
||||
return st
|
||||
|
||||
@ -46,15 +68,13 @@ def getTimeDesc(timestamp):
|
||||
|
||||
# limit a multidict to only the specified keys
|
||||
# would be a simple constructor expression, but multidicts apparently don't let me do that
|
||||
# hardcoding this to only allow multi values for a key in one case: artist when there is also a title specified
|
||||
def pickKeys(d,*keys):
|
||||
from bottle import FormsDict
|
||||
if isinstance(d,dict) or not "title" in d:
|
||||
if isinstance(d,dict):
|
||||
return {k:d.get(k) for k in d if k in keys}
|
||||
else:
|
||||
# create a normal dictionary of lists
|
||||
newd = {k:d.getall(k) for k in d if k in keys and k=="artist"}
|
||||
newd2 = {k:[d.get(k)] for k in d if k in keys and k!="artist"}
|
||||
newd = {k:d.getall(k) for k in d if k in keys}
|
||||
# one by one add the list entries to the formsdict
|
||||
finald = FormsDict()
|
||||
for k in newd:
|
||||
@ -62,3 +82,13 @@ def pickKeys(d,*keys):
|
||||
finald.append(k,v)
|
||||
|
||||
return finald
|
||||
|
||||
# removes all duplicate keys, except artists when a title is specified
|
||||
def clean(d):
|
||||
from bottle import FormsDict
|
||||
if isinstance(d,dict):
|
||||
return
|
||||
else:
|
||||
for k in d:
|
||||
if (k != "artist") or "title" not in d:
|
||||
d[k] = d.pop(k)
|
||||
|
@ -3,7 +3,8 @@ import json
|
||||
|
||||
|
||||
def replacedict(keys,dbport):
|
||||
from utilities import getArtistInfo, artistLink
|
||||
from utilities import getArtistInfo
|
||||
from htmlgenerators import artistLink
|
||||
|
||||
|
||||
info = getArtistInfo(keys["artist"])
|
||||
|
@ -4,13 +4,13 @@ import json
|
||||
|
||||
def replacedict(keys,dbport):
|
||||
from utilities import getArtistInfo
|
||||
from htmlgenerators import getTimeDesc, artistLink, keysToUrl, pickKeys
|
||||
from htmlgenerators import getTimeDesc, artistLink, artistLinks, trackLink, keysToUrl, pickKeys, clean
|
||||
|
||||
clean(keys)
|
||||
timekeys = pickKeys(keys,"since","to","in")
|
||||
limitkeys = pickKeys(keys,"artist","title")
|
||||
limitkeys = pickKeys(keys,"artist","title","associated")
|
||||
|
||||
limitstring = ""
|
||||
|
||||
response = urllib.request.urlopen("http://localhost:" + str(dbport) + "/scrobbles?" + keysToUrl(limitkeys,timekeys))
|
||||
db_data = json.loads(response.read())
|
||||
scrobbles = db_data["list"]
|
||||
@ -38,16 +38,10 @@ def replacedict(keys,dbport):
|
||||
|
||||
html = "<table class='list'>"
|
||||
for s in scrobbles:
|
||||
html += "<tr><td class='time'>"
|
||||
timestring = getTimeDesc(s["time"])
|
||||
html += timestring
|
||||
html += "</td><td class='artists'>"
|
||||
html += ", ".join([artistLink(a) for a in s["artists"]])
|
||||
#artisthtml = ""
|
||||
#for a in s["artists"]:
|
||||
# artisthtml += "<a href=/artist?artist=" + urllib.parse.quote(a) + ">" + a + "</a>, "
|
||||
#html += artisthtml[:-2]
|
||||
html += "</td><td class='title'>" + s["title"] + "</td></tr>"
|
||||
html += "<tr>"
|
||||
html += "<td class='time'>" + getTimeDesc(s["time"]) + "</td>"
|
||||
html += "<td class='artists'>" + artistLinks(s["artists"]) + "</td>"
|
||||
html += "<td class='title'>" + trackLink({"artists":s["artists"],"title":s["title"]}) + "</td></tr>"
|
||||
html += "</table>"
|
||||
|
||||
return {"KEY_SCROBBLELIST":html,"KEY_SCROBBLES":str(len(scrobbles)),"KEY_IMAGEURL":imgurl,"KEY_LIMITS":limitstring}
|
||||
|
@ -4,8 +4,9 @@ import json
|
||||
|
||||
def replacedict(keys,dbport):
|
||||
from utilities import getArtistInfo
|
||||
from htmlgenerators import artistLink, keysToUrl, pickKeys
|
||||
from htmlgenerators import artistLink, artistLinks, trackLink, scrobblesArtistLink, keysToUrl, pickKeys, clean
|
||||
|
||||
clean(keys)
|
||||
timekeys = pickKeys(keys,"since","to","in")
|
||||
limitkeys = pickKeys(keys)
|
||||
|
||||
@ -30,13 +31,13 @@ def replacedict(keys,dbport):
|
||||
html = "<table class='list'>"
|
||||
for e in charts:
|
||||
html += "<tr>"
|
||||
html += "<td class='rank'>#" + str(i) + "</td><td class='artist'>"
|
||||
#html += "<a href=/artist?artist=" + urllib.parse.quote(e["artist"]) + ">" + e["artist"] + "</a>"
|
||||
html += artistLink(e["artist"])
|
||||
html += "<td class='rank'>#" + str(i) + "</td>"
|
||||
html += "<td class='artist'>" + artistLink(e["artist"])
|
||||
if (e["counting"] != []):
|
||||
html += " <span class='extra'>incl. " + ", ".join([artistLink(a) for a in e["counting"]]) + "</span>"
|
||||
html += "</td><td class='amount'><a href='/scrobbles?artist=" + urllib.parse.quote(e["artist"]) + "&associated&" + keysToUrl(timekeys) + "'>" + str(e["scrobbles"]) + "</a></td>"
|
||||
html += "<td class='bar'><a href='/scrobbles?artist=" + urllib.parse.quote(e["artist"]) + "&associated&" + keysToUrl(timekeys) + "'><div style='width:" + str(e["scrobbles"]/maxbar * 100) + "%;'></div></a></td>"
|
||||
html += "</td>"
|
||||
html += "<td class='amount'>" + scrobblesArtistLink(e["artist"],timekeys,amount=e["scrobbles"],associated=True) + "</td>"
|
||||
html += "<td class='bar'>" + scrobblesArtistLink(e["artist"],timekeys,pixels=e["scrobbles"]*100/maxbar,associated=True) + "</td>"
|
||||
html += "</tr>"
|
||||
i += 1
|
||||
html += "</table>"
|
||||
|
@ -4,8 +4,9 @@ import json
|
||||
|
||||
def replacedict(keys,dbport):
|
||||
from utilities import getArtistInfo
|
||||
from htmlgenerators import artistLink, keysToUrl, pickKeys
|
||||
from htmlgenerators import artistLink, artistLinks, trackLink, scrobblesTrackLink, keysToUrl, pickKeys, clean
|
||||
|
||||
clean(keys)
|
||||
timekeys = pickKeys(keys,"since","to","in")
|
||||
limitkeys = pickKeys(keys,"artist")
|
||||
|
||||
@ -37,12 +38,11 @@ def replacedict(keys,dbport):
|
||||
html = "<table class='list'>"
|
||||
for e in charts:
|
||||
html += "<tr>"
|
||||
html += "<td class='rank'>#" + str(i) + "</td><td class='artists'>"
|
||||
html += ", ".join([artistLink(a) for a in e["track"]["artists"]])
|
||||
html += "</td><td class='title'>" + e["track"]["title"]
|
||||
html += "</td><td class='amount'><a href='/scrobbles?" + "&".join(["artist=" + urllib.parse.quote(a) for a in e["track"]["artists"]]) + "&title=" + urllib.parse.quote(e["track"]["title"]) + "&" + keysToUrl(timekeys) + "'>" + str(e["scrobbles"]) + "</a></td>"
|
||||
html += "<td class='bar'><a href='/scrobbles?" + "&".join(["artist=" + urllib.parse.quote(a) for a in e["track"]["artists"]]) + "&title=" + urllib.parse.quote(e["track"]["title"]) + "&" + keysToUrl(timekeys) + "'><div style='width:" + str(e["scrobbles"]/maxbar * 100) + "%;'></div></a>"
|
||||
html += "</td>"
|
||||
html += "<td class='rank'>#" + str(i) + "</td>"
|
||||
html += "<td class='artists'>" + artistLinks(e["track"]["artists"]) + "</td>"
|
||||
html += "<td class='title'>" + trackLink(e["track"]) + "</td>"
|
||||
html += "<td class='amount'>" + scrobblesTrackLink(e["track"],timekeys,amount=e["scrobbles"]) + "</td>"
|
||||
html += "<td class='bar'>" + scrobblesTrackLink(e["track"],timekeys,pixels=e["scrobbles"]*100/maxbar) + "</td>"
|
||||
html += "</tr>"
|
||||
i += 1
|
||||
html += "</table>"
|
||||
|
Loading…
Reference in New Issue
Block a user