Implement image uploads and flight patching

This commit is contained in:
april
2024-01-12 15:48:36 -06:00
parent 7a0ea052f1
commit 5ab412d82a
7 changed files with 316 additions and 16 deletions

View File

@@ -1,12 +1,16 @@
import logging
from datetime import datetime
from typing import Any
from fastapi import APIRouter, HTTPException, Depends
from fastapi import APIRouter, HTTPException, Depends, Form, UploadFile, File
from app.deps import get_current_user, admin_required
from database import flights as db
from database.flights import update_flight_fields
from database.img import upload_image
from schemas.flight import FlightConciseSchema, FlightDisplaySchema, FlightCreateSchema, FlightByDateSchema
from schemas.flight import FlightConciseSchema, FlightDisplaySchema, FlightCreateSchema, FlightByDateSchema, \
FlightSchema
from schemas.user import UserDisplaySchema, AuthLevel
router = APIRouter()
@@ -109,20 +113,49 @@ async def get_flight(flight_id: str, user: UserDisplaySchema = Depends(get_curre
@router.post('/', summary="Add a flight logbook entry", status_code=200)
async def add_flight(flight_body: FlightCreateSchema, user: UserDisplaySchema = Depends(get_current_user)) -> dict:
async def add_flight(flight_body: FlightSchema, user: UserDisplaySchema = Depends(get_current_user)) -> dict:
"""
Add a flight logbook entry
:param flight_body: Information associated with new flight
:param images: Images associated with the new flight log
:param user: Currently logged-in user
:return: Error message if request invalid, else ID of newly created log
:return: ID of newly created log
"""
flight = await db.insert_flight(flight_body, user.id)
flight_create = FlightCreateSchema(**flight_body.model_dump(), images=[])
flight = await db.insert_flight(flight_create, user.id)
return {"id": str(flight)}
@router.post('/{flight_id}/add_images', summary="Add images to a flight log")
async def add_images(log_id: str, images: list[UploadFile] = File(...),
user: UserDisplaySchema = Depends(get_current_user)):
"""
Add images to a flight logbook entry
:param log_id: ID of flight log to add images to
:param images: Images to add
:param user: Currently logged-in user
:return: ID of updated flight
"""
flight = await db.retrieve_flight(log_id)
if not str(flight.user) == user.id and not user.level == AuthLevel.ADMIN:
raise HTTPException(403, "Unauthorized access")
image_ids = flight.images
if images:
for image in images:
image_response = await upload_image(image, user.id)
image_ids.append(image_response["file_id"])
return await update_flight_fields(log_id, dict(images=image_ids))
@router.put('/{flight_id}', summary="Update the given flight with new information", status_code=200)
async def update_flight(flight_id: str, flight_body: FlightCreateSchema,
user: UserDisplaySchema = Depends(get_current_user)) -> dict:
@@ -132,7 +165,7 @@ async def update_flight(flight_id: str, flight_body: FlightCreateSchema,
:param flight_id: ID of flight to update
:param flight_body: New flight information to update with
:param user: Currently logged-in user
:return: Updated flight
:return: ID of updated flight
"""
flight = await get_flight(flight_id, user)
if flight is None:
@@ -147,6 +180,29 @@ async def update_flight(flight_id: str, flight_body: FlightCreateSchema,
return {"id": str(updated_flight_id)}
@router.patch('/{flight_id}', summary="Update a single field of the given flight with new information", status_code=200)
async def patch_flight(flight_id: str, update: dict,
user: UserDisplaySchema = Depends(get_current_user)) -> dict:
"""
Update a single field of the given flight
:param flight_id: ID of flight to update
:param update: Dictionary of fields and values to update
:param user: Currently logged-in user
:return: ID of updated flight
"""
flight = await get_flight(flight_id, user)
if flight is None:
raise HTTPException(404, "Flight not found")
if str(flight.user) != user.id and AuthLevel(user.level) != AuthLevel.ADMIN:
logger.info("Attempted access to unauthorized flight by %s", user.username)
raise HTTPException(403, "Unauthorized access")
updated_flight_id = await db.update_flight_fields(flight_id, update)
return {"id": str(updated_flight_id)}
@router.delete('/{flight_id}', summary="Delete the given flight", status_code=200, response_model=FlightDisplaySchema)
async def delete_flight(flight_id: str, user: UserDisplaySchema = Depends(get_current_user)) -> FlightDisplaySchema:
"""

69
api/routes/img.py Normal file
View File

@@ -0,0 +1,69 @@
import logging
import mimetypes
import os
from fastapi import APIRouter, UploadFile, File, Path, Depends, HTTPException
from starlette.responses import StreamingResponse
from app.deps import get_current_user
from database import img
from schemas.user import UserDisplaySchema, AuthLevel
router = APIRouter()
logger = logging.getLogger("img")
@router.get("/{image_id}", description="Retrieve an image from the database")
async def get_image(user: UserDisplaySchema = Depends(get_current_user),
image_id: str = Path(..., description="ID of image to retrieve")) -> StreamingResponse:
"""
Retrieve an image from the database
:param user: Current user
:param image_id: ID of image to retrieve
:return: Stream associated with requested image
"""
stream, user_created = await img.retrieve_image(image_id)
if not user.id == user_created and not user.level == AuthLevel.ADMIN:
raise HTTPException(403, "Access denied")
file_extension = os.path.splitext(image_id)[1]
media_type = mimetypes.types_map.get(file_extension)
return StreamingResponse(stream, media_type=media_type)
@router.post("/upload", description="Upload an image to the database")
async def upload_image(user: UserDisplaySchema = Depends(get_current_user),
image: UploadFile = File(..., description="Image file to upload")) -> dict:
"""
Upload the given image to the database
:param user: Current user
:param image: Image to upload
:return: Image filename and id
"""
return await img.upload_image(image, str(user.id))
@router.delete("/{image_id}", description="Delete the given image from the database")
async def delete_image(user: UserDisplaySchema = Depends(get_current_user),
image_id: str = Path(..., description="ID of image to delete")):
"""
Delete the given image from the database
:param user: Current user
:param image_id: ID of image to delete
:return:
"""
metadata = await img.retrieve_image_metadata(image_id)
if not user.id == metadata["user"] and not user.level == AuthLevel.ADMIN:
raise HTTPException(403, "Access denied")
if metadata is None:
raise HTTPException(404, "Image not found")
return await img.delete_image(image_id)