Adjusted stepper for shipment tracking

This commit is contained in:
GotthardG
2024-10-31 10:25:46 +01:00
parent 930d551464
commit d6d7e7c919
12 changed files with 959 additions and 239 deletions

View File

@ -1,12 +1,10 @@
from fastapi import FastAPI
from fastapi import FastAPI, HTTPException, status
from fastapi.middleware.cors import CORSMiddleware
from fastapi import HTTPException, status
from pydantic import BaseModel
from typing import List, Optional
import logging
import uuid
logging.basicConfig(level=logging.INFO)
app = FastAPI()
@ -21,7 +19,7 @@ app.add_middleware(
class ContactPerson(BaseModel):
id: int
id: Optional[int] = None # Make id optional
firstname: str
lastname: str
phone_number: str
@ -29,16 +27,18 @@ class ContactPerson(BaseModel):
class Address(BaseModel):
id: int
id: Optional[int] = None # Make id optional
street: str
city: str
zipcode: str
country: str
class Proposal(BaseModel):
id: int
id: Optional[int]
number: str
class Dewar(BaseModel):
id: Optional[str] = None
dewar_name: str
@ -51,8 +51,7 @@ class Dewar(BaseModel):
ready_date: Optional[str] = None
shipping_date: Optional[str] = None
arrival_date: Optional[str] = None
shippingStatus: str
arrivalStatus: str
returning_date: Optional[str] = None
qrcode: str
@ -85,8 +84,10 @@ class Shipment(BaseModel):
# Example data for contacts
contacts = [
ContactPerson(id=1, firstname="Frodo", lastname="Baggins", phone_number="123-456-7890", email="frodo.baggins@lotr.com"),
ContactPerson(id=2, firstname="Samwise", lastname="Gamgee", phone_number="987-654-3210", email="samwise.gamgee@lotr.com"),
ContactPerson(id=1, firstname="Frodo", lastname="Baggins", phone_number="123-456-7890",
email="frodo.baggins@lotr.com"),
ContactPerson(id=2, firstname="Samwise", lastname="Gamgee", phone_number="987-654-3210",
email="samwise.gamgee@lotr.com"),
ContactPerson(id=3, firstname="Aragorn", lastname="Elessar", phone_number="123-333-4444",
email="aragorn.elessar@lotr.com"),
ContactPerson(id=4, firstname="Legolas", lastname="Greenleaf", phone_number="555-666-7777",
@ -102,7 +103,7 @@ contacts = [
ContactPerson(id=9, firstname="Elrond", lastname="Half-elven", phone_number="777-888-9999",
email="elrond.halfelven@lotr.com"),
ContactPerson(id=10, firstname="Eowyn", lastname="Shieldmaiden of Rohan", phone_number="000-111-2222",
email="eowyn.rohan@lotr.com")
email="eowyn.rohan@lotr.com"),
]
# Example data for return addresses
@ -121,12 +122,11 @@ dewars = [
number_of_samples=70,
return_address=[return_addresses[0]],
contact_person=[contacts[0]],
status='Ready',
status='Ready for Shipping',
ready_date='2023-09-30',
shipping_date='2023-10-01',
arrival_date='2023-10-02',
shippingStatus='Shipped',
arrivalStatus='Arrived',
shipping_date='',
arrival_date='',
returning_date='',
qrcode='QR123DEWAR001'
),
Dewar(
@ -138,11 +138,10 @@ dewars = [
return_address=[return_addresses[1]],
contact_person=[contacts[1]],
status='In Preparation',
ready_date='2023-10-01',
shipping_date='2023-10-02',
arrival_date='2023-10-04',
shippingStatus='not shipped',
arrivalStatus='not arrived',
ready_date='',
shipping_date='',
arrival_date='',
returning_date='',
qrcode='QR123DEWAR002'
),
Dewar(
@ -153,9 +152,11 @@ dewars = [
number_of_samples=47,
return_address=[return_addresses[0]],
contact_person=[contacts[2]],
status='Pending',
shippingStatus='Ready for Shipping',
arrivalStatus='Pending',
status='Not Shipped',
ready_date='2024.01.01',
shipping_date='',
arrival_date='',
returning_date='',
qrcode='QR123DEWAR003'
),
Dewar(
@ -166,9 +167,11 @@ dewars = [
number_of_samples=47,
return_address=[return_addresses[0]],
contact_person=[contacts[2]],
status='In Preparation',
shippingStatus='not shipped',
arrivalStatus='not arrived',
status='Delayed',
ready_date='2024.01.01',
shipping_date='2024.01.02',
arrival_date='',
returning_date='',
qrcode='QR123DEWAR003'
),
Dewar(
@ -179,9 +182,11 @@ dewars = [
number_of_samples=47,
return_address=[return_addresses[0]],
contact_person=[contacts[2]],
status='Ready for Shipping',
shippingStatus='shipped',
arrivalStatus='not arrived',
status='Returned',
ready_date='2024.01.01',
shipping_date='2024.01.02',
arrival_date='2024.01.03',
returning_date='2024.01.07',
qrcode='QR123DEWAR003'
),
]
@ -206,7 +211,6 @@ specific_dewars1 = [dewar for dewar in dewars if dewar.id in specific_dewar_ids1
specific_dewars2 = [dewar for dewar in dewars if dewar.id in specific_dewar_ids2]
specific_dewars3 = [dewar for dewar in dewars if dewar.id in specific_dewar_ids3]
# Define shipments with the selected Dewars
shipments = [
Shipment(
@ -242,17 +246,19 @@ shipments = [
comments='Contains the one ring',
dewars=specific_dewars3
)
]
@app.get("/contacts", response_model=List[ContactPerson])
async def get_contacts():
return contacts
@app.get("/return_addresses", response_model=List[Address])
async def get_return_addresses():
return return_addresses
@app.get("/proposals", response_model=List[Proposal])
async def get_proposals():
return proposals
@ -262,29 +268,55 @@ async def get_proposals():
async def get_shipments():
return shipments
@app.delete("/shipments/{shipment_id}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_shipment(shipment_id: str):
global shipments # Use global variable to access the shipments list
shipments = [shipment for shipment in shipments if shipment.shipment_id != shipment_id]
@app.post("/shipments/{shipment_id}/add_dewar", response_model=Shipment)
async def add_dewar_to_shipment(shipment_id: str, dewar_id: str):
# Log received parameters for debugging
logging.info(f"Received request to add dewar {dewar_id} to shipment {shipment_id}")
# Find the shipment by id
shipment = next((sh for sh in shipments if sh.shipment_id == shipment_id), None)
if not shipment:
logging.error("Shipment not found")
raise HTTPException(status_code=404, detail="Shipment not found")
# Find the dewar by id
dewar = next((dw for dw in dewars if dw.id == dewar_id), None)
if not dewar:
logging.error("Dewar not found")
raise HTTPException(status_code=404, detail="Dewar not found")
# Add the dewar to the shipment
if dewar not in shipment.dewars:
shipment.dewars.append(dewar)
return shipment
@app.get("/dewars", response_model=List[Dewar])
async def get_dewars():
return dewars
@app.post("/dewars", response_model=List[Dewar], status_code=status.HTTP_201_CREATED)
async def create_dewar(shipment: Dewar):
dewar_id = f'SHIP-{uuid.uuid4().hex[:8].upper()}' # Generates a unique shipment ID
shipment.id = dewar_id # Set the generated ID on the shipment object
dewars.append(shipment) # Add the modified shipment object to the list
@app.post("/dewars", response_model=Dewar, status_code=status.HTTP_201_CREATED)
async def create_dewar(dewar: Dewar) -> Dewar:
dewar_id = f'DEWAR-{uuid.uuid4().hex[:8].upper()}' # Generates a unique dewar ID
dewar.id = dewar_id # Set the generated ID on the dewar object
dewars.append(dewar) # Add the modified dewar object to the list
return dewar # Return the newly created dewar
return dewars # Return the list of all dewars
@app.get("/shipment_contact_persons")
async def get_shipment_contact_persons():
return [{"shipment_id": shipment.shipment_id, "contact_person": shipment.get_shipment_contact_persons()} for shipment in
shipments]
return [{"shipment_id": shipment.shipment_id, "contact_person": shipment.get_shipment_contact_persons()} for
shipment in shipments]
@app.post("/shipments", response_model=Shipment, status_code=status.HTTP_201_CREATED)
@ -297,6 +329,7 @@ async def create_shipment(shipment: Shipment):
shipments.append(shipment)
return shipment
# Creation of a new contact
@app.post("/contacts", response_model=ContactPerson, status_code=status.HTTP_201_CREATED)
async def create_contact(contact: ContactPerson):
@ -305,16 +338,24 @@ async def create_contact(contact: ContactPerson):
if any(c.email == contact.email for c in contacts):
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Contact with this email already exists."
detail="This contact already exists."
)
# Find the next available id
if contacts:
max_id = max(c.id for c in contacts)
contact.id = max_id + 1 if contact.id is None else contact.id
else:
contact.id = 1 if contact.id is None else contact.id
contacts.append(contact)
return contact
# Creation of a return address
@app.post("/return_addresses", response_model=Address, status_code=status.HTTP_201_CREATED)
async def create_return_address(address: Address):
logging.info(f"Received contact creation request: {address}")
logging.info(f"Received address creation request: {address}")
# Check for duplicate address by city
if any(a.city == address.city for a in return_addresses):
raise HTTPException(
@ -322,5 +363,12 @@ async def create_return_address(address: Address):
detail="Address in this city already exists."
)
# Find the next available id
if return_addresses:
max_id = max(a.id for a in return_addresses)
address.id = max_id + 1 if address.id is None else address.id
else:
address.id = 1 if address.id is None else address.id
return_addresses.append(address)
return address