| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- """主案ACL借阅授权模型(0026迁移:面向用户的限时借阅)。"""
- from __future__ import annotations
- from datetime import datetime
- from sqlalchemy import (
- BigInteger,
- Boolean,
- Computed,
- DateTime,
- ForeignKey,
- Index,
- Integer,
- String,
- UniqueConstraint,
- text,
- )
- from sqlalchemy.orm import Mapped, mapped_column
- from dms.database.base import BusinessTableMixin
- from dms.extensions import db
- class Permission(BusinessTableMixin, db.Model):
- __tablename__ = "doc_permission"
- __table_args__ = (
- UniqueConstraint(
- "document_id",
- "subject_id",
- "active_marker",
- name="uq_doc_permission_active",
- ),
- Index(
- "ix_doc_permission_document_active",
- "document_id",
- "is_deleted",
- ),
- Index(
- "ix_doc_permission_subject_active",
- "subject_id",
- "is_deleted",
- ),
- )
- document_id: Mapped[int] = mapped_column(
- BigInteger,
- ForeignKey("doc_document.id", ondelete="RESTRICT"),
- nullable=False,
- )
- subject_id: Mapped[int] = mapped_column(BigInteger, nullable=False)
- subject_name: Mapped[str] = mapped_column(String(255), nullable=False)
- can_view: Mapped[bool] = mapped_column(
- Boolean,
- nullable=False,
- server_default=text("1"),
- )
- can_download: Mapped[bool] = mapped_column(
- Boolean,
- nullable=False,
- server_default=text("0"),
- )
- # 借阅到期时刻:服务层强制必填且不早于当前、不晚于当前+7天;
- # 1970 哨兵默认=fail-safe,漏赋值路径插入的行立即过期拒绝访问
- expires_at: Mapped[datetime] = mapped_column(
- DateTime,
- nullable=False,
- server_default=text("'1970-01-01 00:00:00'"),
- )
- # 审批单凭证(每条授权一份PDF,可空)
- voucher_file_name: Mapped[str | None] = mapped_column(
- String(255), nullable=True
- )
- voucher_storage_path: Mapped[str | None] = mapped_column(
- String(512), nullable=True
- )
- voucher_file_size: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
- voucher_uploaded_at: Mapped[datetime | None] = mapped_column(
- DateTime, nullable=True
- )
- active_marker: Mapped[int | None] = mapped_column(
- Integer,
- Computed("IF(is_deleted = 0, 1, NULL)", persisted=False),
- nullable=True,
- )
|