-
Notifications
You must be signed in to change notification settings - Fork 1
/
dynamic_settings.py
35 lines (28 loc) · 1.38 KB
/
dynamic_settings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from datetime import datetime
from sqlalchemy import DateTime, Double, Integer, String
from sqlalchemy.orm import Mapped, Session, mapped_column
from .base import Base
class DynamicOption(Base):
id: Mapped[int] = mapped_column(Integer, primary_key=True)
name: Mapped[str] = mapped_column(String, unique=True)
value_integer: Mapped[int] = mapped_column(Integer, nullable=True)
value_double: Mapped[float] = mapped_column(Double, nullable=True)
value_string: Mapped[str] = mapped_column(String, nullable=True)
create_ts: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
update_ts: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
@property
def value(self) -> str | float | int:
return self.value_double or self.value_integer or self.value_string
@value.setter
def set_value(self, value: str | float | int):
if isinstance(value, str):
self.value_string = value
elif isinstance(value, float):
self.value_double = value
elif isinstance(value, int):
self.value_integer = value
else:
raise TypeError("Only str, float or int options allowed")
@staticmethod
def get(name, default=None, *, session: Session):
return session.query(DynamicOption).filter(DynamicOption.name == name).one_or_none() or default