Changeset 527c097 for uc_approve.py


Ignore:
Timestamp:
04/05/26 20:02:04 (3 months ago)
Author:
teo <teodorbogoeski@…>
Branches:
main
Parents:
95cf703
Message:

Add connection pooling and transaction management

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uc_approve.py

    r95cf703 r527c097  
    1 from db import get_connection, pick_from_list, print_table
     1from db import get_connection, get_transaction, pick_from_list, print_table
    22
    33
     
    120120
    121121    new_status = "approved" if action == 0 else "rejected"
    122     conn = get_connection()
    123122    try:
    124         with conn.cursor() as cur:
    125             cur.execute(
    126                 """
    127                 UPDATE reservations
    128                 SET status = %s, approved_by = %s
    129                 WHERE reservation_id = %s AND status = 'pending'
    130                 RETURNING reservation_id, status
    131                 """,
    132                 (new_status, admin_user_id, res["reservation_id"]),
    133             )
    134             result = cur.fetchone()
    135         conn.commit()
     123        with get_transaction() as conn:
     124            with conn.cursor() as cur:
     125                # For approvals, re-check conflicts within the transaction
     126                # to prevent approving overlapping reservations
     127                if new_status == "approved":
     128                    cur.execute(
     129                        """
     130                        SELECT COUNT(*) FROM reservations
     131                        WHERE resource_id = %s
     132                          AND reservation_id != %s
     133                          AND status = 'approved'
     134                          AND start_time < %s AND end_time > %s
     135                        """,
     136                        (res["resource_id"], res["reservation_id"],
     137                         res["end_time"], res["start_time"]),
     138                    )
     139                    if cur.fetchone()[0] > 0:
     140                        raise RuntimeError("Conflict: another reservation was approved for this time slot")
     141
     142                cur.execute(
     143                    """
     144                    UPDATE reservations
     145                    SET status = %s, approved_by = %s
     146                    WHERE reservation_id = %s AND status = 'pending'
     147                    RETURNING reservation_id, status
     148                    """,
     149                    (new_status, admin_user_id, res["reservation_id"]),
     150                )
     151                result = cur.fetchone()
     152        # Transaction committed automatically
    136153        if result:
    137154            print(f"\n  Reservation #{result[0]} has been {result[1]}.")
     
    139156            print("\n  Reservation was already processed by another administrator.")
    140157    except Exception as e:
    141         conn.rollback()
     158        # Transaction rolled back automatically
    142159        print(f"\n  Error: {e}")
    143     finally:
    144         conn.close()
Note: See TracChangeset for help on using the changeset viewer.