From 349e0bb7ea7cebf55e48f620012bd7380db05ed7 Mon Sep 17 00:00:00 2001 From: krateng Date: Sat, 26 Feb 2022 21:07:25 +0100 Subject: [PATCH] Experimenting with DB connections in Jinja context --- maloja/database/jinjaview.py | 19 +++++++++++++++++++ maloja/database_packed.py | 28 ---------------------------- maloja/jinjaenv/context.py | 5 +++-- maloja/server.py | 31 +++++++++++++++++++------------ 4 files changed, 41 insertions(+), 42 deletions(-) create mode 100644 maloja/database/jinjaview.py diff --git a/maloja/database/jinjaview.py b/maloja/database/jinjaview.py new file mode 100644 index 0000000..7c90935 --- /dev/null +++ b/maloja/database/jinjaview.py @@ -0,0 +1,19 @@ + +from .. import database + +# this is a wrapper object that provides a DB connection so that one jinja page +# (with all its included partials) can use it for all functions +# it also translates the non-unpacked calls to unpacked calls that the DB wants +class JinjaDBConnection: + def __init__(self): + pass + def __getattr__(self,name): + originalmethod = getattr(database,name) + + def packedmethod(*keys): + kwargs = {} + for k in keys: + kwargs.update(k) + return originalmethod(**kwargs) + + return packedmethod diff --git a/maloja/database_packed.py b/maloja/database_packed.py index a41cade..e69de29 100644 --- a/maloja/database_packed.py +++ b/maloja/database_packed.py @@ -1,28 +0,0 @@ -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/jinjaenv/context.py b/maloja/jinjaenv/context.py index 3990400..ba1b2df 100644 --- a/maloja/jinjaenv/context.py +++ b/maloja/jinjaenv/context.py @@ -2,7 +2,8 @@ from .. import database_packed from . import filters from ..globalconf import malojaconfig -from .. import database, database_packed, malojatime, utilities, malojauri, thirdparty, __pkginfo__ +from .. import database, malojatime, utilities, malojauri, thirdparty, __pkginfo__ +from ..database import jinjaview from doreah.regular import repeatdaily import urllib @@ -11,7 +12,7 @@ import math # templating from jinja2 import Environment, PackageLoader, select_autoescape -dbp = database_packed.DB() +dbp = jinjaview.JinjaDBConnection() jinja_environment = Environment( loader=PackageLoader('maloja', "web/jinja"), diff --git a/maloja/server.py b/maloja/server.py index 6fa4040..8318bf8 100644 --- a/maloja/server.py +++ b/maloja/server.py @@ -18,6 +18,7 @@ from doreah import auth # rest of the project from . import database +from .database.jinjaview import JinjaDBConnection from .utilities import get_track_image, get_artist_image from .malojauri import uri_to_internal, remove_identical from .globalconf import malojaconfig, apikeystore, data_dir @@ -25,6 +26,7 @@ from .jinjaenv.context import jinja_environment from .apis import init_apis +from .proccontrol.profiler import profile ###### @@ -215,28 +217,33 @@ def static(name,ext): ### DYNAMIC +@profile def jinja_page(name): if name in aliases: redirect(aliases[name]) keys = remove_identical(FormsDict.decode(request.query)) adminmode = request.cookies.get("adminmode") == "true" and auth.check(request) + clock = Clock() clock.start() - loc_context = { - "adminmode":adminmode, - "apikey":request.cookies.get("apikey") if adminmode else None, - "apikeys":apikeystore, - "_urikeys":keys, #temporary! - } - loc_context["filterkeys"], loc_context["limitkeys"], loc_context["delimitkeys"], loc_context["amountkeys"], loc_context["specialkeys"] = uri_to_internal(keys) + with JinjaDBConnection() as conn: - template = jinja_environment.get_template(name + '.jinja') - try: - res = template.render(**loc_context) - except (ValueError, IndexError): - abort(404,"This Artist or Track does not exist") + loc_context = { + "dbc":conn, + "adminmode":adminmode, + "apikey":request.cookies.get("apikey") if adminmode else None, + "apikeys":apikeystore, + "_urikeys":keys, #temporary! + } + loc_context["filterkeys"], loc_context["limitkeys"], loc_context["delimitkeys"], loc_context["amountkeys"], loc_context["specialkeys"] = uri_to_internal(keys) + + template = jinja_environment.get_template(name + '.jinja') + try: + res = template.render(**loc_context) + except (ValueError, IndexError): + abort(404,"This Artist or Track does not exist") if malojaconfig["DEV_MODE"]: jinja_environment.cache.clear()