Download src/apps/documents/models.py from anaselamr/yaz: direct link, hf CLI and curl.
- Browser
- Download file 3.75 kB
-
https://huggingface.co/spaces/anaselamr/yaz/resolve/main/src/apps/documents/models.py
- Command line
-
hf download hf://spaces/anaselamr/yaz/src/apps/documents/models.py
-
curl -L -o models.py https://huggingface.co/spaces/anaselamr/yaz/resolve/main/src/apps/documents/models.py
3.75 kB
| """ | |
| YAZ Care Platform — Document Models | |
| ===================================== | |
| SQLModel entities for clinical document management. | |
| Standards: HIPAA § 164.312(c)(1) — integrity controls | |
| ISO 13606 — EHR document exchange | |
| HL7 FHIR DocumentReference | |
| """ | |
| from __future__ import annotations | |
| from datetime import datetime, timezone | |
| from enum import Enum | |
| from pathlib import Path | |
| from typing import Optional | |
| from uuid import UUID, uuid4 | |
| from sqlmodel import Field, SQLModel | |
| # --------------------------------------------------------------------------- | |
| # Enumerations | |
| # --------------------------------------------------------------------------- | |
| class DocumentType(str, Enum): | |
| CONSENT = "consent" | |
| PREOP_ASSESSMENT = "preop_assessment" | |
| IMAGING = "imaging" | |
| LAB_RESULT = "lab_result" | |
| OPERATIVE_NOTE = "operative_note" | |
| DISCHARGE_LETTER = "discharge_letter" | |
| FOLLOW_UP_REPORT = "follow_up_report" | |
| BILLING_ATTACHMENT = "billing_attachment" | |
| OTHER = "other" | |
| class DocumentStatus(str, Enum): | |
| PENDING = "pending" # uploaded, awaiting review | |
| ACTIVE = "active" # validated and available | |
| SUPERSEDED = "superseded" # replaced by a newer version | |
| ARCHIVED = "archived" # retained but no longer primary | |
| DELETED = "deleted" # soft-deleted (NEVER purged) | |
| class StorageBackend(str, Enum): | |
| LOCAL = "local" | |
| S3 = "s3" | |
| OVH_OBJECT_STORAGE = "ovh" | |
| # --------------------------------------------------------------------------- | |
| # Database model | |
| # --------------------------------------------------------------------------- | |
| class ClinicalDocument(SQLModel, table=True): | |
| """ | |
| Metadata record for a clinical document attached to a surgical case. | |
| The actual file bytes are stored in the :attr:`storage_path` location. | |
| The database record stores immutable metadata and provenance. | |
| Multi-tenancy: every query MUST filter by ``tenant_id``. | |
| Audit: ``created_by`` / ``updated_by`` are always set. | |
| Soft-delete: ``deleted_at`` is set instead of row removal. | |
| """ | |
| __tablename__ = "clinical_documents" | |
| __table_args__ = {"schema": "surgio"} | |
| id: UUID = Field(default_factory=uuid4, primary_key=True) | |
| tenant_id: UUID = Field(index=True) | |
| case_id: Optional[UUID] = Field(default=None, index=True, foreign_key="surgio.cases.id") | |
| # Document identity | |
| document_type: DocumentType = Field(default=DocumentType.OTHER, index=True) | |
| status: DocumentStatus = Field(default=DocumentStatus.PENDING, index=True) | |
| # File metadata (filled at upload time) | |
| original_filename: str = Field(max_length=255) | |
| mime_type: str = Field(max_length=100) | |
| file_size_bytes: int = Field(ge=0) | |
| sha256_hash: str = Field(max_length=64) # integrity check | |
| # Storage | |
| storage_backend: StorageBackend = Field(default=StorageBackend.LOCAL) | |
| storage_path: str = Field(max_length=1024) # relative path or S3 key | |
| # Signed URL placeholder (populated on demand, never persisted) | |
| # Optional description | |
| title: Optional[str] = Field(default=None, max_length=255) | |
| description: Optional[str] = Field(default=None, max_length=1000) | |
| # Version tracking (for superseded documents) | |
| version: int = Field(default=1, ge=1) | |
| supersedes_id: Optional[UUID] = Field(default=None) | |
| # Provenance | |
| created_by: UUID | |
| updated_by: Optional[UUID] = Field(default=None) | |
| created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc)) | |
| updated_at: Optional[datetime] = Field(default=None) | |
| # Soft-delete | |
| deleted_at: Optional[datetime] = Field(default=None) | |
| deleted_by: Optional[UUID] = Field(default=None) | |
| deletion_reason: Optional[str] = Field(default=None, max_length=500) | |