feat: allow arbitrary kwargs for config.get()
and app.get_setting()
also `app.save_setting()` and `app.delete_setting()`
This commit is contained in:
parent
e38c45b15a
commit
988749d80e
2 changed files with 20 additions and 6 deletions
|
@ -586,7 +586,7 @@ class AppHandler:
|
|||
|
||||
return short_session(**kwargs)
|
||||
|
||||
def get_setting(self, session, name):
|
||||
def get_setting(self, session, name, **kwargs):
|
||||
"""
|
||||
Get a :term:`config setting` value from the DB.
|
||||
|
||||
|
@ -602,6 +602,9 @@ class AppHandler:
|
|||
|
||||
:param name: Name of the setting to get.
|
||||
|
||||
:param \\**kwargs: Any remaining kwargs are ignored by the
|
||||
default logic, but subclass may override.
|
||||
|
||||
:returns: Setting value as string, or ``None``.
|
||||
"""
|
||||
from .db import get_setting
|
||||
|
@ -614,6 +617,7 @@ class AppHandler:
|
|||
name,
|
||||
value,
|
||||
force_create=False,
|
||||
**kwargs
|
||||
):
|
||||
"""
|
||||
Save a :term:`config setting` value to the DB.
|
||||
|
@ -636,6 +640,9 @@ class AppHandler:
|
|||
exists.
|
||||
|
||||
(Theoretically the latter offers a slight efficiency gain.)
|
||||
|
||||
:param \\**kwargs: Any remaining kwargs are ignored by the
|
||||
default logic, but subclass may override.
|
||||
"""
|
||||
model = self.model
|
||||
|
||||
|
@ -652,7 +659,7 @@ class AppHandler:
|
|||
# set value
|
||||
setting.value = value
|
||||
|
||||
def delete_setting(self, session, name):
|
||||
def delete_setting(self, session, name, **kwargs):
|
||||
"""
|
||||
Delete a :term:`config setting` from the DB.
|
||||
|
||||
|
@ -661,6 +668,9 @@ class AppHandler:
|
|||
:param session: Current :term:`db session`.
|
||||
|
||||
:param name: Name of the setting to delete.
|
||||
|
||||
:param \\**kwargs: Any remaining kwargs are ignored by the
|
||||
default logic, but subclass may override.
|
||||
"""
|
||||
model = self.model
|
||||
setting = session.get(model.Setting, name)
|
||||
|
|
|
@ -334,6 +334,7 @@ class WuttaConfig:
|
|||
usedb=None,
|
||||
preferdb=None,
|
||||
session=None,
|
||||
**kwargs
|
||||
):
|
||||
"""
|
||||
Retrieve a string value from config.
|
||||
|
@ -414,6 +415,9 @@ class WuttaConfig:
|
|||
:param session: Optional SQLAlchemy session to use for DB lookups.
|
||||
NOTE: This param is not yet implemented; currently ignored.
|
||||
|
||||
:param \\**kwargs: Any remaining kwargs are passed as-is to
|
||||
the :meth:`get_from_db()` call, if applicable.
|
||||
|
||||
:returns: Value as string.
|
||||
|
||||
"""
|
||||
|
@ -428,7 +432,7 @@ class WuttaConfig:
|
|||
|
||||
# read from db first if so requested
|
||||
if usedb and preferdb:
|
||||
value = self.get_from_db(key, session=session)
|
||||
value = self.get_from_db(key, session=session, **kwargs)
|
||||
if value is not None:
|
||||
return value
|
||||
|
||||
|
@ -448,7 +452,7 @@ class WuttaConfig:
|
|||
|
||||
# read from db last if so requested
|
||||
if usedb and not preferdb:
|
||||
value = self.get_from_db(key, session=session)
|
||||
value = self.get_from_db(key, session=session, **kwargs)
|
||||
if value is not None:
|
||||
return value
|
||||
|
||||
|
@ -461,7 +465,7 @@ class WuttaConfig:
|
|||
if default is not UNSPECIFIED:
|
||||
return default
|
||||
|
||||
def get_from_db(self, key, session=None):
|
||||
def get_from_db(self, key, session=None, **kwargs):
|
||||
"""
|
||||
Retrieve a config value from database settings table.
|
||||
|
||||
|
@ -470,7 +474,7 @@ class WuttaConfig:
|
|||
"""
|
||||
app = self.get_app()
|
||||
with app.short_session(session=session) as s:
|
||||
return app.get_setting(s, key)
|
||||
return app.get_setting(s, key, **kwargs)
|
||||
|
||||
def require(self, *args, **kwargs):
|
||||
"""
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue