19 lines
567 B
Python
19 lines
567 B
Python
"""Activity logging helper — auto-record CRUD operations."""
|
|
import json
|
|
from sqlalchemy.orm import Session
|
|
from app.models.activity import ActivityLog
|
|
|
|
|
|
def log_activity(db: Session, action: str, entity_type: str, entity_id: int, user_id: int = None, details: dict = None):
|
|
"""Record an activity log entry."""
|
|
entry = ActivityLog(
|
|
action=action,
|
|
entity_type=entity_type,
|
|
entity_id=entity_id,
|
|
user_id=user_id,
|
|
details=json.dumps(details) if details else None,
|
|
)
|
|
db.add(entry)
|
|
db.commit()
|
|
return entry
|