From 0b3324cd8ff9575439ab685291c23ec5ec9e2b61 Mon Sep 17 00:00:00 2001 From: Vlad Stan Date: Fri, 20 Jan 2023 18:10:29 +0200 Subject: [PATCH] refactor: create `TransientSettings` for settings that are not to be persisted --- lnbits/core/services.py | 3 ++- lnbits/settings.py | 20 ++++++++++++++------ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/lnbits/core/services.py b/lnbits/core/services.py index eefb2f990..a74745bd2 100644 --- a/lnbits/core/services.py +++ b/lnbits/core/services.py @@ -22,6 +22,7 @@ from lnbits.settings import ( readonly_variables, send_admin_user_to_saas, settings, + transient_variables, ) from lnbits.wallets.base import PaymentResponse, PaymentStatus @@ -449,7 +450,7 @@ async def check_admin_settings(): def update_cached_settings(sets_dict: dict): for key, value in sets_dict.items(): - if not key in readonly_variables: + if not key in readonly_variables + transient_variables: try: setattr(settings, key, value) except: diff --git a/lnbits/settings.py b/lnbits/settings.py index 5c1b017ce..abf22c481 100644 --- a/lnbits/settings.py +++ b/lnbits/settings.py @@ -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( - EnvSettings, - SaaSSettings, - PersistenceSettings, - SuperUserSettings, - InstalledExtensionsSettings, + EnvSettings, SaaSSettings, PersistenceSettings, SuperUserSettings ): 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("_")] -class Settings(EditableSettings, ReadOnlySettings): +class Settings(EditableSettings, ReadOnlySettings, TransientSettings): @classmethod def from_row(cls, row: Row) -> "Settings": data = dict(row) @@ -324,6 +331,7 @@ def send_admin_user_to_saas(): ############### INIT ################# readonly_variables = ReadOnlySettings.readonly_fields() +transient_variables = TransientSettings.readonly_fields() settings = Settings()