now creating dewars from spreadsheet
This commit is contained in:
@ -1,13 +1,13 @@
|
||||
# app/routers/shipment.py
|
||||
|
||||
from fastapi import APIRouter, HTTPException, status, Query, Depends
|
||||
from sqlalchemy.orm import Session
|
||||
from typing import List, Optional
|
||||
import logging
|
||||
from pydantic import BaseModel
|
||||
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
|
||||
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
|
||||
from app.database import get_db
|
||||
@ -74,8 +74,8 @@ async def create_shipment(shipment: ShipmentCreate, db: Session = Depends(get_db
|
||||
|
||||
|
||||
@router.delete("/{shipment_id}", status_code=status.HTTP_204_NO_CONTENT)
|
||||
async def delete_shipment(id: int, db: Session = Depends(get_db)):
|
||||
shipment = db.query(ShipmentModel).filter(ShipmentModel.id == id).first()
|
||||
async def delete_shipment(shipment_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")
|
||||
db.delete(shipment)
|
||||
@ -84,10 +84,10 @@ async def delete_shipment(id: int, db: Session = Depends(get_db)):
|
||||
|
||||
|
||||
@router.put("/{shipment_id}", response_model=ShipmentSchema)
|
||||
async def update_shipment(id: int, updated_shipment: ShipmentCreate, db: Session = Depends(get_db)):
|
||||
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 == id).first()
|
||||
shipment = db.query(ShipmentModel).filter(ShipmentModel.id == shipment_id).first()
|
||||
if not shipment:
|
||||
raise HTTPException(status_code=404, detail="Shipment not found")
|
||||
|
||||
@ -137,8 +137,8 @@ async def update_shipment(id: int, updated_shipment: ShipmentCreate, db: Session
|
||||
|
||||
|
||||
@router.post("/{shipment_id}/add_dewar", response_model=ShipmentSchema)
|
||||
async def add_dewar_to_shipment(id: int, dewar_id: int, db: Session = Depends(get_db)):
|
||||
shipment = db.query(ShipmentModel).filter(ShipmentModel.id == id).first()
|
||||
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")
|
||||
dewar = db.query(DewarModel).filter(DewarModel.id == dewar_id).first()
|
||||
@ -175,8 +175,8 @@ async def get_shipment_contact_persons(db: Session = Depends(get_db)):
|
||||
|
||||
|
||||
@router.get("/{shipment_id}/samples", response_model=List[SampleSchema])
|
||||
def get_samples_in_shipment(id: int, db: Session = Depends(get_db)):
|
||||
shipment = db.query(ShipmentModel).filter(ShipmentModel.id == id).first()
|
||||
async def get_samples_in_shipment(shipment_id: int, db: Session = Depends(get_db)):
|
||||
shipment = db.query(ShipmentModel).filter(ShipmentModel.id == shipment_id).first()
|
||||
if shipment is None:
|
||||
raise HTTPException(status_code=404, detail="Shipment not found")
|
||||
|
||||
@ -189,7 +189,7 @@ def get_samples_in_shipment(id: int, db: Session = Depends(get_db)):
|
||||
|
||||
|
||||
@router.get("/shipments/{shipment_id}/dewars/{dewar_id}/samples", response_model=List[SampleSchema])
|
||||
def get_samples_in_dewar(shipment_id: int, dewar_id: int, db: Session = Depends(get_db)):
|
||||
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")
|
||||
@ -207,8 +207,9 @@ def get_samples_in_dewar(shipment_id: int, dewar_id: int, db: Session = Depends(
|
||||
|
||||
|
||||
@router.put("/{shipment_id}/comments", response_model=ShipmentSchema)
|
||||
async def update_shipment_comments(id: int, comments_data: UpdateShipmentComments, db: Session = Depends(get_db)):
|
||||
shipment = db.query(ShipmentModel).filter(ShipmentModel.id == id).first()
|
||||
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")
|
||||
|
||||
@ -219,72 +220,30 @@ async def update_shipment_comments(id: int, comments_data: UpdateShipmentComment
|
||||
|
||||
|
||||
@router.post("/{shipment_id}/add_dewar_puck_sample", response_model=ShipmentSchema, status_code=status.HTTP_201_CREATED)
|
||||
async def add_dewar_puck_sample_to_shipment(shipment_id: int, payload: DewarCreate, db: Session = Depends(get_db)):
|
||||
async 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:
|
||||
logging.error(f"Shipment not found with ID: {shipment_id}")
|
||||
raise HTTPException(status_code=404, detail="Shipment not found")
|
||||
|
||||
for dewar_data in payload.dewars:
|
||||
dewar = Dewar(
|
||||
shipment_id=shipment_id,
|
||||
dewar_name=dewar_data.dewar_name,
|
||||
tracking_number=dewar_data.tracking_number,
|
||||
status=dewar_data.status,
|
||||
contact_person_id=dewar_data.contact_person_id,
|
||||
return_address_id=dewar_data.return_address_id,
|
||||
)
|
||||
db.add(dewar)
|
||||
db.commit()
|
||||
db.refresh(dewar)
|
||||
|
||||
for puck_data in dewar_data.pucks:
|
||||
puck = Puck(
|
||||
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 = Sample(
|
||||
puck_id=puck.id,
|
||||
sample_name=sample_data.sample_name,
|
||||
position=sample_data.position,
|
||||
data_collection_parameters=DataCollectionParameters(
|
||||
**sample_data.data_collection_parameters
|
||||
),
|
||||
)
|
||||
db.add(sample)
|
||||
db.commit()
|
||||
db.refresh(sample)
|
||||
|
||||
db.refresh(shipment)
|
||||
return shipment
|
||||
|
||||
|
||||
@router.post("/{shipment_id}/add_dewar_puck_sample", response_model=ShipmentSchema, status_code=status.HTTP_201_CREATED)
|
||||
async def add_dewar_puck_sample_to_shipment(shipment_id: int, payload: ShipmentCreate, 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")
|
||||
|
||||
for dewar_data in payload.dewars:
|
||||
try:
|
||||
dewar = DewarModel(
|
||||
shipment_id=shipment_id,
|
||||
dewar_name=dewar_data.dewar_name, # Ensure this field aligns with the frontend
|
||||
tracking_number=dewar_data.tracking_number,
|
||||
status=dewar_data.status,
|
||||
contact_person_id=dewar_data.contact_person_id,
|
||||
return_address_id=dewar_data.return_address_id,
|
||||
dewar_name=payload.dewar_name,
|
||||
tracking_number=payload.tracking_number,
|
||||
status=payload.status,
|
||||
contact_person_id=payload.contact_person_id,
|
||||
return_address_id=payload.return_address_id,
|
||||
)
|
||||
db.add(dewar)
|
||||
db.commit()
|
||||
db.refresh(dewar)
|
||||
|
||||
for puck_data in dewar_data.pucks:
|
||||
for puck_data in payload.pucks:
|
||||
puck = PuckModel(
|
||||
dewar_id=dewar.id,
|
||||
puck_name=puck_data.puck_name,
|
||||
@ -298,13 +257,21 @@ async def add_dewar_puck_sample_to_shipment(shipment_id: int, payload: ShipmentC
|
||||
for sample_data in puck_data.samples:
|
||||
sample = SampleModel(
|
||||
puck_id=puck.id,
|
||||
sample_name=sample_data.crystalname,
|
||||
position=sample_data.positioninpuck,
|
||||
data_collection_parameters=sample_data.data_collection_parameters
|
||||
sample_name=sample_data.sample_name,
|
||||
position=sample_data.position,
|
||||
data_collection_parameters=sample_data.data_collection_parameters,
|
||||
)
|
||||
db.add(sample)
|
||||
db.commit()
|
||||
db.refresh(sample)
|
||||
|
||||
db.refresh(shipment)
|
||||
return shipment
|
||||
db.refresh(shipment)
|
||||
except SQLAlchemyError as e:
|
||||
logging.error(f"Database error occurred: {e}")
|
||||
raise HTTPException(status_code=500, detail="Internal server error")
|
||||
except ValidationError as e:
|
||||
logging.error(f"Validation error occurred: {e}")
|
||||
raise HTTPException(status_code=400, detail="Validation error")
|
||||
|
||||
logging.info(f"Successfully added dewar, puck, and sample for shipment ID: {shipment_id}")
|
||||
return shipment
|
||||
|
Reference in New Issue
Block a user