Fix some issues
This commit is contained in:
@@ -46,6 +46,14 @@ def log_action(user_id, action, entity_type=None, entity_id=None, details=None):
|
||||
entity_type : str | None – 'ticket', 'comment', 'user', etc.
|
||||
entity_id : int | None – primary key of the affected entity
|
||||
details : str | None – free-form JSON or human-readable details
|
||||
|
||||
Transaction note
|
||||
----------------
|
||||
This function deliberately does NOT call db.session.commit(). The entry
|
||||
is added to the current session and committed by the caller alongside its
|
||||
own business objects. This ensures the log entry is only persisted when
|
||||
the parent operation succeeds — an independent commit here would leave
|
||||
orphaned log entries for operations that were subsequently rolled back.
|
||||
"""
|
||||
ip = _get_real_ip()
|
||||
|
||||
@@ -59,7 +67,8 @@ def log_action(user_id, action, entity_type=None, entity_id=None, details=None):
|
||||
ip_address = ip,
|
||||
)
|
||||
db.session.add(entry)
|
||||
db.session.commit()
|
||||
# flush to surface constraint violations early without committing
|
||||
db.session.flush()
|
||||
logger.info(
|
||||
f'[ACTIVITY] action={action} entity={entity_type}:{entity_id} '
|
||||
f'user_id={user_id} ip={ip}'
|
||||
@@ -70,7 +79,13 @@ def log_action(user_id, action, entity_type=None, entity_id=None, details=None):
|
||||
|
||||
|
||||
def log_ticket_history(ticket, field_name, old_value, new_value, changed_by_id):
|
||||
"""Record a granular field-level change on a ticket."""
|
||||
"""Record a granular field-level change on a ticket.
|
||||
|
||||
Transaction note
|
||||
----------------
|
||||
Like log_action, this function does NOT commit — the caller is responsible
|
||||
for committing the session after all field changes have been recorded.
|
||||
"""
|
||||
from app.models import TicketHistory
|
||||
try:
|
||||
entry = TicketHistory(
|
||||
@@ -81,7 +96,7 @@ def log_ticket_history(ticket, field_name, old_value, new_value, changed_by_id):
|
||||
new_value = str(new_value) if new_value is not None else None,
|
||||
)
|
||||
db.session.add(entry)
|
||||
db.session.commit()
|
||||
db.session.flush()
|
||||
logger.info(
|
||||
f'[TICKET HISTORY] ticket_id={ticket.id} field={field_name} '
|
||||
f'"{old_value}" -> "{new_value}" by user_id={changed_by_id}'
|
||||
|
||||
Reference in New Issue
Block a user