nft.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. from typing import Any, List, Optional
  2. from fastapi import APIRouter, Depends, HTTPException, File, UploadFile, Form
  3. from pydantic.errors import NotNoneError
  4. from sqlalchemy.orm import Session
  5. from app import crud, models, schemas
  6. from app.core.config import settings
  7. from app.api import deps
  8. from uuid import uuid4
  9. router = APIRouter()
  10. @router.get("/", response_model=List[schemas.NftPrint])
  11. # @router.get("/")
  12. def read_items(
  13. db: Session = Depends(deps.get_db),
  14. skip: int = 0,
  15. limit: int = 100,
  16. current_user: models.users = Depends(deps.get_current_active_superuser),
  17. ) -> Any:
  18. """
  19. Retrieve items.
  20. """
  21. if crud.user.is_superuser(current_user):
  22. nfts = crud.nft.get_group_by_title(db, skip=skip, limit=limit)
  23. else:
  24. nfts = crud.nft.get_multi_by_owner(
  25. db=db, owner_id=current_user.userid, skip=skip, limit=limit
  26. )
  27. return nfts
  28. @router.post("/", response_model=schemas.NftBase)
  29. async def create_item(
  30. *,
  31. db: Session = Depends(deps.get_db),
  32. # item_in: schemas.NftCreate,
  33. hash: Optional[str] = Form(None),
  34. userid: Optional[str] = Form(None),
  35. title: Optional[str] = Form(None),
  36. context: Optional[str] = Form(None),
  37. is_active: Optional[bool] = Form(True),
  38. catagory: Optional[str] = Form(None),
  39. image: UploadFile = File(...),
  40. current_user: models.users = Depends(deps.get_current_active_user),
  41. ) -> Any:
  42. """
  43. Create new item.
  44. """
  45. if image.content_type.split('/')[0] != 'image':
  46. raise HTTPException(status_code=415, detail='content type error! Please upload valid image type')
  47. filename = str(uuid4()) + '.' + image.content_type.split('/')[1]
  48. with open(settings.IMG_PATH + filename, 'wb+') as f:
  49. f.write(image.file.read())
  50. f.close()
  51. item_in = schemas.NftCreate
  52. item_in.hash = hash
  53. item_in.userid = userid
  54. item_in.title = title
  55. item_in.context = context
  56. item_in.is_active = is_active
  57. item_in.category = catagory
  58. item_in.imgurl = settings.IMG_HOST + filename
  59. nft = crud.nft.create_with_owner(
  60. db=db, obj_in=item_in, owner_id=item_in.userid)
  61. return nft
  62. @router.put("/{id}", response_model=schemas.NftCreate)
  63. def update_item(
  64. *,
  65. db: Session = Depends(deps.get_db),
  66. id: int,
  67. item_in: schemas.NftUpdate,
  68. current_user: models.users = Depends(deps.get_current_active_user),
  69. ) -> Any:
  70. """
  71. Update an item.
  72. """
  73. nft = crud.nft.get(db=db, id=id)
  74. if not nft:
  75. raise HTTPException(status_code=404, detail="Item not found")
  76. if not crud.user.is_superuser(current_user) and (nft.userid != current_user.userid):
  77. raise HTTPException(status_code=400, detail="Not enough permissions")
  78. nft = crud.nft.update(db=db, db_obj=nft, obj_in=item_in)
  79. return nft
  80. @router.get("/{id}", response_model=schemas.NftCreate)
  81. def read_item(
  82. *,
  83. db: Session = Depends(deps.get_db),
  84. id: int,
  85. current_user: models.users = Depends(deps.get_current_active_user),
  86. ) -> Any:
  87. """
  88. Get item by ID.
  89. """
  90. nft = crud.nft.get(db=db, id=id)
  91. if not nft:
  92. raise HTTPException(status_code=404, detail="Item not found")
  93. if not crud.user.is_superuser(current_user) and (nft.userid != current_user.userid):
  94. raise HTTPException(status_code=400, detail="Not enough permissions")
  95. return nft
  96. # @router.post("/title", response_model=schemas.NftCreate)
  97. @router.post("/title")
  98. def read_user_by_title(
  99. *,
  100. db: Session = Depends(deps.get_db),
  101. title: str,
  102. skip: int = 0,
  103. limit: int = 100,
  104. current_user: models.users = Depends(deps.get_current_active_user),
  105. ) -> Any:
  106. """
  107. Get item by title.
  108. """
  109. nft = crud.nft.get_user_by_title(db=db, title=title, skip=skip, limit=limit)
  110. if not nft:
  111. raise HTTPException(status_code=404, detail="Item not found")
  112. if not crud.user.is_superuser(current_user) and (nft.userid != current_user.userid):
  113. raise HTTPException(status_code=400, detail="Not enough permissions")
  114. return nft
  115. @router.delete("/{id}", response_model=schemas.NftCreate)
  116. def delete_item(
  117. *,
  118. db: Session = Depends(deps.get_db),
  119. id: int,
  120. current_user: models.users = Depends(deps.get_current_active_user),
  121. ) -> Any:
  122. """
  123. Delete an item.
  124. """
  125. nft = crud.nft.get(db=db, id=id)
  126. if not nft:
  127. raise HTTPException(status_code=404, detail="Item not found")
  128. if not crud.user.is_superuser(current_user) and (nft.userid != current_user.userid):
  129. raise HTTPException(status_code=400, detail="Not enough permissions")
  130. nft = crud.nft.remove(db=db, id=id)
  131. return nft