Journaling improvements
This commit is contained in:
@@ -4,6 +4,8 @@ from sqlalchemy import desc
|
||||
from datetime import datetime
|
||||
from app.models.circuit import Circuit
|
||||
from app.schemas.circuit import CircuitCreate, CircuitUpdate
|
||||
from app.models.journal import EntityType
|
||||
from app.crud.crud_journal import journal
|
||||
|
||||
|
||||
class CRUDCircuit:
|
||||
@@ -30,7 +32,7 @@ class CRUDCircuit:
|
||||
) -> List[Circuit]:
|
||||
return db.query(Circuit).order_by(desc(Circuit.created_at)).offset(skip).limit(limit).all()
|
||||
|
||||
def create(self, db: Session, obj_in: CircuitCreate) -> Circuit:
|
||||
def create(self, db: Session, obj_in: CircuitCreate, user: str = "system", user_ip: Optional[str] = None) -> Circuit:
|
||||
db_obj = Circuit(
|
||||
local_flight_id=obj_in.local_flight_id,
|
||||
arrival_id=obj_in.arrival_id,
|
||||
@@ -39,22 +41,74 @@ class CRUDCircuit:
|
||||
db.add(db_obj)
|
||||
db.commit()
|
||||
db.refresh(db_obj)
|
||||
|
||||
# Log circuit creation in journal
|
||||
# Use LOCAL_FLIGHT entity type if local_flight_id exists, otherwise ARRIVAL
|
||||
entity_type = EntityType.LOCAL_FLIGHT if obj_in.local_flight_id else EntityType.ARRIVAL
|
||||
entity_id = obj_in.local_flight_id if obj_in.local_flight_id else obj_in.arrival_id
|
||||
|
||||
journal.log_change(
|
||||
db,
|
||||
entity_type,
|
||||
entity_id,
|
||||
f"Circuit recorded at {obj_in.circuit_timestamp.isoformat()}",
|
||||
user,
|
||||
user_ip
|
||||
)
|
||||
|
||||
return db_obj
|
||||
|
||||
def update(self, db: Session, db_obj: Circuit, obj_in: CircuitUpdate) -> Circuit:
|
||||
def update(self, db: Session, db_obj: Circuit, obj_in: CircuitUpdate, user: str = "system", user_ip: Optional[str] = None) -> Circuit:
|
||||
obj_data = obj_in.dict(exclude_unset=True)
|
||||
changes = []
|
||||
|
||||
for field, value in obj_data.items():
|
||||
setattr(db_obj, field, value)
|
||||
old_value = getattr(db_obj, field)
|
||||
if old_value != value:
|
||||
changes.append(f"{field} changed from '{old_value}' to '{value}'")
|
||||
setattr(db_obj, field, value)
|
||||
|
||||
db.add(db_obj)
|
||||
db.commit()
|
||||
db.refresh(db_obj)
|
||||
|
||||
# Log changes in journal if any were made
|
||||
if changes:
|
||||
entity_type = EntityType.LOCAL_FLIGHT if db_obj.local_flight_id else EntityType.ARRIVAL
|
||||
entity_id = db_obj.local_flight_id if db_obj.local_flight_id else db_obj.arrival_id
|
||||
|
||||
for change in changes:
|
||||
journal.log_change(
|
||||
db,
|
||||
entity_type,
|
||||
entity_id,
|
||||
f"Circuit: {change}",
|
||||
user,
|
||||
user_ip
|
||||
)
|
||||
|
||||
return db_obj
|
||||
|
||||
def delete(self, db: Session, circuit_id: int) -> bool:
|
||||
def delete(self, db: Session, circuit_id: int, user: str = "system", user_ip: Optional[str] = None) -> bool:
|
||||
circuit = self.get(db, circuit_id)
|
||||
if circuit:
|
||||
# Determine which entity this circuit belongs to
|
||||
entity_type = EntityType.LOCAL_FLIGHT if circuit.local_flight_id else EntityType.ARRIVAL
|
||||
entity_id = circuit.local_flight_id if circuit.local_flight_id else circuit.arrival_id
|
||||
|
||||
db.delete(circuit)
|
||||
db.commit()
|
||||
|
||||
# Log deletion in journal
|
||||
journal.log_change(
|
||||
db,
|
||||
entity_type,
|
||||
entity_id,
|
||||
f"Circuit deleted (recorded at {circuit.circuit_timestamp.isoformat()})",
|
||||
user,
|
||||
user_ip
|
||||
)
|
||||
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
Reference in New Issue
Block a user