add command params
This commit is contained in:
@@ -6,7 +6,7 @@ from typing import Any, get_args, get_origin
|
||||
|
||||
from ..db import async_session
|
||||
from .role import RoleBase
|
||||
from .descriptors import EntityFieldDescriptor, Setting
|
||||
from .descriptors import FieldDescriptor, Setting
|
||||
from ..utils.serialization import deserialize, serialize
|
||||
|
||||
import ujson as json
|
||||
@@ -39,7 +39,7 @@ class SettingsMetaclass(type):
|
||||
if isinstance(attr_value, Setting):
|
||||
descriptor_kwargs = attr_value.__dict__.copy()
|
||||
name = descriptor_kwargs.pop("name") or annotation
|
||||
attributes[annotation] = EntityFieldDescriptor(
|
||||
attributes[annotation] = FieldDescriptor(
|
||||
name=name,
|
||||
field_name=annotation,
|
||||
type_=type_,
|
||||
@@ -48,7 +48,7 @@ class SettingsMetaclass(type):
|
||||
)
|
||||
|
||||
else:
|
||||
attributes[annotation] = EntityFieldDescriptor(
|
||||
attributes[annotation] = FieldDescriptor(
|
||||
name=annotation,
|
||||
field_name=annotation,
|
||||
type_=type_,
|
||||
@@ -83,7 +83,7 @@ class SettingsMetaclass(type):
|
||||
|
||||
class Settings(metaclass=SettingsMetaclass):
|
||||
_cache: dict[str, Any] = dict[str, Any]()
|
||||
_settings_descriptors: dict[str, EntityFieldDescriptor] = {}
|
||||
_settings_descriptors: dict[str, FieldDescriptor] = {}
|
||||
|
||||
PAGE_SIZE: int = Setting(
|
||||
default=10,
|
||||
@@ -213,7 +213,7 @@ class Settings(metaclass=SettingsMetaclass):
|
||||
return ret_val
|
||||
|
||||
@classmethod
|
||||
async def load_param(cls, param: EntityFieldDescriptor) -> Any:
|
||||
async def load_param(cls, param: FieldDescriptor) -> Any:
|
||||
async with async_session() as session:
|
||||
db_setting = (
|
||||
await session.exec(
|
||||
@@ -244,7 +244,7 @@ class Settings(metaclass=SettingsMetaclass):
|
||||
db_settings = (await session.exec(select(DbSettings))).all()
|
||||
for db_setting in db_settings:
|
||||
if db_setting.name in cls.__dict__:
|
||||
setting = cls.__dict__[db_setting.name] # type: EntityFieldDescriptor
|
||||
setting = cls.__dict__[db_setting.name] # type: FieldDescriptor
|
||||
cls._cache[db_setting.name] = await deserialize(
|
||||
session=session,
|
||||
type_=setting.type_,
|
||||
@@ -255,7 +255,7 @@ class Settings(metaclass=SettingsMetaclass):
|
||||
cls._loaded = True
|
||||
|
||||
@classmethod
|
||||
async def set_param(cls, param: str | EntityFieldDescriptor, value) -> None:
|
||||
async def set_param(cls, param: str | FieldDescriptor, value) -> None:
|
||||
if isinstance(param, str):
|
||||
param = cls._settings_descriptors[param]
|
||||
ser_value = serialize(value, param)
|
||||
@@ -273,11 +273,11 @@ class Settings(metaclass=SettingsMetaclass):
|
||||
cls._cache[param.field_name] = value
|
||||
|
||||
@classmethod
|
||||
def list_params(cls) -> dict[str, EntityFieldDescriptor]:
|
||||
def list_params(cls) -> dict[str, FieldDescriptor]:
|
||||
return cls._settings_descriptors
|
||||
|
||||
@classmethod
|
||||
async def get_params(cls) -> dict[EntityFieldDescriptor, Any]:
|
||||
async def get_params(cls) -> dict[FieldDescriptor, Any]:
|
||||
params = cls.list_params()
|
||||
return {
|
||||
param: await cls.get(param, all_locales=True) for _, param in params.items()
|
||||
|
||||
Reference in New Issue
Block a user