Make shipment fields optional and refactor test scripts.

Updated the `number_of_pucks` and `number_of_samples` fields in the `schemas.py` to be optional for greater flexibility. Simplified the test Jupyter Notebook by restructuring imports and consolidating function calls for better readability and maintainability.
This commit is contained in:
GotthardG
2025-01-17 09:36:16 +01:00
parent 481068603b
commit 9739b8cfe9
6 changed files with 485 additions and 309 deletions

View File

@ -2,9 +2,7 @@ from fastapi import APIRouter, HTTPException, status, Query, Depends
from sqlalchemy.orm import Session
from typing import List, Optional
import logging
from pydantic import ValidationError
from datetime import date
from sqlalchemy.exc import SQLAlchemyError
import json
from app.models import (
@ -13,8 +11,6 @@ from app.models import (
Address as AddressModel,
Proposal as ProposalModel,
Dewar as DewarModel,
Puck as PuckModel,
Sample as SampleModel,
LogisticsEvent,
SampleEvent,
PuckEvent,
@ -25,7 +21,6 @@ from app.schemas import (
Shipment as ShipmentSchema,
ContactPerson as ContactPersonSchema,
Sample as SampleSchema,
DewarCreate,
DewarSchema,
)
from app.database import get_db
@ -334,8 +329,13 @@ async def remove_dewar_from_shipment(
f"has associated events. Removal not allowed.",
)
# Unlink the dewar from the shipment
shipment.dewars = [dw for dw in shipment.dewars if dw.id != dewar_id]
# Perform cascade deletion: Delete samples, pucks, and the dewar
for puck in dewar.pucks:
for sample in puck.samples:
db.delete(sample) # Delete associated samples
db.delete(puck) # Delete associated puck
db.delete(dewar) # Finally, delete the dewar itself
db.commit()
db.refresh(shipment)
@ -399,68 +399,3 @@ async def update_shipment_comments(
db.commit()
db.refresh(shipment)
return shipment
@router.post(
"/{shipment_id}/add_dewar_puck_sample",
response_model=ShipmentSchema,
status_code=status.HTTP_201_CREATED,
)
def add_dewar_puck_sample_to_shipment(
shipment_id: int, payload: DewarCreate, db: Session = Depends(get_db)
):
shipment = db.query(ShipmentModel).filter(ShipmentModel.id == shipment_id).first()
if not shipment:
raise HTTPException(status_code=404, detail="Shipment not found")
try:
for dewar_data in payload.dewars:
dewar = (
db.query(DewarModel)
.filter(DewarModel.dewar_name == dewar_data.dewar_name)
.first()
)
if dewar:
# Update existing dewar
dewar.tracking_number = dewar_data.tracking_number
dewar.status = dewar_data.status
db.commit()
else:
dewar = DewarModel(
shipment_id=shipment_id,
dewar_name=dewar_data.dewar_name,
tracking_number=dewar_data.tracking_number,
status=dewar_data.status,
)
db.add(dewar)
db.commit()
db.refresh(dewar)
for puck_data in dewar_data.pucks:
puck = PuckModel(
dewar_id=dewar.id,
puck_name=puck_data.puck_name,
puck_type=puck_data.puck_type,
puck_location_in_dewar=puck_data.puck_location_in_dewar,
)
db.add(puck)
db.commit()
db.refresh(puck)
for sample_data in puck_data.samples:
sample = SampleModel(
puck_id=puck.id,
sample_name=sample_data.sample_name,
position=sample_data.position,
)
db.add(sample)
db.commit()
db.refresh(sample)
db.refresh(shipment)
except SQLAlchemyError as e:
raise HTTPException(status_code=500, detail=f"Database error: {e}")
except ValidationError as e:
raise HTTPException(status_code=400, detail=f"Validation error: {e}")
return shipment