refactor: create TransientSettings for settings that are not to be persisted

This commit is contained in:
Vlad Stan
2023-01-20 18:10:29 +02:00
parent 2dff48dcd0
commit 0b3324cd8f
2 changed files with 16 additions and 7 deletions

View File

@@ -22,6 +22,7 @@ from lnbits.settings import (
readonly_variables, readonly_variables,
send_admin_user_to_saas, send_admin_user_to_saas,
settings, settings,
transient_variables,
) )
from lnbits.wallets.base import PaymentResponse, PaymentStatus from lnbits.wallets.base import PaymentResponse, PaymentStatus
@@ -449,7 +450,7 @@ async def check_admin_settings():
def update_cached_settings(sets_dict: dict): def update_cached_settings(sets_dict: dict):
for key, value in sets_dict.items(): for key, value in sets_dict.items():
if not key in readonly_variables: if not key in readonly_variables + transient_variables:
try: try:
setattr(settings, key, value) setattr(settings, key, value)
except: except:

View File

@@ -243,12 +243,19 @@ class SuperUserSettings(LNbitsSettings):
) )
class TransientSettings(InstalledExtensionsSettings):
# Transient Settings:
# - are initialized, updated and used at runtime
# - are not read from a file or from the `setings` table
# - are not persisted in the `settings` table when the setings are updated
@classmethod
def readonly_fields(cls):
return [f for f in inspect.signature(cls).parameters if not f.startswith("_")]
class ReadOnlySettings( class ReadOnlySettings(
EnvSettings, EnvSettings, SaaSSettings, PersistenceSettings, SuperUserSettings
SaaSSettings,
PersistenceSettings,
SuperUserSettings,
InstalledExtensionsSettings,
): ):
lnbits_admin_ui: bool = Field(default=False) lnbits_admin_ui: bool = Field(default=False)
@@ -264,7 +271,7 @@ class ReadOnlySettings(
return [f for f in inspect.signature(cls).parameters if not f.startswith("_")] return [f for f in inspect.signature(cls).parameters if not f.startswith("_")]
class Settings(EditableSettings, ReadOnlySettings): class Settings(EditableSettings, ReadOnlySettings, TransientSettings):
@classmethod @classmethod
def from_row(cls, row: Row) -> "Settings": def from_row(cls, row: Row) -> "Settings":
data = dict(row) data = dict(row)
@@ -324,6 +331,7 @@ def send_admin_user_to_saas():
############### INIT ################# ############### INIT #################
readonly_variables = ReadOnlySettings.readonly_fields() readonly_variables = ReadOnlySettings.readonly_fields()
transient_variables = TransientSettings.readonly_fields()
settings = Settings() settings = Settings()