59 lines
1.8 KiB
Python
59 lines
1.8 KiB
Python
from dataclasses import dataclass
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.models.mobile_settings import MOBILE_SETTINGS_ROW_ID, MobileSettings
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class MobileSettingsConfig:
|
|
devices_allowed: bool
|
|
max_devices_per_user: int
|
|
min_app_version: str | None
|
|
source: str = "db"
|
|
|
|
|
|
def get_effective_mobile_settings(db: Session) -> MobileSettingsConfig:
|
|
row = db.get(MobileSettings, MOBILE_SETTINGS_ROW_ID)
|
|
if row is None:
|
|
return MobileSettingsConfig(
|
|
devices_allowed=False,
|
|
max_devices_per_user=3,
|
|
min_app_version=None,
|
|
source="default",
|
|
)
|
|
return MobileSettingsConfig(
|
|
devices_allowed=bool(row.devices_allowed),
|
|
max_devices_per_user=max(1, int(row.max_devices_per_user or 3)),
|
|
min_app_version=(row.min_app_version or "").strip() or None,
|
|
source="db",
|
|
)
|
|
|
|
|
|
def upsert_mobile_settings(
|
|
db: Session,
|
|
*,
|
|
devices_allowed: bool,
|
|
max_devices_per_user: int,
|
|
min_app_version: str | None,
|
|
) -> MobileSettingsConfig:
|
|
if max_devices_per_user < 1 or max_devices_per_user > 50:
|
|
raise ValueError("max_devices_per_user must be between 1 and 50")
|
|
cleaned_version = (min_app_version or "").strip() or None
|
|
row = db.get(MobileSettings, MOBILE_SETTINGS_ROW_ID)
|
|
if row is None:
|
|
row = MobileSettings(
|
|
id=MOBILE_SETTINGS_ROW_ID,
|
|
devices_allowed=devices_allowed,
|
|
max_devices_per_user=max_devices_per_user,
|
|
min_app_version=cleaned_version,
|
|
)
|
|
db.add(row)
|
|
else:
|
|
row.devices_allowed = devices_allowed
|
|
row.max_devices_per_user = max_devices_per_user
|
|
row.min_app_version = cleaned_version
|
|
db.commit()
|
|
db.refresh(row)
|
|
return get_effective_mobile_settings(db)
|