Fix formatting with black
This commit is contained in:
@ -6,10 +6,27 @@ from pydantic import BaseModel, ValidationError
|
||||
from datetime import date
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
|
||||
from app.models import Shipment as ShipmentModel, ContactPerson as ContactPersonModel, Address as AddressModel, \
|
||||
Proposal as ProposalModel, Dewar as DewarModel, Puck as PuckModel, Sample as SampleModel
|
||||
from app.schemas import ShipmentCreate, UpdateShipmentComments, Shipment as ShipmentSchema, DewarUpdate, \
|
||||
ContactPerson as ContactPersonSchema, Sample as SampleSchema, DewarCreate, PuckCreate, SampleCreate, DewarSchema
|
||||
from app.models import (
|
||||
Shipment as ShipmentModel,
|
||||
ContactPerson as ContactPersonModel,
|
||||
Address as AddressModel,
|
||||
Proposal as ProposalModel,
|
||||
Dewar as DewarModel,
|
||||
Puck as PuckModel,
|
||||
Sample as SampleModel,
|
||||
)
|
||||
from app.schemas import (
|
||||
ShipmentCreate,
|
||||
UpdateShipmentComments,
|
||||
Shipment as ShipmentSchema,
|
||||
DewarUpdate,
|
||||
ContactPerson as ContactPersonSchema,
|
||||
Sample as SampleSchema,
|
||||
DewarCreate,
|
||||
PuckCreate,
|
||||
SampleCreate,
|
||||
DewarSchema,
|
||||
)
|
||||
from app.database import get_db
|
||||
from app.crud import get_shipments, get_shipment_by_id
|
||||
|
||||
@ -23,7 +40,9 @@ def default_serializer(obj):
|
||||
|
||||
|
||||
@router.get("", response_model=List[ShipmentSchema])
|
||||
async def fetch_shipments(id: Optional[int] = Query(None), db: Session = Depends(get_db)):
|
||||
async def fetch_shipments(
|
||||
id: Optional[int] = Query(None), db: Session = Depends(get_db)
|
||||
):
|
||||
if id:
|
||||
shipment = get_shipment_by_id(db, id)
|
||||
if not shipment:
|
||||
@ -35,9 +54,12 @@ async def fetch_shipments(id: Optional[int] = Query(None), db: Session = Depends
|
||||
shipments = get_shipments(db)
|
||||
logging.info(f"Total shipments fetched: {len(shipments)}")
|
||||
for shipment in shipments:
|
||||
logging.info(f"Shipment ID: {shipment.id}, Shipment Name: {shipment.shipment_name}")
|
||||
logging.info(
|
||||
f"Shipment ID: {shipment.id}, Shipment Name: {shipment.shipment_name}"
|
||||
)
|
||||
return shipments
|
||||
|
||||
|
||||
@router.get("/{shipment_id}/dewars", response_model=List[DewarSchema])
|
||||
async def get_dewars_by_shipment_id(shipment_id: int, db: Session = Depends(get_db)):
|
||||
shipment = db.query(ShipmentModel).filter(ShipmentModel.id == shipment_id).first()
|
||||
@ -51,12 +73,21 @@ async def get_dewars_by_shipment_id(shipment_id: int, db: Session = Depends(get_
|
||||
return dewars
|
||||
|
||||
|
||||
|
||||
@router.post("", response_model=ShipmentSchema, status_code=status.HTTP_201_CREATED)
|
||||
async def create_shipment(shipment: ShipmentCreate, db: Session = Depends(get_db)):
|
||||
contact_person = db.query(ContactPersonModel).filter(ContactPersonModel.id == shipment.contact_person_id).first()
|
||||
return_address = db.query(AddressModel).filter(AddressModel.id == shipment.return_address_id).first()
|
||||
proposal = db.query(ProposalModel).filter(ProposalModel.id == shipment.proposal_id).first()
|
||||
contact_person = (
|
||||
db.query(ContactPersonModel)
|
||||
.filter(ContactPersonModel.id == shipment.contact_person_id)
|
||||
.first()
|
||||
)
|
||||
return_address = (
|
||||
db.query(AddressModel)
|
||||
.filter(AddressModel.id == shipment.return_address_id)
|
||||
.first()
|
||||
)
|
||||
proposal = (
|
||||
db.query(ProposalModel).filter(ProposalModel.id == shipment.proposal_id).first()
|
||||
)
|
||||
|
||||
if not (contact_person or return_address or proposal):
|
||||
raise HTTPException(status_code=404, detail="Associated entity not found")
|
||||
@ -97,17 +128,29 @@ async def delete_shipment(shipment_id: int, db: Session = Depends(get_db)):
|
||||
|
||||
|
||||
@router.put("/{shipment_id}", response_model=ShipmentSchema)
|
||||
async def update_shipment(shipment_id: int, updated_shipment: ShipmentCreate, db: Session = Depends(get_db)):
|
||||
print("Received payload:", json.dumps(updated_shipment.dict(), indent=2, default=default_serializer))
|
||||
async def update_shipment(
|
||||
shipment_id: int, updated_shipment: ShipmentCreate, db: Session = Depends(get_db)
|
||||
):
|
||||
print(
|
||||
"Received payload:",
|
||||
json.dumps(updated_shipment.dict(), indent=2, default=default_serializer),
|
||||
)
|
||||
|
||||
shipment = db.query(ShipmentModel).filter(ShipmentModel.id == shipment_id).first()
|
||||
if not shipment:
|
||||
raise HTTPException(status_code=404, detail="Shipment not found")
|
||||
|
||||
# Validate relationships by IDs
|
||||
contact_person = db.query(ContactPersonModel).filter(
|
||||
ContactPersonModel.id == updated_shipment.contact_person_id).first()
|
||||
return_address = db.query(AddressModel).filter(AddressModel.id == updated_shipment.return_address_id).first()
|
||||
contact_person = (
|
||||
db.query(ContactPersonModel)
|
||||
.filter(ContactPersonModel.id == updated_shipment.contact_person_id)
|
||||
.first()
|
||||
)
|
||||
return_address = (
|
||||
db.query(AddressModel)
|
||||
.filter(AddressModel.id == updated_shipment.return_address_id)
|
||||
.first()
|
||||
)
|
||||
if not contact_person:
|
||||
raise HTTPException(status_code=404, detail="Contact person not found")
|
||||
if not return_address:
|
||||
@ -123,25 +166,39 @@ async def update_shipment(shipment_id: int, updated_shipment: ShipmentCreate, db
|
||||
|
||||
# Process and update dewars' details
|
||||
for dewar_data in updated_shipment.dewars:
|
||||
dewar = db.query(DewarModel).filter(DewarModel.id == dewar_data.dewar_id).first()
|
||||
dewar = (
|
||||
db.query(DewarModel).filter(DewarModel.id == dewar_data.dewar_id).first()
|
||||
)
|
||||
if not dewar:
|
||||
raise HTTPException(status_code=404, detail=f"Dewar with ID {dewar_data.dewar_id} not found")
|
||||
raise HTTPException(
|
||||
status_code=404, detail=f"Dewar with ID {dewar_data.dewar_id} not found"
|
||||
)
|
||||
|
||||
update_fields = dewar_data.dict(exclude_unset=True)
|
||||
for key, value in update_fields.items():
|
||||
if key == 'contact_person_id':
|
||||
contact_person = db.query(ContactPersonModel).filter(ContactPersonModel.id == value).first()
|
||||
if key == "contact_person_id":
|
||||
contact_person = (
|
||||
db.query(ContactPersonModel)
|
||||
.filter(ContactPersonModel.id == value)
|
||||
.first()
|
||||
)
|
||||
if not contact_person:
|
||||
raise HTTPException(status_code=404,
|
||||
detail=f"Contact person with ID {value} for Dewar {dewar_data.dewar_id} not found")
|
||||
if key == 'return_address_id':
|
||||
address = db.query(AddressModel).filter(AddressModel.id == value).first()
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail=f"Contact person with ID {value} for Dewar {dewar_data.dewar_id} not found",
|
||||
)
|
||||
if key == "return_address_id":
|
||||
address = (
|
||||
db.query(AddressModel).filter(AddressModel.id == value).first()
|
||||
)
|
||||
if not address:
|
||||
raise HTTPException(status_code=404,
|
||||
detail=f"Address with ID {value} for Dewar {dewar_data.dewar_id} not found")
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail=f"Address with ID {value} for Dewar {dewar_data.dewar_id} not found",
|
||||
)
|
||||
|
||||
for key, value in update_fields.items():
|
||||
if key != 'dewar_id':
|
||||
if key != "dewar_id":
|
||||
setattr(dewar, key, value)
|
||||
|
||||
db.commit()
|
||||
@ -150,7 +207,9 @@ async def update_shipment(shipment_id: int, updated_shipment: ShipmentCreate, db
|
||||
|
||||
|
||||
@router.post("/{shipment_id}/add_dewar", response_model=ShipmentSchema)
|
||||
async def add_dewar_to_shipment(shipment_id: int, dewar_id: int, db: Session = Depends(get_db)):
|
||||
async def add_dewar_to_shipment(
|
||||
shipment_id: int, dewar_id: int, 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")
|
||||
@ -166,14 +225,18 @@ async def add_dewar_to_shipment(shipment_id: int, dewar_id: int, db: Session = D
|
||||
|
||||
|
||||
@router.delete("/{shipment_id}/remove_dewar/{dewar_id}", response_model=ShipmentSchema)
|
||||
async def remove_dewar_from_shipment(shipment_id: int, dewar_id: int, db: Session = Depends(get_db)):
|
||||
async def remove_dewar_from_shipment(
|
||||
shipment_id: int, dewar_id: int, 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")
|
||||
|
||||
dewar_exists = any(dw.id == dewar_id for dw in shipment.dewars)
|
||||
if not dewar_exists:
|
||||
raise HTTPException(status_code=404, detail=f"Dewar with ID {dewar_id} not found in shipment")
|
||||
raise HTTPException(
|
||||
status_code=404, detail=f"Dewar with ID {dewar_id} not found in shipment"
|
||||
)
|
||||
|
||||
shipment.dewars = [dw for dw in shipment.dewars if dw.id != dewar_id]
|
||||
db.commit()
|
||||
@ -201,8 +264,13 @@ async def get_samples_in_shipment(shipment_id: int, db: Session = Depends(get_db
|
||||
return samples
|
||||
|
||||
|
||||
@router.get("/shipments/{shipment_id}/dewars/{dewar_id}/samples", response_model=List[SampleSchema])
|
||||
async def get_samples_in_dewar(shipment_id: int, dewar_id: int, db: Session = Depends(get_db)):
|
||||
@router.get(
|
||||
"/shipments/{shipment_id}/dewars/{dewar_id}/samples",
|
||||
response_model=List[SampleSchema],
|
||||
)
|
||||
async def get_samples_in_dewar(
|
||||
shipment_id: int, dewar_id: int, db: Session = Depends(get_db)
|
||||
):
|
||||
shipment = get_shipment_by_id(db, shipment_id)
|
||||
if not shipment:
|
||||
raise HTTPException(status_code=404, detail="Shipment not found")
|
||||
@ -220,8 +288,11 @@ async def get_samples_in_dewar(shipment_id: int, dewar_id: int, db: Session = De
|
||||
|
||||
|
||||
@router.put("/{shipment_id}/comments", response_model=ShipmentSchema)
|
||||
async def update_shipment_comments(shipment_id: int, comments_data: UpdateShipmentComments,
|
||||
db: Session = Depends(get_db)):
|
||||
async def update_shipment_comments(
|
||||
shipment_id: int,
|
||||
comments_data: UpdateShipmentComments,
|
||||
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")
|
||||
@ -232,15 +303,25 @@ async def update_shipment_comments(shipment_id: int, comments_data: UpdateShipme
|
||||
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)):
|
||||
@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()
|
||||
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
|
||||
@ -284,4 +365,4 @@ def add_dewar_puck_sample_to_shipment(shipment_id: int, payload: DewarCreate, db
|
||||
except ValidationError as e:
|
||||
raise HTTPException(status_code=400, detail=f"Validation error: {e}")
|
||||
|
||||
return shipment
|
||||
return shipment
|
||||
|
Reference in New Issue
Block a user