[FEAT] improve update_admin_settings (#1903)

* [FEAT] improve update_admin_settings

while working on the push notification pr i found it very hard just to update
2 settings inside the db, so i improved upon update_admin_settings.
now you just need to provide a dict with key/values you want to update inside db.

also debugging the endpoints for update_settings i found despite the type of `EditableSettings`
fastapi did in fact pass a dict.

* t

* use `EditableSettings` as param in update_settings

* fix settings model validation

we previously overrode the pydantic validation with our own method

* make `LnbitsSettings` a `BaseModel` and only add `BaseSettings` later

this allows us to instantiate `EditableSettings` without the environment values being loaded in

* add test

* forbid extra fields in update api

* fixup

* add test

* test datadir

* move UpdateSettings

* fix compat

* fixup webpush

---------

Co-authored-by: jacksn <jkranawetter05@gmail.com>
This commit is contained in:
dni ⚡
2023-09-12 10:59:32 +01:00
committed by GitHub
co-authored by jacksn
parent 1b2db34e08
commit bda054415a
6 changed files with 76 additions and 25 deletions
+17 -13
View File
@@ -9,7 +9,7 @@ from typing import Any, List, Optional
import httpx
from loguru import logger
from pydantic import BaseSettings, Extra, Field, validator
from pydantic import BaseModel, BaseSettings, Extra, Field, validator
def list_parse_fallback(v: str):
@@ -23,20 +23,13 @@ def list_parse_fallback(v: str):
return []
class LNbitsSettings(BaseSettings):
class LNbitsSettings(BaseModel):
@classmethod
def validate(cls, val):
def validate_list(cls, val):
if isinstance(val, str):
val = val.split(",") if val else []
return val
class Config:
env_file = ".env"
env_file_encoding = "utf-8"
case_sensitive = False
json_loads = list_parse_fallback
extra = Extra.ignore
class UsersSettings(LNbitsSettings):
lnbits_admin_users: List[str] = Field(default=[])
@@ -253,7 +246,7 @@ class EditableSettings(
)
@classmethod
def validate_editable_settings(cls, val):
return super().validate(val)
return super().validate_list(val)
@classmethod
def from_dict(cls, d: dict):
@@ -269,6 +262,11 @@ class EditableSettings(
prop.pop("env_names", None)
class UpdateSettings(EditableSettings):
class Config:
extra = Extra.forbid
class EnvSettings(LNbitsSettings):
debug: bool = Field(default=False)
bundle_assets: bool = Field(default=True)
@@ -338,19 +336,25 @@ class ReadOnlySettings(
)
@classmethod
def validate_readonly_settings(cls, val):
return super().validate(val)
return super().validate_list(val)
@classmethod
def readonly_fields(cls):
return [f for f in inspect.signature(cls).parameters if not f.startswith("_")]
class Settings(EditableSettings, ReadOnlySettings, TransientSettings):
class Settings(EditableSettings, ReadOnlySettings, TransientSettings, BaseSettings):
@classmethod
def from_row(cls, row: Row) -> "Settings":
data = dict(row)
return cls(**data)
class Config:
env_file = ".env"
env_file_encoding = "utf-8"
case_sensitive = False
json_loads = list_parse_fallback
class SuperSettings(EditableSettings):
super_user: str