From 22ee6bf7518287a89bd4243294bcc2c1068d7875 Mon Sep 17 00:00:00 2001 From: Krateng Date: Sun, 30 Aug 2020 23:49:14 +0200 Subject: [PATCH] Moved jinja handling to submodule --- maloja/jinjaenv/context.py | 72 +++++++++++++++++++ .../{jinja_filters.py => jinjaenv/filters.py} | 12 ++++ maloja/server.py | 55 ++------------ 3 files changed, 89 insertions(+), 50 deletions(-) create mode 100644 maloja/jinjaenv/context.py rename maloja/{jinja_filters.py => jinjaenv/filters.py} (86%) diff --git a/maloja/jinjaenv/context.py b/maloja/jinjaenv/context.py new file mode 100644 index 0000000..01e8c1b --- /dev/null +++ b/maloja/jinjaenv/context.py @@ -0,0 +1,72 @@ +from .. import database_packed +from . import filters + +from .. import database, database_packed, htmlmodules, htmlgenerators, malojatime, utilities, urihandler, malojauri +from doreah import settings + +import urllib +import math + +# templating +from jinja2 import Environment, PackageLoader, select_autoescape + +dbp = database_packed.DB() + +JINJA_CONTEXT = { + # maloja + "db": database, + "dbp":dbp, + "htmlmodules": htmlmodules, + "htmlgenerators": htmlgenerators, + "malojatime": malojatime, + "utilities": utilities, + "urihandler": urihandler, + "mlj_uri": malojauri, + "settings": settings.get_settings, + # external + "urllib": urllib, + "math":math, + # config + "ranges": [ + ('day','7 days',malojatime.today().next(-6),'day',7), + ('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":"12 days","firstrange":malojatime.today().next(-11),"amount":12}, + {"identifier":"week","localisation":"12 weeks","firstrange":malojatime.thisweek().next(-11),"amount":12}, + {"identifier":"month","localisation":"12 months","firstrange":malojatime.thismonth().next(-11),"amount":12}, + {"identifier":"year","localisation":"12 years","firstrange":malojatime.thisyear().next(-11),"amount":12} + ], + "xcurrent": [ + {"identifier":"day","localisation":"Today","range":malojatime.today()}, + {"identifier":"week","localisation":"This Week","range":malojatime.thisweek()}, + {"identifier":"month","localisation":"This Month","range":malojatime.thismonth()}, + {"identifier":"year","localisation":"This Year","range":malojatime.thisyear()}, + {"identifier":"alltime","localisation":"All Time","range":malojatime.alltime()} + ], + "xdelimiters": [ + {"identifier":"daily","replacekeys":{"step":"day","stepn":1},"localisation":"Daily"}, + {"identifier":"weekly","replacekeys":{"step":"week","stepn":1},"localisation":"Weekly"}, + {"identifier":"fortnightly","replacekeys":{"step":"week","stepn":2},"localisation":"Fortnightly"}, + {"identifier":"monthly","replacekeys":{"step":"month","stepn":1},"localisation":"Monthly"}, + {"identifier":"quarterly","replacekeys":{"step":"month","stepn":3},"localisation":"Quarterly"}, + {"identifier":"yearly","replacekeys":{"step":"year","stepn":1},"localisation":"Yearly"} + ], + "xtrails": [ + {"identifier":"standard","replacekeys":{"trail":1},"localisation":"Standard"}, + {"identifier":"trailing","replacekeys":{"trail":2},"localisation":"Trailing"}, + {"identifier":"longtrailing","replacekeys":{"trail":3},"localisation":"Long Trailing"}, + {"identifier":"inert","replacekeys":{"trail":10},"localisation":"Inert"}, + {"identifier":"cumulative","replacekeys":{"trail":math.inf},"localisation":"Cumulative"} + ] +} + + +jinja_environment = Environment( + loader=PackageLoader('maloja', "web/jinja"), + autoescape=select_autoescape(['html', 'xml']) +) +jinja_environment.globals.update(JINJA_CONTEXT) +jinja_environment.filters.update({k:filters.__dict__[k] for k in filters.__dict__ if not k.startswith("__")}) diff --git a/maloja/jinja_filters.py b/maloja/jinjaenv/filters.py similarity index 86% rename from maloja/jinja_filters.py rename to maloja/jinjaenv/filters.py index c2d6899..f6cc0b2 100644 --- a/maloja/jinja_filters.py +++ b/maloja/jinjaenv/filters.py @@ -39,3 +39,15 @@ def find_representative(sequence,attribute_id,attribute_count): # # return mostappearances # # among those, pick the one with the highest count in one of their appearances + + +def combine_dicts(dictlist): + res = {k:d[k] for d in dictlist for k in d} + return res + + +def compare_key_in_dicts(key,d1,d2): + return d1[key] == d2[key] + +def alltrue(seq): + return all(s for s in seq) diff --git a/maloja/server.py b/maloja/server.py index e777c3f..2d1d6e5 100755 --- a/maloja/server.py +++ b/maloja/server.py @@ -6,8 +6,7 @@ from .globalconf import datadir, DATA_DIR # server stuff from bottle import Bottle, route, get, post, error, run, template, static_file, request, response, FormsDict, redirect, template, HTTPResponse, BaseRequest, abort import waitress -# templating -from jinja2 import Environment, PackageLoader, select_autoescape + # monkey patching from . import monkey # rest of the project @@ -16,12 +15,13 @@ from . import htmlmodules from . import htmlgenerators from . import malojatime from . import utilities +from . import malojauri from .utilities import resolveImage from .urihandler import remove_identical from .malojauri import uri_to_internal from . import urihandler from . import globalconf -from . import jinja_filters +from .jinjaenv.context import jinja_environment # doreah toolkit from doreah import settings from doreah.logging import log @@ -188,51 +188,6 @@ aliases = { -from . import database_packed -dbp = database_packed.DB() - -JINJA_CONTEXT = { - # maloja - "db": database, - "dbp":dbp, - "htmlmodules": htmlmodules, - "htmlgenerators": htmlgenerators, - "malojatime": malojatime, - "utilities": utilities, - "urihandler": urihandler, - "settings": settings.get_settings, - # external - "urllib": urllib, - "math":math, - # config - "ranges": [ - ('day','7 days',malojatime.today().next(-6),'day',7), - ('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":"12 days","firstrange":malojatime.today().next(-11),"amount":12}, - {"identifier":"week","localisation":"12 weeks","firstrange":malojatime.thisweek().next(-11),"amount":12}, - {"identifier":"month","localisation":"12 months","firstrange":malojatime.thismonth().next(-11),"amount":12}, - {"identifier":"year","localisation":"12 years","firstrange":malojatime.thisyear().next(-11),"amount":12} - ], - "xcurrent": [ - {"identifier":"day","localisation":"Today","range":malojatime.today()}, - {"identifier":"week","localisation":"This Week","range":malojatime.thisweek()}, - {"identifier":"month","localisation":"This Month","range":malojatime.thismonth()}, - {"identifier":"year","localisation":"This Year","range":malojatime.thisyear()}, - {"identifier":"alltime","localisation":"All Time","range":malojatime.alltime()}, - ] -} - - -jinjaenv = Environment( - loader=PackageLoader('maloja', "web/jinja"), - autoescape=select_autoescape(['html', 'xml']) -) -jinjaenv.globals.update(JINJA_CONTEXT) -jinjaenv.filters.update({k:jinja_filters.__dict__[k] for k in jinja_filters.__dict__ if not k.startswith("__")}) @webserver.route("/") @@ -262,9 +217,9 @@ def static_html(name): lc = LOCAL_CONTEXT lc["filterkeys"], lc["limitkeys"], lc["delimitkeys"], lc["amountkeys"], lc["specialkeys"] = uri_to_internal(keys) - template = jinjaenv.get_template(name + '.jinja') - + template = jinja_environment.get_template(name + '.jinja') res = template.render(**LOCAL_CONTEXT) + log("Generated page {name} in {time:.5f}s (Jinja)".format(name=name,time=clock.stop()),module="debug_performance") return res