diff --git a/maloja/db/sqldb.py b/maloja/db/sqldb.py index 26517ec..1a88ab2 100644 --- a/maloja/db/sqldb.py +++ b/maloja/db/sqldb.py @@ -72,9 +72,12 @@ meta.create_all(engine) + +##### Conversions between DB and dicts +## These should only take the row info from their respective table and fill in +## other information by calling the respective id lookup functions + def scrobble_db_to_dict(row): - - return { "time":row.timestamp, "track":get_track(row.track_id), @@ -84,6 +87,7 @@ def scrobble_db_to_dict(row): def track_db_to_dict(row): return { + "artists":get_artists_of_track(row.id), "title":row.title, "length":row.length } @@ -229,7 +233,6 @@ def get_scrobbles_of_artist(artist,since,to): ) result = conn.execute(op).all() - print(result) return result @@ -244,19 +247,16 @@ def get_scrobbles_of_track(track,since,to): ) result = conn.execute(op).all() - print(result) return result def get_scrobbles(since,to): - print(since,to) with engine.begin() as conn: op = DB['scrobbles'].select().where( DB['scrobbles'].c.timestamp<=to, DB['scrobbles'].c.timestamp>=since, ) - print(str(op)) result = conn.execute(op).all() result = [scrobble_db_to_dict(row) for row in result] @@ -272,16 +272,7 @@ def get_track(id): trackinfo = result[0] - with engine.begin() as conn: - op = DB['trackartists'].select().where( - DB['trackartists'].c.track_id==id - ) - result = conn.execute(op).all() - - artists = [get_artist(row.artist_id) for row in result] - result = track_db_to_dict(trackinfo) - result['artists'] = artists return result @@ -295,6 +286,15 @@ def get_artist(id): artistinfo = result[0] return artist_db_to_dict(artistinfo) +def get_artists_of_track(track_id): + with engine.begin() as conn: + op = DB['trackartists'].select().where( + DB['trackartists'].c.track_id==track_id + ) + result = conn.execute(op).all() + + artists = [get_artist(row.artist_id) for row in result] + return artists diff --git a/maloja/web/jinja/partials/scrobbles.jinja b/maloja/web/jinja/partials/scrobbles.jinja index c3086dc..3c00b3c 100644 --- a/maloja/web/jinja/partials/scrobbles.jinja +++ b/maloja/web/jinja/partials/scrobbles.jinja @@ -11,7 +11,7 @@ {%- if loop.index0 >= firstindex and loop.index0 < lastindex -%} {{ malojatime.timestamp_desc(s["time"],short=shortTimeDesc) }} - {{ entityrow.row(s) }} + {{ entityrow.row(s.track) }} {%- endif -%} {% endfor %} diff --git a/maloja/web/jinja/scrobbles.jinja b/maloja/web/jinja/scrobbles.jinja index 062cb90..814cde6 100644 --- a/maloja/web/jinja/scrobbles.jinja +++ b/maloja/web/jinja/scrobbles.jinja @@ -12,7 +12,7 @@ {% elif filterkeys.get('artist') is not none %} {% set img = utilities.getArtistImage(filterkeys.artist,fast=True) %} {% elif scrobbles.__len__() > 0 %} - {% set img = utilities.getTrackImage(artists=scrobbles[0].artists,title=scrobbles[0].title,fast=True) %} + {% set img = utilities.getTrackImage(artists=scrobbles[0].track.artists,title=scrobbles[0].track.title,fast=True) %} {% else %} {% set img = "/favicon.png" %} {% endif %}