mirror of
https://github.com/krateng/maloja.git
synced 2023-08-10 21:12:55 +03:00
Experimenting with DB connections in Jinja context
This commit is contained in:
parent
4cd16d73d3
commit
349e0bb7ea
19
maloja/database/jinjaview.py
Normal file
19
maloja/database/jinjaview.py
Normal file
@ -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
|
@ -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)
|
@ -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"),
|
||||
|
@ -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()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user