various fixes and changes
This commit is contained in:
parent
26156682c8
commit
a55b32e2df
|
@ -143,10 +143,24 @@ class Session(object):
|
||||||
return self.query(self.table[table_name]).filter_by(**kwargs).count()
|
return self.query(self.table[table_name]).filter_by(**kwargs).count()
|
||||||
|
|
||||||
|
|
||||||
def fetch(self, table_name, single=True, **kwargs):
|
def fetch(self, table_name, single=True, orderby=None, orderdir='asc', **kwargs):
|
||||||
|
table = self.table[table_name]
|
||||||
RowClass = self.classes.get(table_name.capitalize())
|
RowClass = self.classes.get(table_name.capitalize())
|
||||||
|
|
||||||
rows = self.query(self.table[table_name]).filter_by(**kwargs).all()
|
query = self.query(table).filter_by(**kwargs)
|
||||||
|
|
||||||
|
if not orderby:
|
||||||
|
rows = query.all()
|
||||||
|
|
||||||
|
else:
|
||||||
|
if orderdir == 'asc':
|
||||||
|
rows = query.order_by(getattr(table.c, orderby).asc()).all()
|
||||||
|
|
||||||
|
elif orderdir == 'desc':
|
||||||
|
rows = query.order_by(getattr(table.c, orderby).asc()).all()
|
||||||
|
|
||||||
|
else:
|
||||||
|
raise ValueError(f'Unsupported order direction: {orderdir}')
|
||||||
|
|
||||||
if single:
|
if single:
|
||||||
return RowClass(table_name, rows[0], self) if len(rows) > 0 else None
|
return RowClass(table_name, rows[0], self) if len(rows) > 0 else None
|
||||||
|
|
|
@ -22,11 +22,9 @@ except ImportError:
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from sanic.request import Request as SanicRequest
|
from sanic.request import Request as SanicRequest
|
||||||
from sanic.exceptions import SanicException
|
|
||||||
sanic_enabled = True
|
|
||||||
except ImportError:
|
except ImportError:
|
||||||
logging.verbose('Sanic module not found. Request verification is disabled')
|
logging.verbose('Sanic module not found. Request verification is disabled')
|
||||||
sanic_enabled = False
|
SanicRequest = False
|
||||||
|
|
||||||
|
|
||||||
Client = None
|
Client = None
|
||||||
|
@ -191,7 +189,7 @@ def VerifyRequest(request: SanicRequest, actor: dict):
|
||||||
request: The request with the headers to verify
|
request: The request with the headers to verify
|
||||||
actor: A dictionary containing the activitypub actor and the link to the pubkey used for verification
|
actor: A dictionary containing the activitypub actor and the link to the pubkey used for verification
|
||||||
'''
|
'''
|
||||||
if not sanic_enabled:
|
if not SanicRequest:
|
||||||
logging.error('Sanic request verification disabled')
|
logging.error('Sanic request verification disabled')
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
|
@ -255,23 +255,10 @@ class DotDict(dict):
|
||||||
|
|
||||||
|
|
||||||
def toJson(self, indent=None, **kwargs):
|
def toJson(self, indent=None, **kwargs):
|
||||||
kwargs.pop('cls', None)
|
if 'cls' not in kwargs:
|
||||||
return json.dumps(dict(self), indent=indent, cls=DotDictEncoder, **kwargs)
|
kwargs['cls'] = DotDictEncoder
|
||||||
|
|
||||||
|
return json.dumps(dict(self), indent=indent, **kwargs)
|
||||||
def toJson2(self, indent=None, **kwargs):
|
|
||||||
data = {}
|
|
||||||
|
|
||||||
for k,v in self.items():
|
|
||||||
if k and not type(k) in [str, int, float, dict]:
|
|
||||||
k = str(k)
|
|
||||||
|
|
||||||
if v and not type(k) in [str, int, float, dict]:
|
|
||||||
v = str(v)
|
|
||||||
|
|
||||||
data[k] = v
|
|
||||||
|
|
||||||
return json.dumps(data, indent=indent, **kwargs)
|
|
||||||
|
|
||||||
|
|
||||||
def fromJson(self, string):
|
def fromJson(self, string):
|
||||||
|
|
|
@ -62,6 +62,17 @@ class Template(Environment):
|
||||||
if tpl_path.str() not in self.search:
|
if tpl_path.str() not in self.search:
|
||||||
self.search.append(tpl_path.str())
|
self.search.append(tpl_path.str())
|
||||||
|
|
||||||
|
def setContext(self, context):
|
||||||
|
if not hasattr(context, '__call__'):
|
||||||
|
logging.error('Context is not callable')
|
||||||
|
return
|
||||||
|
|
||||||
|
if not isinstance(context({}, {}), dict):
|
||||||
|
logging.error('Context does not return a dict or dict-like object')
|
||||||
|
return
|
||||||
|
|
||||||
|
self.func_context = context
|
||||||
|
|
||||||
|
|
||||||
def addEnv(self, k, v):
|
def addEnv(self, k, v):
|
||||||
self.globals[k] = v
|
self.globals[k] = v
|
||||||
|
|
Loading…
Reference in a new issue