File size: 1,542 Bytes
34e27fb
 
 
 
 
 
 
 
 
 
 
 
 
621a3cb
34e27fb
 
 
 
 
 
 
 
 
 
 
 
 
621a3cb
34e27fb
 
 
 
 
 
 
 
 
 
 
 
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
from sqlmodel import SQLModel, Field
from typing import Optional
from datetime import datetime
from sqlalchemy import Column, DateTime, JSON
import uuid


class AuditLogBase(SQLModel):
    event_id: str = Field(index=True)  # UUID for deduplication
    event_type: str = Field(max_length=50)  # created|updated|completed|deleted
    user_id: str  # String user identifier
    task_id: int  # Reference to the affected task
    event_data: dict = Field(sa_column=Column(JSON))  # JSONB field for event data
    timestamp: datetime = Field(sa_column=Column(DateTime(timezone=True)), default_factory=lambda: datetime.now(datetime.UTC))


class AuditLog(AuditLogBase, table=True):
    """
    Persistent record of all task events for a user.
    Contains id, event_id, event_type, user_id, task_id, event_data (JSONB), and timestamp.
    """
    id: Optional[int] = Field(default=None, primary_key=True)
    event_id: str = Field(index=True, unique=True)  # Unique constraint for deduplication
    event_type: str = Field(max_length=50)  # created|updated|completed|deleted
    user_id: str  # String user identifier
    task_id: int  # Reference to the affected task
    event_data: dict = Field(sa_column=Column(JSON))  # JSONB field for event data
    timestamp: datetime = Field(sa_column=Column(DateTime(timezone=True)), default_factory=lambda: datetime.now(datetime.UTC))


class AuditLogCreate(AuditLogBase):
    pass


class AuditLogRead(AuditLogBase):
    id: int
    timestamp: datetime

    class Config:
        from_attributes = True