Download src/core/tools/document/models.py from anaselamr/yaz: direct link, hf CLI and curl.
- Browser
- Download file 11 kB
-
https://huggingface.co/spaces/anaselamr/yaz/resolve/main/src/core/tools/document/models.py
- Command line
-
hf download hf://spaces/anaselamr/yaz/src/core/tools/document/models.py
-
curl -L -o models.py https://huggingface.co/spaces/anaselamr/yaz/resolve/main/src/core/tools/document/models.py
11 kB
| """ | |
| Document Tool - Data Models | |
| ============================== | |
| Domain-agnostic document/evidence lifecycle management with versioning, | |
| multi-signer workflows, and storage abstraction. | |
| Entities: | |
| - DocumentDefinition: Reusable document template (e.g., "Operative Report") | |
| - Document: Concrete document instance with content and metadata | |
| - DocumentVersion: Immutable version record (content-addressed) | |
| - DocumentSignature: Electronic signature record (21 CFR Part 11) | |
| - DocumentEvent: Immutable audit trail | |
| Domains: | |
| - Surgical: Operative reports, surgical checklists, procedure logs | |
| - Clinical: Progress notes, discharge summaries, H&P | |
| - Laboratory: Lab certificates, QC reports, specimen chain-of-custody | |
| - Insurance: Claim documents, policy certificates, authorization letters | |
| - Logistics: Shipping manifests, inspection certificates, customs docs | |
| - Hospitality: Inspection reports, maintenance logs, guest agreements | |
| - Education: Training certificates, competency records, transcripts | |
| Standards: HIPAA, HL7 CDA, FDA 21 CFR Part 11, ISO 15489 (Records Mgmt) | |
| Performance: <100ms p95 for metadata ops, storage-dependent for content | |
| """ | |
| from __future__ import annotations | |
| from datetime import datetime, timezone | |
| from enum import Enum | |
| from typing import Any, Dict, List, Optional | |
| from uuid import UUID, uuid4 | |
| from pydantic import BaseModel, ConfigDict, Field as PydanticField | |
| from sqlmodel import Column, Field, JSON, SQLModel | |
| # ============================================================================ | |
| # ENUMS | |
| # ============================================================================ | |
| class DocumentStatus(str, Enum): | |
| """Document lifecycle states.""" | |
| DRAFT = "draft" | |
| PENDING_REVIEW = "pending_review" | |
| PENDING_SIGNATURE = "pending_signature" | |
| SIGNED = "signed" | |
| FINAL = "final" | |
| AMENDED = "amended" | |
| SUPERSEDED = "superseded" | |
| ARCHIVED = "archived" | |
| DELETED = "deleted" | |
| class DocumentCategory(str, Enum): | |
| """Document categories.""" | |
| CLINICAL_NOTE = "clinical_note" | |
| OPERATIVE_REPORT = "operative_report" | |
| DISCHARGE_SUMMARY = "discharge_summary" | |
| CONSENT_FORM = "consent_form" | |
| LAB_REPORT = "lab_report" | |
| RADIOLOGY_REPORT = "radiology_report" | |
| CERTIFICATE = "certificate" | |
| POLICY = "policy" | |
| MANIFEST = "manifest" | |
| INSPECTION = "inspection" | |
| AGREEMENT = "agreement" | |
| INVOICE = "invoice" | |
| TRAINING_RECORD = "training_record" | |
| FORM = "form" | |
| ATTACHMENT = "attachment" | |
| OTHER = "other" | |
| class ContentFormat(str, Enum): | |
| """Document content formats.""" | |
| MARKDOWN = "markdown" | |
| HTML = "html" | |
| PLAIN_TEXT = "plain_text" | |
| JSON = "json" | |
| PDF = "pdf" | |
| IMAGE = "image" | |
| BINARY = "binary" | |
| class AccessLevel(str, Enum): | |
| """Document access levels.""" | |
| PUBLIC = "public" | |
| INTERNAL = "internal" | |
| RESTRICTED = "restricted" | |
| CONFIDENTIAL = "confidential" | |
| class SignatureType(str, Enum): | |
| """Electronic signature types (FDA 21 CFR Part 11).""" | |
| AUTHOR = "author" | |
| REVIEWER = "reviewer" | |
| APPROVER = "approver" | |
| WITNESS = "witness" | |
| CO_SIGNER = "co_signer" | |
| AUTHENTICATOR = "authenticator" | |
| class DocumentEventType(str, Enum): | |
| """Audit event types.""" | |
| DOCUMENT_CREATED = "document.created" | |
| DOCUMENT_UPDATED = "document.updated" | |
| VERSION_CREATED = "document.version_created" | |
| SUBMITTED_FOR_REVIEW = "document.submitted_for_review" | |
| SIGNATURE_ADDED = "document.signature_added" | |
| DOCUMENT_FINALIZED = "document.finalized" | |
| DOCUMENT_AMENDED = "document.amended" | |
| DOCUMENT_ARCHIVED = "document.archived" | |
| DOCUMENT_ACCESSED = "document.accessed" | |
| DOCUMENT_EXPORTED = "document.exported" | |
| DOCUMENT_DELETED = "document.deleted" | |
| # ============================================================================ | |
| # CORE ENTITIES | |
| # ============================================================================ | |
| class DocumentDefinition(SQLModel, table=False): | |
| """ | |
| Reusable document template. | |
| Examples: | |
| - "Operative Report" (Surgio) | |
| - "Discharge Summary" (Clinica) | |
| - "Lab Certificate of Analysis" (Labo) | |
| - "Insurance Claim Document" (Insura) | |
| """ | |
| id: UUID = Field(default_factory=uuid4, primary_key=True) | |
| tenant_id: UUID = Field(index=True) | |
| key: str = Field(max_length=120, index=True) | |
| title: str = Field(max_length=250) | |
| description: Optional[str] = Field(default=None, max_length=1000) | |
| version: str = Field(default="1.0") | |
| domain: str = Field(default="general", max_length=60) | |
| category: DocumentCategory = Field(default=DocumentCategory.OTHER) | |
| content_format: ContentFormat = Field(default=ContentFormat.MARKDOWN) | |
| access_level: AccessLevel = Field(default=AccessLevel.INTERNAL) | |
| # Template content (body template with {{placeholders}}) | |
| template_body: Optional[str] = Field(default=None) | |
| # Sections schema (JSON list of section defs) | |
| sections: Optional[List[Dict[str, Any]]] = Field( | |
| default=None, sa_column=Column(JSON), | |
| ) | |
| # Signature requirements | |
| required_signatures: Optional[List[Dict[str, Any]]] = Field( | |
| default=None, sa_column=Column(JSON), | |
| ) | |
| # Retention policy | |
| retention_days: Optional[int] = Field(default=None, description="Days to retain (HIPAA: 2190=6yr)") | |
| meta_data: Optional[Dict[str, Any]] = Field(default=None, sa_column=Column(JSON)) | |
| is_active: bool = Field(default=True) | |
| is_system: bool = Field(default=False) | |
| created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc)) | |
| class Document(SQLModel, table=False): | |
| """ | |
| A concrete document instance with content and metadata. | |
| """ | |
| id: UUID = Field(default_factory=uuid4, primary_key=True) | |
| tenant_id: UUID = Field(index=True) | |
| definition_id: Optional[UUID] = Field(default=None, index=True) | |
| case_id: Optional[UUID] = Field(default=None, index=True) | |
| subject_id: Optional[UUID] = Field(default=None, index=True) | |
| title: str = Field(max_length=500) | |
| category: DocumentCategory = Field(default=DocumentCategory.OTHER) | |
| content_format: ContentFormat = Field(default=ContentFormat.MARKDOWN) | |
| access_level: AccessLevel = Field(default=AccessLevel.INTERNAL) | |
| status: DocumentStatus = Field(default=DocumentStatus.DRAFT) | |
| # Content | |
| content: Optional[str] = Field(default=None) | |
| content_hash: Optional[str] = Field(default=None) # SHA-256 of content | |
| file_size_bytes: Optional[int] = None | |
| mime_type: Optional[str] = Field(default=None, max_length=100) | |
| # Versioning | |
| current_version: int = Field(default=1) | |
| # Author | |
| author_id: Optional[UUID] = None | |
| author_name: Optional[str] = Field(default=None, max_length=200) | |
| # Tags / classification | |
| tags: Optional[List[str]] = Field(default=None, sa_column=Column(JSON)) | |
| meta_data: Optional[Dict[str, Any]] = Field(default=None, sa_column=Column(JSON)) | |
| created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc)) | |
| updated_at: Optional[datetime] = None | |
| finalized_at: Optional[datetime] = None | |
| archived_at: Optional[datetime] = None | |
| class DocumentVersion(SQLModel, table=False): | |
| """Immutable version record (content-addressed).""" | |
| id: UUID = Field(default_factory=uuid4, primary_key=True) | |
| document_id: UUID = Field(index=True) | |
| version_number: int = Field(ge=1) | |
| content: Optional[str] = None | |
| content_hash: str = Field(default="") | |
| change_summary: Optional[str] = Field(default=None, max_length=500) | |
| created_by: Optional[UUID] = None | |
| created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc)) | |
| class DocumentSignature(SQLModel, table=False): | |
| """Electronic signature record (FDA 21 CFR Part 11 compliant).""" | |
| id: UUID = Field(default_factory=uuid4, primary_key=True) | |
| document_id: UUID = Field(index=True) | |
| signer_id: UUID | |
| signer_name: str = Field(max_length=200) | |
| signer_role: Optional[str] = Field(default=None, max_length=100) | |
| signature_type: SignatureType = Field(default=SignatureType.AUTHOR) | |
| reason: Optional[str] = Field(default=None, max_length=500) | |
| signature_hash: str = Field(default="") | |
| signed_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc)) | |
| class DocumentEvent(SQLModel, table=False): | |
| """Immutable audit event.""" | |
| id: UUID = Field(default_factory=uuid4, primary_key=True) | |
| tenant_id: UUID = Field(index=True) | |
| document_id: Optional[UUID] = None | |
| event_type: DocumentEventType = Field(index=True) | |
| actor_id: Optional[UUID] = None | |
| data: Optional[Dict[str, Any]] = Field(default=None, sa_column=Column(JSON)) | |
| prev_hash: str = Field(default="genesis") | |
| event_hash: str = Field(default="") | |
| occurred_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc)) | |
| # ============================================================================ | |
| # API SCHEMAS | |
| # ============================================================================ | |
| class DocumentDefinitionResponse(BaseModel): | |
| id: UUID | |
| key: str | |
| title: str | |
| description: Optional[str] = None | |
| version: str | |
| domain: str | |
| category: str | |
| content_format: str | |
| access_level: str | |
| required_signatures: Optional[List[Dict[str, Any]]] = None | |
| retention_days: Optional[int] = None | |
| is_active: bool | |
| is_system: bool | |
| model_config = ConfigDict(from_attributes=True) | |
| class DocumentCreateRequest(BaseModel): | |
| definition_id: Optional[UUID] = None | |
| case_id: Optional[UUID] = None | |
| subject_id: Optional[UUID] = None | |
| title: str | |
| category: DocumentCategory = DocumentCategory.OTHER | |
| content_format: ContentFormat = ContentFormat.MARKDOWN | |
| access_level: AccessLevel = AccessLevel.INTERNAL | |
| content: Optional[str] = None | |
| tags: Optional[List[str]] = None | |
| meta_data: Optional[Dict[str, Any]] = None | |
| class DocumentUpdateRequest(BaseModel): | |
| content: str | |
| change_summary: Optional[str] = None | |
| class DocumentSignRequest(BaseModel): | |
| signer_name: str | |
| signer_role: Optional[str] = None | |
| signature_type: SignatureType = SignatureType.AUTHOR | |
| reason: Optional[str] = None | |
| class DocumentResponse(BaseModel): | |
| id: UUID | |
| definition_id: Optional[UUID] = None | |
| case_id: Optional[UUID] = None | |
| subject_id: Optional[UUID] = None | |
| title: str | |
| category: str | |
| content_format: str | |
| access_level: str | |
| status: str | |
| content_hash: Optional[str] = None | |
| current_version: int | |
| author_id: Optional[UUID] = None | |
| author_name: Optional[str] = None | |
| tags: Optional[List[str]] = None | |
| signature_count: int = 0 | |
| created_at: datetime | |
| updated_at: Optional[datetime] = None | |
| finalized_at: Optional[datetime] = None | |
| model_config = ConfigDict(from_attributes=True) | |
| class DocumentDetailResponse(DocumentResponse): | |
| content: Optional[str] = None | |
| versions: List[Dict[str, Any]] = [] | |
| signatures: List[Dict[str, Any]] = [] | |