Files
TieMeasureFlow/server/models/setting.py
T
Adriano d6508e0ae8 feat: FASE 1 - Backend Core (modelli, auth, API)
Implementazione completa del backend FastAPI:
- Modelli SQLAlchemy: User, Recipe, RecipeVersion, RecipeTask,
  RecipeSubtask, Measurement, AccessLog, SystemSetting, RecipeVersionAudit
- Schemas Pydantic v2 per tutti i CRUD + statistiche SPC
- Middleware: API Key auth (X-API-Key) con role checking + access logging
- Router: auth, users, recipes, tasks, measurements, files, settings
- Services: auth (bcrypt+secrets), recipe (copy-on-write versioning),
  measurement (auto pass/fail con UTL/UWL/LWL/LTL)
- Alembic env.py con import modelli attivi
- Fix architect review: no double-commit, recipe_id subquery filter,
  user_id in access logs, type annotations corrette

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-07 00:40:50 +01:00

61 lines
2.2 KiB
Python

"""SystemSetting and RecipeVersionAudit models."""
from datetime import datetime
from typing import Optional
from sqlalchemy import DateTime, Enum, ForeignKey, Index, Integer, String, Text, func
from sqlalchemy.orm import Mapped, mapped_column
from database import Base
class SystemSetting(Base):
__tablename__ = "system_settings"
setting_key: Mapped[str] = mapped_column(String(100), primary_key=True)
setting_value: Mapped[str] = mapped_column(Text, nullable=False)
setting_type: Mapped[str] = mapped_column(
Enum("string", "number", "boolean", "json", name="setting_type_enum"),
nullable=False,
default="string",
)
description: Mapped[Optional[str]] = mapped_column(String(500), nullable=True)
updated_at: Mapped[datetime] = mapped_column(
DateTime, nullable=False, server_default=func.now(), onupdate=func.now()
)
updated_by: Mapped[Optional[int]] = mapped_column(
Integer, ForeignKey("users.id"), nullable=True
)
def __repr__(self) -> str:
return f"<SystemSetting {self.setting_key}={self.setting_value}>"
class RecipeVersionAudit(Base):
__tablename__ = "recipe_version_audit"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
recipe_id: Mapped[int] = mapped_column(
Integer, ForeignKey("recipes.id"), nullable=False
)
old_version_id: Mapped[Optional[int]] = mapped_column(Integer, nullable=True)
new_version_id: Mapped[int] = mapped_column(Integer, nullable=False)
changed_by: Mapped[int] = mapped_column(
Integer, ForeignKey("users.id"), nullable=False
)
change_type: Mapped[str] = mapped_column(
Enum("CREATE", "UPDATE", "ACTIVATE", "RETIRE", name="change_type_enum"),
nullable=False,
)
change_reason: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
created_at: Mapped[datetime] = mapped_column(
DateTime, nullable=False, server_default=func.now()
)
__table_args__ = (
Index("ix_recipe_audit_recipe_time", "recipe_id", "created_at"),
{"mysql_engine": "InnoDB", "mysql_charset": "utf8mb4"},
)
def __repr__(self) -> str:
return f"<RecipeVersionAudit recipe={self.recipe_id} {self.change_type}>"