50 lines
1.4 KiB
Python
50 lines
1.4 KiB
Python
"""Setting model - key-value application settings."""
|
|
|
|
import secrets
|
|
from datetime import datetime
|
|
from sqlalchemy import String, func
|
|
from sqlalchemy.dialects.postgresql import JSONB
|
|
from sqlalchemy.orm import Mapped, mapped_column
|
|
|
|
from app.models.base import Base
|
|
|
|
|
|
def generate_api_key() -> str:
|
|
"""Generate a secure random API key."""
|
|
return secrets.token_urlsafe(32)
|
|
|
|
|
|
class Setting(Base):
|
|
"""Key-value setting storage."""
|
|
|
|
__tablename__ = "settings"
|
|
|
|
key: Mapped[str] = mapped_column(String(100), primary_key=True)
|
|
value: Mapped[dict] = mapped_column(JSONB, nullable=False)
|
|
updated_at: Mapped[datetime] = mapped_column(default=func.now(), onupdate=func.now())
|
|
|
|
def __repr__(self) -> str:
|
|
return f"<Setting {self.key}>"
|
|
|
|
def to_dict(self) -> dict:
|
|
"""Convert to dictionary for API responses."""
|
|
return {
|
|
"key": self.key,
|
|
"value": self.value,
|
|
"updated_at": self.updated_at.isoformat() if self.updated_at else None,
|
|
}
|
|
|
|
|
|
# Default settings that should exist
|
|
DEFAULT_SETTINGS = {
|
|
"download.parallel_limit": 3,
|
|
"download.rate_limit": 3.0,
|
|
"download.retry_count": 3,
|
|
"download.schedule_interval": 3600,
|
|
"notification.enabled": False,
|
|
"notification.webhook_url": None,
|
|
}
|
|
|
|
# Key for the extension API key setting
|
|
EXTENSION_API_KEY_SETTING = "security.extension_api_key"
|