Files
Document_editor/backend/app/models/template.py
2026-06-15 09:18:58 +03:00

55 lines
2.1 KiB
Python

from datetime import datetime
from sqlalchemy import DateTime, ForeignKey, Integer, String, Text
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.database import Base
class DocumentTemplate(Base):
__tablename__ = "document_templates"
id: Mapped[int] = mapped_column(primary_key=True, index=True)
name: Mapped[str] = mapped_column(String(255))
description: Mapped[str | None] = mapped_column(Text, nullable=True)
original_filename: Mapped[str] = mapped_column(String(255))
file_path: Mapped[str] = mapped_column(String(512))
owner_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"))
is_public: Mapped[bool] = mapped_column(default=False)
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
updated_at: Mapped[datetime] = mapped_column(
DateTime, default=datetime.utcnow, onupdate=datetime.utcnow
)
owner = relationship("User", back_populates="templates")
variables = relationship(
"TemplateVariable",
back_populates="template",
cascade="all, delete-orphan",
order_by="TemplateVariable.order",
)
documents = relationship(
"FilledDocument",
back_populates="template",
cascade="all, delete-orphan",
)
class TemplateVariable(Base):
__tablename__ = "template_variables"
id: Mapped[int] = mapped_column(primary_key=True, index=True)
template_id: Mapped[int] = mapped_column(
ForeignKey("document_templates.id", ondelete="CASCADE")
)
name: Mapped[str] = mapped_column(String(255))
label: Mapped[str] = mapped_column(String(255))
field_type: Mapped[str] = mapped_column(String(50))
default_value: Mapped[str | None] = mapped_column(Text, nullable=True)
is_required: Mapped[bool] = mapped_column(default=True)
order: Mapped[int] = mapped_column(Integer, default=0)
parent_variable: Mapped[str | None] = mapped_column(String(255), nullable=True)
style_params: Mapped[str | None] = mapped_column(Text, nullable=True)
template = relationship("DocumentTemplate", back_populates="variables")