Download src/core/artifacts/models.py from anaselamr/yaz: direct link, hf CLI and curl.
- Browser
- Download file 4.65 kB
-
https://huggingface.co/spaces/anaselamr/yaz/resolve/main/src/core/artifacts/models.py
- Command line
-
hf download hf://spaces/anaselamr/yaz/src/core/artifacts/models.py
-
curl -L -o models.py https://huggingface.co/spaces/anaselamr/yaz/resolve/main/src/core/artifacts/models.py
4.65 kB
| """ | |
| Core Artifacts — SQLModel Tables | |
| ================================= | |
| ArtifactMetadata: immutable metadata row for every stored artifact. | |
| Actual bytes live on disk at /data/artifacts/{prefix}/{content_id}. | |
| Multi-tenancy: tenant_id on every row (Commandment #3) | |
| Security: PHI level depends on artifact_type — encrypt before store | |
| Performance: indexed on tenant_id, artifact_type, case_id | |
| """ | |
| from datetime import datetime, timezone | |
| from enum import Enum | |
| from typing import Any, Dict, List, Optional | |
| from uuid import UUID, uuid4 | |
| from sqlalchemy import Column, JSON, Text | |
| from sqlmodel import Field, SQLModel | |
| class ArtifactType(str, Enum): | |
| """Types of immutable artifacts.""" | |
| REPORT = "report" | |
| MEDIA = "media" | |
| PROTOCOL = "protocol" | |
| ATTACHMENT = "attachment" | |
| SIGNED_OUTPUT = "signed_output" | |
| TEMPLATE = "template" | |
| EXPORT = "export" | |
| class ReplicationTarget(str, Enum): | |
| """Where an artifact has been replicated.""" | |
| LOCAL = "local" | |
| MINIO = "minio" | |
| PEER = "peer" | |
| IPFS = "ipfs" | |
| class ArtifactMetadata(SQLModel, table=True): | |
| """ | |
| Immutable metadata for a content-addressed artifact. | |
| content_id is the SHA-256 hex digest of the raw bytes, prefixed with 'sha256:'. | |
| The actual file is at /data/artifacts/{content_id[:8]}/{content_id}. | |
| Standards: HIPAA (encrypted PHI), FDA 21 CFR Part 11 (signed outputs) | |
| Performance: <200ms for metadata lookup via indexed queries | |
| """ | |
| __tablename__ = "artifact_metadata" | |
| # PK is the content-address itself | |
| content_id: str = Field( | |
| primary_key=True, | |
| max_length=71, # 'sha256:' + 64 hex chars | |
| description="SHA-256 content address (sha256:<hex>)", | |
| ) | |
| # Multi-tenancy (COMMANDMENT #3) | |
| tenant_id: UUID = Field(nullable=False, index=True) | |
| # Artifact classification | |
| artifact_type: ArtifactType = Field( | |
| nullable=False, index=True, | |
| description="Logical type of this artifact", | |
| ) | |
| # File metadata | |
| original_filename: Optional[str] = Field( | |
| default=None, max_length=500, | |
| description="Original upload filename", | |
| ) | |
| mime_type: Optional[str] = Field( | |
| default=None, max_length=100, | |
| description="MIME type (e.g. application/pdf, image/png)", | |
| ) | |
| size_bytes: int = Field( | |
| nullable=False, | |
| description="Size in bytes of the raw artifact", | |
| ) | |
| checksum_sha256: str = Field( | |
| max_length=64, nullable=False, | |
| description="Raw SHA-256 hex (without prefix)", | |
| ) | |
| # Encryption (optional — required for PHI) | |
| encryption_key_id: Optional[str] = Field( | |
| default=None, max_length=100, | |
| description="Encryption key identifier (NULL = not encrypted)", | |
| ) | |
| encryption_algo: Optional[str] = Field( | |
| default=None, max_length=20, | |
| description="Encryption algorithm (e.g. aes-256-gcm)", | |
| ) | |
| # Ownership | |
| owner_id: Optional[UUID] = Field( | |
| default=None, | |
| description="User who uploaded this artifact", | |
| ) | |
| workspace_id: Optional[UUID] = Field( | |
| default=None, index=True, | |
| description="Workspace scope (if applicable)", | |
| ) | |
| case_id: Optional[UUID] = Field( | |
| default=None, index=True, | |
| description="Case this artifact belongs to (if applicable)", | |
| ) | |
| # Classification | |
| tags: List[str] = Field( | |
| default_factory=list, sa_column=Column(JSON), | |
| description="Free-form tags", | |
| ) | |
| # Retention & replication | |
| retention_policy: str = Field( | |
| default="standard", max_length=50, | |
| description="Retention policy (standard, long_term, ephemeral)", | |
| ) | |
| replication_status: Dict[str, Any] = Field( | |
| default_factory=lambda: {"local": True}, | |
| sa_column=Column(JSON), | |
| description="Where artifact is replicated: {local: true, minio: false, peer: false}", | |
| ) | |
| # Signing (for signed outputs) | |
| signed_by: Optional[str] = Field( | |
| default=None, max_length=100, | |
| description="Actor who signed this artifact", | |
| ) | |
| signed_at: Optional[datetime] = Field( | |
| default=None, | |
| description="Signature timestamp", | |
| ) | |
| # Audit | |
| created_at: datetime = Field( | |
| default_factory=lambda: datetime.now(timezone.utc), nullable=False, | |
| description="Upload timestamp", | |
| ) | |
| is_deleted: bool = Field( | |
| default=False, | |
| description="Soft delete — bytes retained per policy", | |
| ) | |
| # Extra metadata | |
| metadata_extra: Optional[Dict[str, Any]] = Field( | |
| default=None, sa_column=Column(JSON), | |
| description="Arbitrary extra metadata (domain-specific)", | |
| ) | |