Skip to content
Snippets Groups Projects
Verified Commit 067295ce authored by hanfi's avatar hanfi
Browse files

fix add by image

parent 2df2ed31
Branches
No related tags found
No related merge requests found
......@@ -116,7 +116,7 @@ def get_delivery_by_uuid(
check_token(token, delivery_uuid)
delivery = utils.get_delivery_by_uuid(db, UUID(delivery_uuid))
if not delivery:
raise HTTPException(status_code=404, detail="Item not found")
raise HTTPException(status_code=404, detail="Delivery not found")
return delivery
......@@ -127,6 +127,8 @@ def get_delivery_by_tag(
db: Session = Depends(get_db),
):
delivery = utils.get_delivery_by_tag(db, delivery_tag)
if not delivery:
raise HTTPException(status_code=404, detail="Delivery not found")
check_token(token, delivery.uuid)
return delivery
......@@ -139,7 +141,10 @@ def update_delivery(
db: Session = Depends(get_db),
):
check_token(token, delivery_uuid)
delivery = utils.update_delivery_data(db, UUID(delivery_uuid), data)
delivery = utils.get_delivery_by_uuid(db, UUID(delivery_uuid))
if not delivery:
raise HTTPException(status_code=404, detail="Delivery not found")
delivery = utils.update_delivery_data(db, delivery, data)
return delivery
......@@ -151,21 +156,23 @@ def add_item(
):
check_token(token, None)
delivery = utils.get_delivery_by_uuid(db, item.delivery_uuid)
storage = utils.get_storage_by_name(db, item.storage_name)
if not delivery:
raise HTTPException(status_code=404, detail="Delivery not found")
storage = utils.get_storage_by_name(db, item.storage_name)
if not storage:
raise HTTPException(status_code=404, detail="Storage not found")
return utils.add_item_for_delivery_at_storage(
db, item.delivery_uuid, item.storage_name
)
return utils.add_item_for_delivery_at_storage(db, delivery, storage)
@app.get("/item/{item_uuid}", response_model=schemas.Item)
def get_item(
item_uuid: str, token: str = Depends(oauth2_scheme), db: Session = Depends(get_db)
):
return utils.get_item_by_uuid(db, UUID(item_uuid))
check_token(token, item_uuid)
item = utils.get_item_by_uuid(db, UUID(item_uuid))
if not item:
raise HTTPException(status_code=404, detail="Item not found")
return item
@app.delete("/item/{item_uuid}", response_model=schemas.Item)
......@@ -173,7 +180,10 @@ def deploy_item(
item_uuid: str, token: str = Depends(oauth2_scheme), db: Session = Depends(get_db)
):
check_token(token, None)
return utils.deploy_item(db, UUID(item_uuid))
item = utils.get_item_by_uuid(db, UUID(item_uuid))
if not item:
raise HTTPException(status_code=404, detail="Item not found")
return utils.deploy_item(db, item)
@app.get("/storage/{storage_name}", response_model=schemas.Storage)
......@@ -183,6 +193,9 @@ def get_storage(
db: Session = Depends(get_db),
):
check_token(token, None)
storage = utils.get_storage_by_name(db, storage_name)
if not storage:
raise HTTPException(status_code=404, detail="Storage not found")
return utils.get_storage_by_name(db, storage_name)
......@@ -195,12 +208,16 @@ def get_storages(
return utils.get_storages(db)
@app.post("/item/register", response_model=schemas.Delivery)
@app.post("/item/register", response_model=schemas.Item)
def add_item_with_image(
image: UploadFile,
storage_name: str,
token: str = Depends(oauth2_scheme),
db: Session = Depends(get_db),
):
check_token(token, None)
print(image.file)
return False # utils.add_item_with_image(db, image)
storage = utils.get_storage_by_name(db, storage_name)
if not storage:
raise HTTPException(status_code=404, detail="Storage not found")
return utils.add_item_with_image_at_storage(db, image, storage)
from datetime import datetime
from html import escape
from random import choices
# from shutil import copyfileobj
from shutil import copyfileobj
from string import digits
from tempfile import SpooledTemporaryFile
from cryptography.exceptions import InvalidSignature
from cryptography.hazmat.primitives.asymmetric.ed448 import Ed448PublicKey
......@@ -13,8 +13,6 @@ from sqlalchemy.orm import Session
from . import models, schemas
from .config import settings
# from tempfile import SpooledTemporaryFile
# helpers
def generate_tag():
......@@ -91,10 +89,8 @@ def prepare_delivery(db: Session, verification: str):
def update_delivery_data(
db: Session, delivery_uuid: UUID4, update_data: schemas.DeliveryUpdate
db: Session, delivery: models.Delivery, update_data: schemas.DeliveryUpdate
):
delivery = db.get(models.Delivery, delivery_uuid)
if delivery:
if update_data.addressee:
delivery.addressee = escape(update_data.addressee)
if update_data.team:
......@@ -109,61 +105,38 @@ def update_delivery_data(
def add_item_for_delivery_at_storage(
db: Session, delivery_uuid: UUID4, storage_name: str
db: Session, delivery: models.Delivery, storage: models.Storage
):
delivery = db.get(models.Delivery, delivery_uuid)
storage = db.get(models.Storage, storage_name)
item = models.Item(storage_name=storage_name, stored_at=storage, part_of=delivery)
item = models.Item(storage_name=storage.name, stored_at=storage, part_of=delivery)
db.add(item)
db.commit()
db.refresh(item)
return item
def deploy_item(db: Session, item_uuid: UUID4):
item = db.get(models.Item, item_uuid)
def deploy_item(db: Session, item: models.Item):
item.deployed_at = datetime.now()
db.commit()
db.refresh(item)
return item
# def add_item_with_image(db: Session, image: SpooledTemporaryFile):
# db_item = models.Delivery()
# db.add(db_item)
# db.commit()
# db_image = models.Image(item_uuid=db_item.uuid)
# db.add(db_image)
# db.commit()
# db.refresh(db_image)
# print(db_item.uuid)
# print(db_image.uuid)
# with open(f"./images/{ db_image.uuid }", "wb") as destination:
# try:
# copyfileobj(image.file, destination)
# finally:
# image.file.close
# return db_item
#
#
#
# def update_item(db: Session, item: schemas.Delivery, data: schemas.ItemUpdate):
# if data.addressee:
# item.addressee = escape(data.addressee)
# if data.team:
# item.team = escape(data.team)
# if data.amount:
# item.amount = data.amount
# db.commit()
# db.refresh(item)
# return item
#
#
# def receive_item_with_image(db: Session, item: schemas.ItemCreateByImageAtStorage):
# new_item = models.Delivery(storage_uuid=item.storage_uuid)
# db.add(new_item)
# db.commit()
# db.refresh(new_item)
# new_image = models.Image(item_uuid=new_item.uuid)
# db.add(new_image)
# db.commit()
def add_item_with_image_at_storage(
db: Session, upload_image: SpooledTemporaryFile, storage: str
):
delivery = prepare_delivery(db, None)
print(delivery.tag)
item = add_item_for_delivery_at_storage(db, delivery, storage)
print(item)
print(item.uuid)
image = models.Image(item_uuid=item.uuid)
print(image)
db.add(image)
db.commit()
db.refresh(image)
with open(f"./images/{ image.uuid }", "wb") as destination:
try:
copyfileobj(upload_image.file, destination)
except Exception:
pass
return item
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment