Files
Pre_Seg_Server/backend/models.py
admin afcddfaeb9 feat: 完善分割工作区导入导出与管理流程
- 新增基于 JWT 当前用户的登录恢复、角色权限、用户管理、审计日志和演示出厂重置后台接口与前端管理页。

- 重串 GT_label 导出和 GT Mask 导入逻辑:导出保留类别真实 maskid,导入仅接受灰度或 RGB 等通道 maskid 图,支持未知 maskid 策略、尺寸最近邻拉伸和导入预览。

- 统一分割结果导出体验:默认当前帧,按项目抽帧顺序和 XhXXmXXsXXXms 时间戳命名 ZIP 与图片,补齐 GT/Pro/Mix/分开 Mask 输出和映射 JSON。

- 调整工作区左侧工具栏:移除创建点/线段入口,新增画笔、橡皮擦及尺寸控制,并按绘制、布尔、导入/AI 工具分组分隔。

- 扩展 Canvas 编辑能力:画笔按语义分类绘制并可自动并入连通选中 mask,橡皮擦对选中区域扣除,优化布尔操作、选区、撤销重做和保存状态联动。

- 优化自动传播时间轴显示:同一蓝色系按传播新旧递进变暗,老传播记录达到阈值后统一旧记录色,并维护范围选择与清空后的历史显示。

- 将 AI 智能分割入口替换为更明确的 AI 元素图标,并同步侧栏、工作区和 AI 页面入口表现。

- 完善模板分类、maskid 工具函数、分类树联动、遮罩透明度、边缘平滑和传播链同步相关前端状态。

- 扩展后端项目、媒体、任务、Dashboard、模板和传播 runner 的用户隔离、任务控制、进度事件与兼容处理。

- 补充前后端测试,覆盖用户管理、GT_label 往返导入导出、GT Mask 校验和预览、画笔/橡皮擦、时间轴传播历史、导出范围、WebSocket 与 API 封装。

- 更新 AGENTS、README 和 doc 文档,记录当前接口契约、实现状态、测试计划、安装说明和 maskid/GT_label 规则。
2026-05-03 03:52:32 +08:00

196 lines
7.1 KiB
Python

"""SQLAlchemy ORM models."""
from sqlalchemy import (
Column,
Integer,
String,
Text,
DateTime,
ForeignKey,
JSON,
Float,
)
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
from database import Base
from statuses import PROJECT_STATUS_PENDING
class User(Base):
"""Application user used for authentication and data ownership."""
__tablename__ = "users"
id = Column(Integer, primary_key=True, index=True)
username = Column(String(150), unique=True, index=True, nullable=False)
password_hash = Column(String(255), nullable=False)
role = Column(String(50), default="admin", nullable=False)
is_active = Column(Integer, default=1, nullable=False)
created_at = Column(DateTime(timezone=True), server_default=func.now())
updated_at = Column(
DateTime(timezone=True), server_default=func.now(), onupdate=func.now()
)
projects = relationship("Project", back_populates="owner")
templates = relationship("Template", back_populates="owner")
class Project(Base):
"""Project model representing a segmentation project."""
__tablename__ = "projects"
id = Column(Integer, primary_key=True, index=True)
name = Column(String(255), nullable=False)
description = Column(Text, nullable=True)
video_path = Column(String(512), nullable=True)
thumbnail_url = Column(String(512), nullable=True)
status = Column(String(50), default=PROJECT_STATUS_PENDING, nullable=False)
source_type = Column(String(20), default="video", nullable=False) # video | dicom
original_fps = Column(Float, nullable=True)
parse_fps = Column(Float, default=30.0, nullable=False)
owner_user_id = Column(Integer, ForeignKey("users.id", ondelete="SET NULL"), nullable=True)
created_at = Column(DateTime(timezone=True), server_default=func.now())
updated_at = Column(
DateTime(timezone=True), server_default=func.now(), onupdate=func.now()
)
owner = relationship("User", back_populates="projects")
frames = relationship("Frame", back_populates="project", cascade="all, delete-orphan")
annotations = relationship(
"Annotation", back_populates="project", cascade="all, delete-orphan"
)
tasks = relationship(
"ProcessingTask", back_populates="project", cascade="all, delete-orphan"
)
class Frame(Base):
"""Frame model representing an extracted video frame."""
__tablename__ = "frames"
id = Column(Integer, primary_key=True, index=True)
project_id = Column(Integer, ForeignKey("projects.id", ondelete="CASCADE"), nullable=False)
frame_index = Column(Integer, nullable=False)
image_url = Column(String(512), nullable=False)
width = Column(Integer, nullable=True)
height = Column(Integer, nullable=True)
timestamp_ms = Column(Float, nullable=True)
source_frame_number = Column(Integer, nullable=True)
created_at = Column(DateTime(timezone=True), server_default=func.now())
project = relationship("Project", back_populates="frames")
annotations = relationship(
"Annotation", back_populates="frame", cascade="all, delete-orphan"
)
class Template(Base):
"""Template (Ontology) model for segmentation classes."""
__tablename__ = "templates"
id = Column(Integer, primary_key=True, index=True)
name = Column(String(255), nullable=False)
description = Column(Text, nullable=True)
color = Column(String(50), nullable=False)
z_index = Column(Integer, default=0, nullable=False)
mapping_rules = Column(JSON, nullable=True)
owner_user_id = Column(Integer, ForeignKey("users.id", ondelete="SET NULL"), nullable=True)
created_at = Column(DateTime(timezone=True), server_default=func.now())
owner = relationship("User", back_populates="templates")
annotations = relationship(
"Annotation", back_populates="template", cascade="all, delete-orphan"
)
class Annotation(Base):
"""Annotation model for segmentation masks and prompts."""
__tablename__ = "annotations"
id = Column(Integer, primary_key=True, index=True)
project_id = Column(
Integer, ForeignKey("projects.id", ondelete="CASCADE"), nullable=False
)
frame_id = Column(
Integer, ForeignKey("frames.id", ondelete="CASCADE"), nullable=True
)
template_id = Column(
Integer, ForeignKey("templates.id", ondelete="SET NULL"), nullable=True
)
mask_data = Column(JSON, nullable=True)
points = Column(JSON, nullable=True)
bbox = Column(JSON, nullable=True)
created_at = Column(DateTime(timezone=True), server_default=func.now())
updated_at = Column(
DateTime(timezone=True), server_default=func.now(), onupdate=func.now()
)
project = relationship("Project", back_populates="annotations")
frame = relationship("Frame", back_populates="annotations")
template = relationship("Template", back_populates="annotations")
masks = relationship("Mask", back_populates="annotation", cascade="all, delete-orphan")
class Mask(Base):
"""Mask model for exported/derived mask files."""
__tablename__ = "masks"
id = Column(Integer, primary_key=True, index=True)
annotation_id = Column(
Integer, ForeignKey("annotations.id", ondelete="CASCADE"), nullable=False
)
mask_url = Column(String(512), nullable=False)
format = Column(String(50), default="png", nullable=False) # png / rle / json
created_at = Column(DateTime(timezone=True), server_default=func.now())
annotation = relationship("Annotation", back_populates="masks")
class AuditLog(Base):
"""Audit trail for security and administrative actions."""
__tablename__ = "audit_logs"
id = Column(Integer, primary_key=True, index=True)
actor_user_id = Column(Integer, ForeignKey("users.id", ondelete="SET NULL"), nullable=True)
action = Column(String(120), nullable=False)
target_type = Column(String(80), nullable=True)
target_id = Column(String(120), nullable=True)
detail = Column(JSON, nullable=True)
created_at = Column(DateTime(timezone=True), server_default=func.now())
actor = relationship("User")
class ProcessingTask(Base):
"""Background task state persisted for dashboard and polling."""
__tablename__ = "processing_tasks"
id = Column(Integer, primary_key=True, index=True)
task_type = Column(String(80), nullable=False)
status = Column(String(40), default="queued", nullable=False)
progress = Column(Integer, default=0, nullable=False)
message = Column(Text, nullable=True)
project_id = Column(
Integer, ForeignKey("projects.id", ondelete="CASCADE"), nullable=True
)
celery_task_id = Column(String(255), nullable=True)
payload = Column(JSON, nullable=True)
result = Column(JSON, nullable=True)
error = Column(Text, nullable=True)
created_at = Column(DateTime(timezone=True), server_default=func.now())
started_at = Column(DateTime(timezone=True), nullable=True)
finished_at = Column(DateTime(timezone=True), nullable=True)
updated_at = Column(
DateTime(timezone=True), server_default=func.now(), onupdate=func.now()
)
project = relationship("Project", back_populates="tasks")