mirror of
https://github.com/krateng/maloja.git
synced 2025-07-09 03:04:07 -04:00
Moved jinja handling to submodule
This commit is contained in:
parent
a5edc113c8
commit
22ee6bf751
72
maloja/jinjaenv/context.py
Normal file
72
maloja/jinjaenv/context.py
Normal file
@ -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("__")})
|
@ -39,3 +39,15 @@ def find_representative(sequence,attribute_id,attribute_count):
|
|||||||
#
|
#
|
||||||
# return mostappearances
|
# return mostappearances
|
||||||
# # among those, pick the one with the highest count in one of their appearances
|
# # 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)
|
@ -6,8 +6,7 @@ from .globalconf import datadir, DATA_DIR
|
|||||||
# server stuff
|
# server stuff
|
||||||
from bottle import Bottle, route, get, post, error, run, template, static_file, request, response, FormsDict, redirect, template, HTTPResponse, BaseRequest, abort
|
from bottle import Bottle, route, get, post, error, run, template, static_file, request, response, FormsDict, redirect, template, HTTPResponse, BaseRequest, abort
|
||||||
import waitress
|
import waitress
|
||||||
# templating
|
|
||||||
from jinja2 import Environment, PackageLoader, select_autoescape
|
|
||||||
# monkey patching
|
# monkey patching
|
||||||
from . import monkey
|
from . import monkey
|
||||||
# rest of the project
|
# rest of the project
|
||||||
@ -16,12 +15,13 @@ from . import htmlmodules
|
|||||||
from . import htmlgenerators
|
from . import htmlgenerators
|
||||||
from . import malojatime
|
from . import malojatime
|
||||||
from . import utilities
|
from . import utilities
|
||||||
|
from . import malojauri
|
||||||
from .utilities import resolveImage
|
from .utilities import resolveImage
|
||||||
from .urihandler import remove_identical
|
from .urihandler import remove_identical
|
||||||
from .malojauri import uri_to_internal
|
from .malojauri import uri_to_internal
|
||||||
from . import urihandler
|
from . import urihandler
|
||||||
from . import globalconf
|
from . import globalconf
|
||||||
from . import jinja_filters
|
from .jinjaenv.context import jinja_environment
|
||||||
# doreah toolkit
|
# doreah toolkit
|
||||||
from doreah import settings
|
from doreah import settings
|
||||||
from doreah.logging import log
|
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("/<name:re:admin.*>")
|
@webserver.route("/<name:re:admin.*>")
|
||||||
@ -262,9 +217,9 @@ def static_html(name):
|
|||||||
lc = LOCAL_CONTEXT
|
lc = LOCAL_CONTEXT
|
||||||
lc["filterkeys"], lc["limitkeys"], lc["delimitkeys"], lc["amountkeys"], lc["specialkeys"] = uri_to_internal(keys)
|
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)
|
res = template.render(**LOCAL_CONTEXT)
|
||||||
|
|
||||||
log("Generated page {name} in {time:.5f}s (Jinja)".format(name=name,time=clock.stop()),module="debug_performance")
|
log("Generated page {name} in {time:.5f}s (Jinja)".format(name=name,time=clock.stop()),module="debug_performance")
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user