05/06 Phase 2: fixed issues 2nd

This commit is contained in:
2026-05-07 11:22:13 -04:00
parent 9f7d656d5e
commit 50347e95c6
+18 -2
View File
@@ -51,5 +51,21 @@ def log_admin_action(action, target_type=None, target_id=None, before=None, afte
def model_to_dict(obj, fields): def model_to_dict(obj, fields):
"""Return a plain dict of the named fields from an ORM object (for audit before/after).""" """
return {f: getattr(obj, f, None) for f in fields} Return a plain JSON-serialisable dict of the named fields from an ORM object.
Converts datetime -> ISO-8601 string, Decimal -> float, all others pass through.
Safe to use directly as AuditLog before_json / after_json values.
"""
import datetime
from decimal import Decimal
result = {}
for f in fields:
val = getattr(obj, f, None)
if isinstance(val, (datetime.datetime, datetime.date)):
result[f] = val.isoformat()
elif isinstance(val, Decimal):
result[f] = float(val)
else:
result[f] = val
return result