|
@@ -1,7 +1,7 @@
|
|
|
-from typing import Any, List, Optional
|
|
|
+from typing import Any, Optional
|
|
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, File, UploadFile, Form
|
|
|
-from pydantic.errors import NotNoneError
|
|
|
+from fastapi.encoders import jsonable_encoder
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
|
|
from app import crud, models, schemas
|
|
@@ -12,7 +12,7 @@ from uuid import uuid4
|
|
|
router = APIRouter()
|
|
|
|
|
|
|
|
|
-@router.get("/", response_model=List[schemas.NftPrint])
|
|
|
+@router.get("/", response_model=list[schemas.NftPrint])
|
|
|
# @router.get("/")
|
|
|
def read_items(
|
|
|
db: Session = Depends(deps.get_db),
|
|
@@ -24,10 +24,10 @@ def read_items(
|
|
|
Retrieve items.
|
|
|
"""
|
|
|
if crud.user.is_superuser(current_user):
|
|
|
- nfts = crud.nft.get_group_by_title(db, skip=skip, limit=limit)
|
|
|
+ nfts = crud.nft.get_group_by_title(db)
|
|
|
else:
|
|
|
nfts = crud.nft.get_multi_by_owner(
|
|
|
- db=db, owner_id=current_user.userid, skip=skip, limit=limit
|
|
|
+ db=db, owner_id=current_user.userid
|
|
|
)
|
|
|
return nfts
|
|
|
|
|
@@ -50,7 +50,8 @@ async def create_item(
|
|
|
Create new item.
|
|
|
"""
|
|
|
if image.content_type.split('/')[0] != 'image':
|
|
|
- raise HTTPException(status_code=415, detail='content type error! Please upload valid image type')
|
|
|
+ raise HTTPException(status_code=415,
|
|
|
+ detail='content type error! Please upload valid image type')
|
|
|
filename = str(uuid4()) + '.' + image.content_type.split('/')[1]
|
|
|
with open(settings.IMG_PATH + filename, 'wb+') as f:
|
|
|
f.write(image.file.read())
|
|
@@ -68,6 +69,50 @@ async def create_item(
|
|
|
return nft
|
|
|
|
|
|
|
|
|
+@router.put("/", response_model=schemas.NftCreate)
|
|
|
+def update_item(
|
|
|
+ *,
|
|
|
+ db: Session = Depends(deps.get_db),
|
|
|
+ title: str,
|
|
|
+ title_new: Optional[str] = Form(None),
|
|
|
+ context: Optional[str] = Form(None),
|
|
|
+ is_active: Optional[bool] = Form(True),
|
|
|
+ catagory: Optional[str] = Form(None),
|
|
|
+ image: Optional[UploadFile] = File(None),
|
|
|
+ current_user: models.users = Depends(deps.get_current_active_user),
|
|
|
+) -> Any:
|
|
|
+ """
|
|
|
+ Update bulk items.
|
|
|
+ """
|
|
|
+ nft = crud.nft.get_by_title(db=db, title=title)
|
|
|
+ if not nft:
|
|
|
+ raise HTTPException(status_code=404, detail="Item not found")
|
|
|
+ # if not crud.user.is_superuser(current_user) and (nft.userid != current_user.userid):
|
|
|
+ if not crud.user.is_superuser(current_user):
|
|
|
+ raise HTTPException(status_code=400, detail="Not enough permissions")
|
|
|
+ item_in = schemas.NftBulkUpdate(**jsonable_encoder(nft[0]))
|
|
|
+ if image:
|
|
|
+ if image.content_type.split('/')[0] != 'image':
|
|
|
+ raise HTTPException(status_code=415,
|
|
|
+ detail='content type error! Please upload valid image type')
|
|
|
+ filename = str(uuid4()) + '.' + image.content_type.split('/')[1]
|
|
|
+ with open(settings.IMG_PATH + filename, 'wb+') as f:
|
|
|
+ f.write(image.file.read())
|
|
|
+ f.close()
|
|
|
+ item_in.imgurl = settings.IMG_HOST + filename
|
|
|
+ if title_new:
|
|
|
+ item_in.title = title_new
|
|
|
+ if context:
|
|
|
+ item_in.context = context
|
|
|
+ if is_active:
|
|
|
+ item_in.is_active = is_active
|
|
|
+ if catagory:
|
|
|
+ item_in.category = catagory
|
|
|
+
|
|
|
+ nft = crud.nft.update_bulk_title(db=db, db_obj_list=nft, obj_in=item_in)
|
|
|
+ return nft
|
|
|
+
|
|
|
+
|
|
|
@router.put("/{id}", response_model=schemas.NftCreate)
|
|
|
def update_item(
|
|
|
*,
|