various fixes and changes

This commit is contained in:
Izalia Mae 2021-03-09 09:37:30 -05:00
parent 26156682c8
commit a55b32e2df
4 changed files with 32 additions and 22 deletions

View file

@ -143,10 +143,24 @@ class Session(object):
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())
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:
return RowClass(table_name, rows[0], self) if len(rows) > 0 else None

View file

@ -22,11 +22,9 @@ except ImportError:
try:
from sanic.request import Request as SanicRequest
from sanic.exceptions import SanicException
sanic_enabled = True
except ImportError:
logging.verbose('Sanic module not found. Request verification is disabled')
sanic_enabled = False
SanicRequest = False
Client = None
@ -191,7 +189,7 @@ def VerifyRequest(request: SanicRequest, actor: dict):
request: The request with the headers to verify
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')
return

View file

@ -255,23 +255,10 @@ class DotDict(dict):
def toJson(self, indent=None, **kwargs):
kwargs.pop('cls', None)
return json.dumps(dict(self), indent=indent, cls=DotDictEncoder, **kwargs)
if 'cls' not in kwargs:
kwargs['cls'] = DotDictEncoder
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)
return json.dumps(dict(self), indent=indent, **kwargs)
def fromJson(self, string):

View file

@ -62,6 +62,17 @@ class Template(Environment):
if tpl_path.str() not in self.search:
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):
self.globals[k] = v