File size: 3,563 Bytes
609d292 b7f068c 609d292 b7f068c 609d292 b7f068c 6a8aa67 609d292 579285c 609d292 b7f068c 609d292 b7f068c 609d292 b7f068c 609d292 b7f068c 609d292 b7f068c 609d292 579285c 609d292 579285c 609d292 b7f068c 609d292 b7f068c 609d292 b7f068c 609d292 b7f068c 579285c 609d292 579285c 609d292 b7f068c 609d292 b7f068c 609d292 b7f068c 609d292 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | """
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
|