26 lines
884 B
Python
26 lines
884 B
Python
from decimal import Decimal
|
|||
|
|
|
||
|
|
from sqlalchemy.orm import Session
|
||
|
|
|
||
|
|
from .models import AuditEntry, User
|
||
|
|
|
||
|
|
|
||
|
|
def _plain(value):
|
||
|
|
if isinstance(value, Decimal):
|
||
|
|
return str(value.normalize()) if value == value.to_integral() else str(value)
|
||
|
|
return value
|
||
|
|
|
||
|
|
|
||
|
|
def snapshot(obj, fields: list[str]) -> dict:
|
||
|
|
return {f: _plain(getattr(obj, f)) for f in fields}
|
||
|
|
|
||
|
|
|
||
|
|
def diff(before: dict, after: dict) -> dict:
|
||
|
|
return {k: [before.get(k), after.get(k)] for k in after if before.get(k) != after.get(k)}
|
||
|
|
|
||
|
|
|
||
|
|
def record(db: Session, user: User | None, entity: str, entity_id: int | None, action: str,
|
||
|
|
summary: str = "", changes: dict | None = None, year_id: int | None = None) -> None:
|
||
|
|
db.add(AuditEntry(user_id=user.id if user else None, entity=entity, entity_id=entity_id,
|
||
|
|
action=action, summary=summary[:500], changes=changes, year_id=year_id))
|