From b3f4fc12460d120e6e0ff1c574d4bde684f6688c Mon Sep 17 00:00:00 2001 From: Krateng Date: Sun, 17 May 2020 01:58:24 +0200 Subject: [PATCH] Created first experimental Jinja templates --- maloja/database_packed.py | 28 ++++ maloja/server.py | 24 ++- maloja/web/jinja/artist.jinja | 153 ++++++++++++++++++ maloja/web/jinja/partials/awards.jinja | 65 ++++++++ maloja/web/jinja/partials/charts_tracks.jinja | 49 ++++++ maloja/web/jinja/partials/performance.jinja | 34 ++++ maloja/web/jinja/partials/pulse.jinja | 29 ++++ maloja/web/jinja/partials/scrobbles.jinja | 24 +++ maloja/web/jinja/snippets/entityrow.jinja | 16 ++ 9 files changed, 414 insertions(+), 8 deletions(-) create mode 100644 maloja/database_packed.py create mode 100644 maloja/web/jinja/artist.jinja create mode 100644 maloja/web/jinja/partials/awards.jinja create mode 100644 maloja/web/jinja/partials/charts_tracks.jinja create mode 100644 maloja/web/jinja/partials/performance.jinja create mode 100644 maloja/web/jinja/partials/pulse.jinja create mode 100644 maloja/web/jinja/partials/scrobbles.jinja create mode 100644 maloja/web/jinja/snippets/entityrow.jinja diff --git a/maloja/database_packed.py b/maloja/database_packed.py new file mode 100644 index 0000000..a41cade --- /dev/null +++ b/maloja/database_packed.py @@ -0,0 +1,28 @@ +from . import database + +# this is simply an object to expose all database functions with their arguments packed into dicts +# because jinja doesn't accept **kwargs +class DB: + def __getattr__(self,name): + originalmethod = getattr(database,name) + + def packedmethod(*keys): + kwargs = {} + for k in keys: + kwargs.update(k) + return originalmethod(**kwargs) + + return packedmethod + + +# class that is initialized with all the uri keys of the currently requested page and exposes curried db functions +class View: + def __init__(self,filterkeys,limitkeys,delimitkeys,amountkeys): + self.filterkeys = filterkeys + self.limitkeys = limitkeys + self.delimitkeys = delimitkeys + self.amountkeys = amountkeys + + + def get_pulse(self): + return database.get_pulse(**self.limitkeys,**self.delimitkeys,**self.filterkeys) diff --git a/maloja/server.py b/maloja/server.py index 9003c9a..13d776a 100755 --- a/maloja/server.py +++ b/maloja/server.py @@ -171,17 +171,17 @@ def static(name,ext): return response -engine_prio = settings.get_settings("WEB_ENGINE_PRIORITY") -engines = { - "python":{"filetypes":["html","py"],"folder":"python"}, - "pyhp":{"filetypes":["pyhp"],"folder":"pyhp"}, - "jinja":{"filetypes":[],"folder":"jinja"} -} + + +from . import database_packed +dbp = database_packed.DB() + JINJA_CONTEXT = { # maloja "db": database, + "dbp":dbp, "htmlmodules": htmlmodules, "htmlgenerators": htmlgenerators, "malojatime": malojatime, @@ -197,6 +197,12 @@ JINJA_CONTEXT = { ('week','12 weeks',malojatime.thisweek().next(-11),'week',12), ('month','12 months',malojatime.thismonth().next(-11),'month',12), ('year','10 years',malojatime.thisyear().next(-9),'year',12) + ], + "xranges": [ + {"identifier":"day","localisation":"14 days","firstrange":malojatime.today().next(-13),"amount":14}, + {"identifier":"week","localisation":"14 weeks","firstrange":malojatime.thisweek().next(-13),"amount":14}, + {"identifier":"month","localisation":"14 months","firstrange":malojatime.thismonth().next(-13),"amount":14}, + {"identifier":"year","localisation":"14 years","firstrange":malojatime.thisyear().next(-13),"amount":14} ] } @@ -205,6 +211,8 @@ jinjaenv = Environment( loader=PackageLoader('maloja', 'web/jinja'), autoescape=select_autoescape(['html', 'xml']) ) +jinjaenv.globals.update(JINJA_CONTEXT) + @webserver.route("/") def static_html(name): @@ -228,13 +236,13 @@ def static_html(name): LOCAL_CONTEXT = { "adminmode":adminmode, "apikey":request.cookies.get("apikey") if adminmode else None, - "_urikeys":keys #temporary! + "_urikeys":keys, #temporary! } LOCAL_CONTEXT["filterkeys"], LOCAL_CONTEXT["limitkeys"], LOCAL_CONTEXT["delimitkeys"], LOCAL_CONTEXT["amountkeys"] = uri_to_internal(keys) template = jinjaenv.get_template(name + '.jinja') - res = template.render(**JINJA_CONTEXT,**LOCAL_CONTEXT) + res = template.render(**LOCAL_CONTEXT) log("Generated page {name} in {time}s (Jinja)".format(name=name,time=clock.stop()),module="debug") return res diff --git a/maloja/web/jinja/artist.jinja b/maloja/web/jinja/artist.jinja new file mode 100644 index 0000000..585bde5 --- /dev/null +++ b/maloja/web/jinja/artist.jinja @@ -0,0 +1,153 @@ +{% extends "base.jinja" %} +{% block title %}Maloja - {{ artist }}{% endblock %} + +{% block scripts %} + +{% endblock %} + +{% set artist = filterkeys.artist %} +{% set info = db.artistInfo(artist) %} + +{% set credited = info.get('replace') %} +{% set included = info.get('associated') %} +{% set initialrange ='month' %} + + +{% if credited is not none %} + {% set competes = false %} +{% else %} + {% set credited = artist %} + {% set competes = true %} +{% endif %} + + +{% set encodedartist = urihandler.compose_querystring(urihandler.internal_to_uri({'artist':artist})) %} +{% set encodedcredited = urihandler.compose_querystring(urihandler.internal_to_uri({'artist':credited})) %} + + +{% block content %} + + + + + + + + + +
+ {% if adminmode %} +
+ {% else %} +
+
+ {% endif %} +
+

{{ artist }}

+ {% if competes %}{{ positionstring }}{% endif %} +
+ {% if competes and included %} + associated: {{ htmlgenerators.artistLinks(included) }} + {% elif not competes %} + Competing under {{ htmlgenerators.artistLink(credited) }} ({{ positionstring }}) + {% endif %} + +

{{ info['scrobbles'] }} Scrobbles

+ + + {% import 'partials/awards.jinja' as awards %} + + {% if competes %} + {{ awards.medals(info) }} + {{ awards.topweeks(info) }} + {% endif %} + {{ awards.certs(artist) }} + + +
+ +

Top Tracks

+ +{# htmlmodules.module_trackcharts(**filterkeys,max_=15)[0] #} +{% import 'partials/charts_tracks.jinja' as charts_tracks %} + +{{ charts_tracks.charts_tracks(filterkeys,limitkeys,amountkeys,compare=false) }} + +
+ + + + + + + +
+

Pulse

+ + {% for range in xranges %} + + {{ range.localisation }} + + {% if not loop.last %}|{% endif %} + {% endfor %} + +

+ + {% import 'partials/pulse.jinja' as pulse %} + {% for range in xranges %} + + + + {{ pulse.pulse(filterkeys,{'since':range.firstrange},{'step':range.identifier,'trail':1},amountkeys) }} + + + {% endfor %} +
+ +

Performance

+ + {% for range in xranges %} + + {{ range.localisation }} + + {% if not loop.last %}|{% endif %} + {% endfor %} + +

+ + {% import 'partials/performance.jinja' as performance %} + {% for range in xranges %} + + + + {{ performance.performance(filterkeys,{'since':range.firstrange},{'step':range.identifier,'trail':1},amountkeys) }} + + + + {% endfor %} + +
+ + +

Last Scrobbles

+ +{% import 'partials/scrobbles.jinja' as scrobbles %} +{{ scrobbles.scrobbles(filterkeys,limitkeys,{"perpage":15,"page":0}) }} + + +{% endblock %} diff --git a/maloja/web/jinja/partials/awards.jinja b/maloja/web/jinja/partials/awards.jinja new file mode 100644 index 0000000..9f5b92d --- /dev/null +++ b/maloja/web/jinja/partials/awards.jinja @@ -0,0 +1,65 @@ +{% macro medals(info) %} + + +{% for year in info.medals.gold -%} + + {{ year }} + +{%- endfor %} +{% for year in info.medals.silver -%} + + {{ year }} + +{%- endfor %} +{% for year in info.medals.bronze -%} + + {{ year }} + +{%- endfor %} + +{%- endmacro %} + + + + + + + + +{% macro topweeks(info) %} + + + + + {% if info.topweeks is not none %} + + {{ info.topweeks }} + + {% endif %} + + +{%- endmacro %} + + + + + + + + + +{% macro certs(artist) %} + + +{% for track in db.get_tracks(artist=artist) %} + {% set info = db.trackInfo(track) %} + {% if info.certification is not none %} + + + + {% endif %} +{% endfor %} + +{%- endmacro %} diff --git a/maloja/web/jinja/partials/charts_tracks.jinja b/maloja/web/jinja/partials/charts_tracks.jinja new file mode 100644 index 0000000..c0cd491 --- /dev/null +++ b/maloja/web/jinja/partials/charts_tracks.jinja @@ -0,0 +1,49 @@ +{% macro charts_tracks(filterkeys,limitkeys,amountkeys,compare=False) %} + + +{% set tracks = dbp.get_charts_tracks(filterkeys,limitkeys) %} +{% if compare %} + {% if compare is true %} + {% set compare = limitkeys.timerange.next(step=-1) %} + {% endif %} + {% set prevtracks = dbp.get_charts_tracks(filterkeys,{'timerange':compare}) %} + + {% set lastrank = {} %} + {% for t in tracks %} + {% if lastrank.update({(t.track.artists,t.track.title):t.rank}) %}{% endif %} + {% endfor %} + + {% for t in tracks %} + {% if (t.track.artists,t.track.title) in lastrank %} + {% if t.update({'lastrank':lastrank[(t.track.artists,t.track.title)]}) %}{% endif %} + {% endif %} + {% endfor %} +{% endif %} + + +{% set maxbar = tracks[0]['scrobbles'] if tracks != [] else 0 %} + + + {% for e in tracks %} + + + + + {% if false %} + {% if e not in prevtracks %}{% endif %} + + {% endif %} + + + {{ htmlgenerators.entity_column(e['track']) }} + + + + + + {% endfor %} +
{%if loop.changed(e.scrobbles) %}#{{ e.rank }}{% endif %}🆕{{ htmlgenerators.scrobblesTrackLink(e['track'],urihandler.internal_to_uri(limitkeys),amount=e['scrobbles']) }}{{ htmlgenerators.scrobblesTrackLink(e['track'],urihandler.internal_to_uri(limitkeys),percent=e['scrobbles']*100/maxbar) }}
+ + + +{%- endmacro %} diff --git a/maloja/web/jinja/partials/performance.jinja b/maloja/web/jinja/partials/performance.jinja new file mode 100644 index 0000000..639d1ff --- /dev/null +++ b/maloja/web/jinja/partials/performance.jinja @@ -0,0 +1,34 @@ +{% macro performance(filterkeys,limitkeys,delimitkeys,amountkeys) %} + +{% set ranges = dbp.get_performance(filterkeys,limitkeys,delimitkeys) %} + +{% set minrank = 80 %} +{% for r in ranges %} + {% if r.rank is not none and r.rank+20 > minrank %} + {% set minrank = r.rank+20 %} + {% endif %} +{% endfor %} + + + + {% for t in ranges %} + + {% set thisrange = t.range %} + + + + + + + + + {% endfor %} +
{{ thisrange.desc() }} + {{ '#' + t.rank.__str__() if t.rank is not none else 'n/a' }} + + {% set prct = ((minrank+1-t.rank)*100/minrank if t.rank is not none else 0) %} + {{ htmlgenerators.rankLink(thisrange.urikeys(),percent=prct,medal=t.rank,**filterkeys) }} +
+ + +{%- endmacro %} diff --git a/maloja/web/jinja/partials/pulse.jinja b/maloja/web/jinja/partials/pulse.jinja new file mode 100644 index 0000000..ebb6b95 --- /dev/null +++ b/maloja/web/jinja/partials/pulse.jinja @@ -0,0 +1,29 @@ +{% macro pulse(filterkeys,limitkeys,delimitkeys,amountkeys) %} + +{% set ranges = dbp.get_pulse(filterkeys,limitkeys,delimitkeys) %} + +{% set maxbar = 1 %} +{% for r in ranges %} + {% if r.scrobbles > maxbar %} + {% set maxbar = r.scrobbles %} + {% endif %} +{% endfor %} + + + {% for t in ranges %} + + {% set thisrange = t.range %} + + + + + + {% endfor %} +
{{ thisrange.desc() }} + {{ htmlgenerators.scrobblesLink(thisrange.urikeys(),amount=t.scrobbles,**filterkeys) }} + + {{ htmlgenerators.scrobblesLink(thisrange.urikeys(),percent=t.scrobbles*100/maxbar,**filterkeys) }} +
+ + +{%- endmacro %} diff --git a/maloja/web/jinja/partials/scrobbles.jinja b/maloja/web/jinja/partials/scrobbles.jinja new file mode 100644 index 0000000..0ba0562 --- /dev/null +++ b/maloja/web/jinja/partials/scrobbles.jinja @@ -0,0 +1,24 @@ +{% macro scrobbles(filterkeys,limitkeys,amountkeys) %} + +{% set scrobbles = dbp.get_scrobbles(filterkeys,limitkeys,amountkeys) %} + +{% set firstindex = amountkeys.page * amountkeys.perpage %} +{% set lastindex = firstindex + amountkeys.perpage %} + +{% import 'snippets/entityrow.jinja' as entityrow %} + + + + {% for s in scrobbles %} + {% if loop.index0 > firstindex and loop.index0 < lastindex %} + + + {{ entityrow.row(s) }} + + {% endif %} + {% endfor %} +
+ +
+ +{%- endmacro %} diff --git a/maloja/web/jinja/snippets/entityrow.jinja b/maloja/web/jinja/snippets/entityrow.jinja new file mode 100644 index 0000000..0e39a9f --- /dev/null +++ b/maloja/web/jinja/snippets/entityrow.jinja @@ -0,0 +1,16 @@ +{% macro row(entity,counting=[]) %} + +{% if "artists" in entity %} + + {{ htmlgenerators.html_links(entity.artists) }} – {{ htmlgenerators.html_link(entity) }} + +{% else %} + {{ htmlgenerators.html_link(entity) }} + {% if counting != [] %} + incl. {{ htmlgenerators.html_links(counting) }} + {% endif %} + + +{% endif %} + +{% endmacro %}