Admin: fix issue with import and error handling

This commit is contained in:
phil 2024-05-15 15:09:10 +02:00
parent 0bf74b2bba
commit 5a63892640
2 changed files with 98 additions and 72 deletions

View file

@ -1,13 +1,13 @@
import logging
from fastapi import (Depends, APIRouter, HTTPException, status, responses,
UploadFile)
from fastapi import Depends, APIRouter, HTTPException, status, responses, UploadFile
from fastapi.responses import FileResponse
from gisaf.models.admin import AdminBasket, BasketImportResult, BasketNameOnly
from gisaf.models.authentication import User, UserRead
from gisaf.security import get_current_active_user
from gisaf.admin import manager
from gisaf.importers import ImportError
logger = logging.getLogger(__name__)
@ -17,20 +17,22 @@ api = APIRouter(
responses={404: {"description": "Not found"}},
)
@api.get('/basket')
@api.get("/basket")
async def get_baskets(
user: User = Depends(get_current_active_user),
) -> list[BasketNameOnly]:
) -> list[BasketNameOnly]:
return [
BasketNameOnly(name=name)
for name, basket in (await manager.baskets_for_role(user)).items()
]
@api.get('/basket/{name}')
@api.get("/basket/{name}")
async def get_basket(
name: str,
user: User = Depends(get_current_active_user),
) -> AdminBasket:
) -> AdminBasket:
basket = manager.baskets[name]
if basket.role and not user.has_role(basket.role):
raise HTTPException(status.HTTP_401_UNAUTHORIZED)
@ -43,7 +45,8 @@ async def get_basket(
# projects=getattr(basket, 'projects', None)
)
@api.post('/basket/upload/{name}')
@api.post("/basket/upload/{name}")
async def upload_basket_file(
name: str,
file: UploadFile,
@ -66,59 +69,62 @@ async def upload_basket_file(
)
return fileItem
@api.get('/basket/download/{name}/{file_id}/{file_name}')
@api.get("/basket/download/{name}/{file_id}/{file_name}")
async def download_basket_file(
name: str,
file_id: int,
file_name: str,
user: User = Depends(get_current_active_user),
) -> FileResponse:
) -> FileResponse:
try:
basket = manager.baskets[name]
except KeyError:
raise HTTPException(status.HTTP_404_NOT_FOUND, f'No basket named {name}')
raise HTTPException(status.HTTP_404_NOT_FOUND, f"No basket named {name}")
if basket.role:
if not user.has_role(basket.role):
raise HTTPException(status.HTTP_401_UNAUTHORIZED)
file_record = await basket.get_file(file_id)
if file_record is None:
raise HTTPException(status.HTTP_404_NOT_FOUND, f'No import file id {file_id}')
raise HTTPException(status.HTTP_404_NOT_FOUND, f"No import file id {file_id}")
abs_path = basket.base_dir / file_record.path
if not abs_path.exists():
raise HTTPException(status.HTTP_404_NOT_FOUND, f'File {file_record.name} not found')
raise HTTPException(
status.HTTP_404_NOT_FOUND, f"File {file_record.name} not found"
)
return FileResponse(abs_path)
@api.get('/basket/import/{basket}/{file_id}')
@api.get("/basket/import/{basket}/{file_id}")
async def import_basket_file(
basket: str,
file_id: int,
dryRun: bool = False,
user: User = Depends(get_current_active_user),
) -> BasketImportResult:
if not (user and user.has_role('reviewer')):
) -> BasketImportResult:
if not (user and user.has_role("reviewer")):
raise HTTPException(status.HTTP_401_UNAUTHORIZED)
basket_ = manager.baskets[basket]
file_import = await basket_.get_file(file_id)
if file_import is None:
raise HTTPException(status.HTTP_404_NOT_FOUND, f'No import file id {file_id}')
raise HTTPException(status.HTTP_404_NOT_FOUND, f"No import file id {file_id}")
try:
result = await basket_.import_file(file_import, dryRun)
## FIXME: shouldn't it be AdminImportError?
except ImportError as err:
raise HTTPException(status.HTTP_500_INTERNAL_SERVER_ERROR, err)
raise HTTPException(status.HTTP_500_INTERNAL_SERVER_ERROR, str(err))
return result
@api.get('/basket/delete/{basket}/{file_id}')
@api.get("/basket/delete/{basket}/{file_id}")
async def delete_basket_file(
basket: str,
file_id: int,
user: User = Depends(get_current_active_user),
) -> None:
if not (user and user.has_role('reviewer')):
) -> None:
if not (user and user.has_role("reviewer")):
raise HTTPException(status.HTTP_401_UNAUTHORIZED)
basket_ = manager.baskets[basket]
file_import = await basket_.get_file(file_id)
if file_import is None:
raise HTTPException(status.HTTP_404_NOT_FOUND, f'No import file id {file_id}')
await basket_.delete_file(file_import)
raise HTTPException(status.HTTP_404_NOT_FOUND, f"No import file id {file_id}")
await basket_.delete_file(file_import)