diff --git a/IzzyLib/database.py b/IzzyLib/database.py index 232436d..05a28ae 100644 --- a/IzzyLib/database.py +++ b/IzzyLib/database.py @@ -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 diff --git a/IzzyLib/http.py b/IzzyLib/http.py index 3012e96..4441686 100644 --- a/IzzyLib/http.py +++ b/IzzyLib/http.py @@ -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 diff --git a/IzzyLib/misc.py b/IzzyLib/misc.py index fc45464..f9dcc1c 100644 --- a/IzzyLib/misc.py +++ b/IzzyLib/misc.py @@ -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): diff --git a/IzzyLib/template.py b/IzzyLib/template.py index 2a7ba08..c49f683 100644 --- a/IzzyLib/template.py +++ b/IzzyLib/template.py @@ -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