1
0
mirror of https://github.com/krateng/maloja.git synced 2023-08-10 21:12:55 +03:00

Created first experimental Jinja templates

This commit is contained in:
Krateng 2020-05-17 01:58:24 +02:00
parent 1a64641fe6
commit b3f4fc1246
9 changed files with 414 additions and 8 deletions

28
maloja/database_packed.py Normal file
View File

@ -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)

View File

@ -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("/<name>")
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

View File

@ -0,0 +1,153 @@
{% extends "base.jinja" %}
{% block title %}Maloja - {{ artist }}{% endblock %}
{% block scripts %}
<script src="/rangeselect.js"></script>
{% 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 %}
<table class="top_info">
<tr>
<td class="image">
{% if adminmode %}
<div
class="changeable-image" data-uploader="b64=>upload('{encodedartist}','{apikey}',b64)"
style="background-image:url('{{ utilities.getArtistImage(artist=artist,fast=True) }}');"
></div>
{% else %}
<div style="background-image:url('{{ utilities.getArtistImage(artist=artist,fast=True) }}');">
</div>
{% endif %}
</td>
<td class="text">
<h1>{{ artist }}</h1>
{% if competes %}<span class="rank"><a href="/charts_artists?max=100">{{ positionstring }}</a></span>{% endif %}
<br/>
{% if competes and included %}
<span>associated: {{ htmlgenerators.artistLinks(included) }}</span>
{% elif not competes %}
<span>Competing under {{ htmlgenerators.artistLink(credited) }} ({{ positionstring }})</span>
{% endif %}
<p class="stats"><a href="/scrobbles?{{ encodedartist }}">{{ info['scrobbles'] }} Scrobbles</a></p>
{% import 'partials/awards.jinja' as awards %}
{% if competes %}
{{ awards.medals(info) }}
{{ awards.topweeks(info) }}
{% endif %}
{{ awards.certs(artist) }}
</td>
</tr>
</table>
<h2><a href='/charts_tracks?{{ encodedartist }}'>Top Tracks</a></h2>
{# htmlmodules.module_trackcharts(**filterkeys,max_=15)[0] #}
{% import 'partials/charts_tracks.jinja' as charts_tracks %}
{{ charts_tracks.charts_tracks(filterkeys,limitkeys,amountkeys,compare=false) }}
<br/>
<table class="twopart">
<tr>
<td>
<h2><a href='/pulse?{{ encodedartist }}&amp;step=year&amp;trail=1'>Pulse</a></h2>
{% for range in xranges %}
<span
onclick="showRangeManual('pulse','{{ range.identifier }}')"
class="stat_selector_pulse selector_pulse_{{ range.identifier }}"
style="{{ 'opacity:0.5;' if initialrange==range.identifier else '' }}">
{{ range.localisation }}
</span>
{% if not loop.last %}|{% endif %}
{% endfor %}
<br/><br/>
{% import 'partials/pulse.jinja' as pulse %}
{% for range in xranges %}
<span
class="stat_module_pulse pulse_{{ range.identifier }}"
style="{{ 'display:none;' if initialrange==range.identifier else '' }}"
>
{{ pulse.pulse(filterkeys,{'since':range.firstrange},{'step':range.identifier,'trail':1},amountkeys) }}
</span>
{% endfor %}
</td>
<td>
<!-- We use the same classes / function calls here because we want it to switch together with pulse -->
<h2><a href='/performance?{{ encodedcredited }}&amp;step=year&amp;trail=1'>Performance</a></h2>
{% for range in xranges %}
<span
onclick="showRangeManual('pulse','{{ range.identifier }}')"
class="stat_selector_pulse selector_pulse_{{ range.identifier }}"
style="{{ 'opacity:0.5;' if initialrange==range.identifier else '' }}">
{{ range.localisation }}
</span>
{% if not loop.last %}|{% endif %}
{% endfor %}
<br/><br/>
{% import 'partials/performance.jinja' as performance %}
{% for range in xranges %}
<span
class="stat_module_pulse pulse_{{ range.identifier }}"
style="{{ 'display:none;' if initialrange==range.identifier else '' }}"
>
{{ performance.performance(filterkeys,{'since':range.firstrange},{'step':range.identifier,'trail':1},amountkeys) }}
</span>
{% endfor %}
</td>
</tr>
</table>
<h2><a href='/scrobbles?{encodedartist}'>Last Scrobbles</a></h2>
{% import 'partials/scrobbles.jinja' as scrobbles %}
{{ scrobbles.scrobbles(filterkeys,limitkeys,{"perpage":15,"page":0}) }}
{% endblock %}

View File

@ -0,0 +1,65 @@
{% macro medals(info) %}
<!-- MEDALS -->
{% for year in info.medals.gold -%}
<a title="Best Artist in {{ year }}" class="hidelink medal shiny gold" href='/charts_artists?max=50&amp;in={{ year }}'>
<span>{{ year }}</span>
</a>
{%- endfor %}
{% for year in info.medals.silver -%}
<a title="Second best Artist in {{ year }}" class="hidelink medal shiny silver" href='/charts_artists?max=50&amp;in={{ year }}'>
<span>{{ year }}</span>
</a>
{%- endfor %}
{% for year in info.medals.bronze -%}
<a title="Third best Artist in {{ year }}" class="hidelink medal shiny bronze" href='/charts_artists?max=50&amp;in={{ year }}'>
<span>{{ year }}</span>
</a>
{%- endfor %}
{%- endmacro %}
{% macro topweeks(info) %}
<!-- TOPWEEKS -->
<span>
{% if info.topweeks is not none %}
<a title="{{ info.topweeks }} weeks on #1" href="/performance?{{ encodedartist }}&step=week">
<img class="star" src="/media/star.png" />{{ info.topweeks }}
</a>
{% endif %}
</span>
{%- endmacro %}
{% macro certs(artist) %}
<!-- CERTS -->
{% for track in db.get_tracks(artist=artist) %}
{% set info = db.trackInfo(track) %}
{% if info.certification is not none %}
<a href='{{ htmlgenerators.link_address(track) }}'>
<img class="certrecord_small"
src="/media/record_{{ info.certification }}.png"
title="{{ track.title }} has reached {{ info.certification.capitalize() }} status" />
</a>
{% endif %}
{% endfor %}
{%- endmacro %}

View File

@ -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 %}
<table class='list'>
{% for e in tracks %}
<tr>
<!-- Rank -->
<td class="rank">{%if loop.changed(e.scrobbles) %}#{{ e.rank }}{% endif %}</td>
<!-- Rank change -->
{% if false %}
{% if e not in prevtracks %}<td class='rankup' title='New'>🆕</td>{% endif %}
{% endif %}
<!-- artist -->
{{ htmlgenerators.entity_column(e['track']) }}
<!-- scrobbles -->
<td class="amount">{{ htmlgenerators.scrobblesTrackLink(e['track'],urihandler.internal_to_uri(limitkeys),amount=e['scrobbles']) }}</td>
<td class="bar">{{ htmlgenerators.scrobblesTrackLink(e['track'],urihandler.internal_to_uri(limitkeys),percent=e['scrobbles']*100/maxbar) }}</td>
</tr>
{% endfor %}
</table>
{%- endmacro %}

View File

@ -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 %}
<table class="list">
{% for t in ranges %}
{% set thisrange = t.range %}
<tr>
<td>{{ thisrange.desc() }}</td>
<td class="rank">
{{ '#' + t.rank.__str__() if t.rank is not none else 'n/a' }}
</td>
<td class="chart">
{% 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) }}
</td>
</tr>
{% endfor %}
</table>
{%- endmacro %}

View File

@ -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 %}
<table class="list">
{% for t in ranges %}
{% set thisrange = t.range %}
<tr>
<td>{{ thisrange.desc() }}</td>
<td class="amount">
{{ htmlgenerators.scrobblesLink(thisrange.urikeys(),amount=t.scrobbles,**filterkeys) }}
</td>
<td class="bar">
{{ htmlgenerators.scrobblesLink(thisrange.urikeys(),percent=t.scrobbles*100/maxbar,**filterkeys) }}
</td>
</tr>
{% endfor %}
</table>
{%- endmacro %}

View File

@ -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 %}
<table class='list'>
{% for s in scrobbles %}
{% if loop.index0 > firstindex and loop.index0 < lastindex %}
<tr>
<td class='time'>
</td>
{{ entityrow.row(s) }}
</tr>
{% endif %}
{% endfor %}
</table>
{%- endmacro %}

View File

@ -0,0 +1,16 @@
{% macro row(entity,counting=[]) %}
{% if "artists" in entity %}
<td class='track'>
<span class='artist_in_trackcolumn'>{{ htmlgenerators.html_links(entity.artists) }}</span> {{ htmlgenerators.html_link(entity) }}
</td>
{% else %}
<td class='artist'>{{ htmlgenerators.html_link(entity) }}
{% if counting != [] %}
<span class='extra'>incl. {{ htmlgenerators.html_links(counting) }}</span>
{% endif %}
</td>
{% endif %}
{% endmacro %}