Spaces:
Sleeping
Sleeping
File size: 2,292 Bytes
74de430 d0a01ab 74de430 d0a01ab 74de430 d0a01ab |
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 |
"""
Document Model - Universal file storage with polymorphic ownership
"""
from sqlalchemy import Column, String, Boolean, Integer, Text, BigInteger
from sqlalchemy.dialects.postgresql import UUID, JSONB
from datetime import datetime
from app.core.database import Base
import uuid
class Document(Base):
"""
Document model - Universal file storage for all entities
Supports versioning and multiple storage providers (Cloudinary, Supabase)
"""
__tablename__ = "documents"
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
# Ownership (polymorphic)
entity_type = Column(Text, nullable=False) # 'user', 'project', 'ticket', 'client', 'contractor'
entity_id = Column(UUID(as_uuid=True), nullable=False)
# File Details
file_name = Column(Text, nullable=False)
file_type = Column(Text) # MIME type (e.g., 'image/jpeg', 'application/pdf')
file_size = Column(BigInteger) # File size in bytes
file_url = Column(Text, nullable=False) # Storage URL
storage_provider = Column(Text, default='supabase') # 'cloudinary', 'supabase', 'local'
# Document Classification
document_type = Column(Text) # 'profile_photo', 'identity_card', 'contract', 'invoice', 'ticket_image'
document_category = Column(Text) # 'legal', 'financial', 'operational', 'evidence'
# Version Control
version = Column(Integer, default=1)
is_latest_version = Column(Boolean, default=True)
previous_version_id = Column(UUID(as_uuid=True), nullable=True)
# Metadata
description = Column(Text)
tags = Column(JSONB, default=list) # Array of tags for search
additional_metadata = Column(JSONB, default=dict) # GPS, OCR text, Cloudinary response, etc.
# Access Control
uploaded_by_user_id = Column(UUID(as_uuid=True), nullable=True)
is_public = Column(Boolean, default=False)
# Timestamps
created_at = Column(String(50), default=lambda: datetime.utcnow().isoformat())
updated_at = Column(String(50), default=lambda: datetime.utcnow().isoformat())
deleted_at = Column(String(50), nullable=True)
def __repr__(self):
return f"<Document(id='{self.id}', entity='{self.entity_type}:{self.entity_id}', file='{self.file_name}')>"
|