Download src/shared/models/base.py from anaselamr/yaz: direct link, hf CLI and curl.
- Browser
- Download file 3.56 kB
-
https://huggingface.co/spaces/anaselamr/yaz/resolve/main/src/shared/models/base.py
- Command line
-
hf download hf://spaces/anaselamr/yaz/src/shared/models/base.py
-
curl -L -o base.py https://huggingface.co/spaces/anaselamr/yaz/resolve/main/src/shared/models/base.py
3.56 kB
| """ | |
| YAZ Care Platform - Base Models | |
| Core model classes and mixins for the entire platform | |
| """ | |
| from datetime import UTC, datetime, timezone | |
| from typing import Optional | |
| from uuid import uuid4 | |
| from sqlalchemy import UUID, Boolean, Column, DateTime, String | |
| from sqlalchemy.dialects.postgresql import UUID as POSTGRES_UUID | |
| from sqlalchemy.orm import declarative_base | |
| from sqlalchemy.sql import func | |
| # Create the declarative base for all models | |
| Base = declarative_base() | |
| class BaseModel(Base): # type: ignore[misc, operator, valid-type] | |
| """ | |
| Abstract base model with common fields for all entities | |
| """ | |
| __abstract__ = True | |
| # Primary key - use UUID for better security and distribution | |
| id = Column(POSTGRES_UUID(as_uuid=True), primary_key=True, default=uuid4, nullable=False) | |
| def __repr__(self): | |
| return f"<{self.__class__.__name__}(id={self.id})>" | |
| class TimestampMixin: | |
| """ | |
| Mixin to add created_at and updated_at timestamps | |
| """ | |
| created_at = Column(DateTime(timezone=True), default=func.now(), nullable=False) | |
| updated_at = Column( | |
| DateTime(timezone=True), default=func.now(), onupdate=func.now(), nullable=False | |
| ) | |
| class SoftDeleteMixin: | |
| """ | |
| Mixin to add soft delete functionality | |
| """ | |
| deleted_at = Column(DateTime(timezone=True), nullable=True) | |
| is_deleted = Column(Boolean, default=False, nullable=False) | |
| def soft_delete(self): | |
| """Mark record as deleted""" | |
| self.is_deleted = True # type: ignore[arg-type, assignment] | |
| self.deleted_at = datetime.now(UTC) # type: ignore[arg-type, assignment] | |
| def restore(self): | |
| """Restore soft-deleted record""" | |
| self.is_deleted = False # type: ignore[arg-type, assignment] | |
| self.deleted_at = None # type: ignore[arg-type, assignment] | |
| class AuditMixin: | |
| """ | |
| Mixin to add audit trail fields | |
| """ | |
| created_by = Column(String(200), nullable=True) | |
| updated_by = Column(String(200), nullable=True) | |
| # IP address for audit purposes | |
| created_from_ip = Column(String(45), nullable=True) # IPv6 support | |
| updated_from_ip = Column(String(45), nullable=True) | |
| class TenantMixin: | |
| """ | |
| Mixin to add multi-tenancy support | |
| """ | |
| tenant_id = Column(POSTGRES_UUID(as_uuid=True), nullable=False, index=True) | |
| class VersionMixin: | |
| """ | |
| Mixin to add optimistic locking with version control | |
| """ | |
| version = Column(String(20), default="1.0", nullable=False) | |
| def increment_version(self): | |
| """Increment version for optimistic locking""" | |
| try: | |
| major, minor = self.version.split(".") | |
| self.version = f"{major}.{int(minor) + 1}" # type: ignore[arg-type, assignment] | |
| except (ValueError, AttributeError): | |
| self.version = "1.1" # type: ignore[arg-type, assignment] | |
| # Composite base classes for common combinations | |
| class StandardModel(BaseModel, TimestampMixin, TenantMixin): | |
| """ | |
| Standard model with ID, timestamps, and tenant support | |
| Most common base for business entities | |
| """ | |
| __abstract__ = True | |
| class AuditableModel(BaseModel, TimestampMixin, SoftDeleteMixin, AuditMixin, TenantMixin): | |
| """ | |
| Full-featured model with all mixins | |
| Used for critical business entities requiring full audit trail | |
| """ | |
| __abstract__ = True | |
| class VersionedModel(BaseModel, TimestampMixin, VersionMixin, TenantMixin): | |
| """ | |
| Model with version control for optimistic locking | |
| Used for entities that require concurrency control | |
| """ | |
| __abstract__ = True | |