WIP: Custom exceptions handling and new models
This commit is contained in:
@@ -2,7 +2,8 @@ import re
|
||||
from os import makedirs, path, rename
|
||||
from shutil import rmtree
|
||||
from typing import Union
|
||||
from classes.models import Album, AlbumModified, SearchResults
|
||||
from classes.exceptions import AlbumAlreadyExistsError, AlbumIncorrectError, AlbumNotFoundError
|
||||
from classes.models import Album, AlbumModified, SearchResultsAlbum
|
||||
from modules.app import app
|
||||
from modules.database import col_photos, col_albums
|
||||
from modules.security import User, get_current_active_user
|
||||
@@ -13,20 +14,24 @@ from fastapi import HTTPException, Security
|
||||
from fastapi.responses import UJSONResponse, Response
|
||||
from starlette.status import HTTP_204_NO_CONTENT, HTTP_404_NOT_FOUND, HTTP_406_NOT_ACCEPTABLE, HTTP_409_CONFLICT
|
||||
|
||||
@app.post("/albums", description="Create album with name and title", response_class=UJSONResponse, response_model=Album)
|
||||
album_create_responses = {
|
||||
406: AlbumIncorrectError("name", "error").openapi,
|
||||
409: AlbumAlreadyExistsError("name").openapi
|
||||
}
|
||||
@app.post("/albums", description="Create album with name and title", response_class=UJSONResponse, response_model=Album, responses=album_create_responses)
|
||||
async def album_create(name: str, title: str, current_user: User = Security(get_current_active_user, scopes=["albums.write"])):
|
||||
|
||||
if re.search(re.compile('^[a-z,0-9,_]*$'), name) is False:
|
||||
raise HTTPException(status_code=HTTP_406_NOT_ACCEPTABLE, detail="Album name can only contain: a-z, 0-9 and _ characters.")
|
||||
raise AlbumIncorrectError("name", "can only contain a-z, 0-9 and _ characters.")
|
||||
|
||||
if 2 > len(name) > 20:
|
||||
raise HTTPException(status_code=HTTP_406_NOT_ACCEPTABLE, detail="Album name must be >2 and <20 characters.")
|
||||
raise AlbumIncorrectError("name", "must be >2 and <20 characters.")
|
||||
|
||||
if 2 > len(title) > 40:
|
||||
raise HTTPException(status_code=HTTP_406_NOT_ACCEPTABLE, detail="Album title must be >2 and <40 characters.")
|
||||
raise AlbumIncorrectError("title", "must be >2 and <40 characters.")
|
||||
|
||||
if col_albums.find_one( {"name": name} ) is not None:
|
||||
raise HTTPException(status_code=HTTP_409_CONFLICT, detail=f"Album with name '{name}' already exists.")
|
||||
raise AlbumAlreadyExistsError(name)
|
||||
|
||||
makedirs(path.join("data", "users", current_user.user, "albums", name), exist_ok=True)
|
||||
|
||||
@@ -40,18 +45,22 @@ async def album_create(name: str, title: str, current_user: User = Security(get_
|
||||
}
|
||||
)
|
||||
|
||||
@app.get("/albums", description="Find album by name", response_model=SearchResults)
|
||||
@app.get("/albums", description="Find album by name", response_model=SearchResultsAlbum)
|
||||
async def album_find(q: str, current_user: User = Security(get_current_active_user, scopes=["albums.list"])):
|
||||
|
||||
output = {"results": []}
|
||||
albums = list(col_albums.find( {"user": current_user.user, "name": re.compile(q)} ))
|
||||
|
||||
for album in albums:
|
||||
output["results"].append( {"id": album["_id"].__str__(), "name": album["name"]} )
|
||||
output["results"].append( {"id": album["_id"].__str__(), "name": album["name"], "title": album["title"]} )
|
||||
|
||||
return UJSONResponse(output)
|
||||
|
||||
@app.patch("/albums/{id}", description="Modify album's name or title by id", response_class=UJSONResponse, response_model=AlbumModified)
|
||||
album_patch_responses = {
|
||||
404: AlbumNotFoundError("id").openapi,
|
||||
406: AlbumIncorrectError("name", "error").openapi
|
||||
}
|
||||
@app.patch("/albums/{id}", description="Modify album's name or title by id", response_class=UJSONResponse, response_model=AlbumModified, responses=album_patch_responses)
|
||||
async def album_patch(id: str, name: Union[str, None] = None, title: Union[str, None] = None, cover: Union[str, None] = None, current_user: User = Security(get_current_active_user, scopes=["albums.write"])):
|
||||
|
||||
try:
|
||||
@@ -59,19 +68,19 @@ async def album_patch(id: str, name: Union[str, None] = None, title: Union[str,
|
||||
if album is None:
|
||||
raise InvalidId(id)
|
||||
except InvalidId:
|
||||
raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Could not find an album with such id.")
|
||||
raise AlbumNotFoundError(id)
|
||||
|
||||
if title is not None:
|
||||
if 2 > len(title) > 40:
|
||||
raise HTTPException(status_code=HTTP_406_NOT_ACCEPTABLE, detail="Album title must be >2 and <40 characters.")
|
||||
raise AlbumIncorrectError("title", "must be >2 and <40 characters.")
|
||||
else:
|
||||
title = album["title"]
|
||||
|
||||
if name is not None:
|
||||
if re.search(re.compile('^[a-z,0-9,_]*$'), name) is False:
|
||||
raise HTTPException(status_code=HTTP_406_NOT_ACCEPTABLE, detail="Album name can only contain: a-z, 0-9 and _ characters.")
|
||||
raise AlbumIncorrectError("name", "can only contain a-z, 0-9 and _ characters.")
|
||||
if 2 > len(name) > 20:
|
||||
raise HTTPException(status_code=HTTP_406_NOT_ACCEPTABLE, detail="Album name must be >2 and <20 characters.")
|
||||
raise AlbumIncorrectError("name", "must be >2 and <20 characters.")
|
||||
rename(
|
||||
path.join("data", "users", current_user.user, "albums", album["name"]),
|
||||
path.join("data", "users", current_user.user, "albums", name)
|
||||
@@ -96,7 +105,11 @@ async def album_patch(id: str, name: Union[str, None] = None, title: Union[str,
|
||||
}
|
||||
)
|
||||
|
||||
@app.put("/albums/{id}", description="Modify album's name and title by id", response_class=UJSONResponse, response_model=AlbumModified)
|
||||
album_put_responses = {
|
||||
404: AlbumNotFoundError("id").openapi,
|
||||
406: AlbumIncorrectError("name", "error").openapi
|
||||
}
|
||||
@app.put("/albums/{id}", description="Modify album's name and title by id", response_class=UJSONResponse, response_model=AlbumModified, responses=album_put_responses)
|
||||
async def album_put(id: str, name: str, title: str, cover: str, current_user: User = Security(get_current_active_user, scopes=["albums.write"])):
|
||||
|
||||
try:
|
||||
@@ -104,16 +117,16 @@ async def album_put(id: str, name: str, title: str, cover: str, current_user: Us
|
||||
if album is None:
|
||||
raise InvalidId(id)
|
||||
except InvalidId:
|
||||
raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Could not find an album with such id.")
|
||||
raise AlbumNotFoundError(id)
|
||||
|
||||
if re.search(re.compile('^[a-z,0-9,_]*$'), name) is False:
|
||||
raise HTTPException(status_code=HTTP_406_NOT_ACCEPTABLE, detail="Album name can only contain: a-z, 0-9 and _ characters.")
|
||||
raise AlbumIncorrectError("name", "can only contain a-z, 0-9 and _ characters.")
|
||||
|
||||
if 2 > len(name) > 20:
|
||||
raise HTTPException(status_code=HTTP_406_NOT_ACCEPTABLE, detail="Album name must be >2 and <20 characters.")
|
||||
raise AlbumIncorrectError("name", "must be >2 and <20 characters.")
|
||||
|
||||
if 2 > len(title) > 40:
|
||||
raise HTTPException(status_code=HTTP_406_NOT_ACCEPTABLE, detail="Album title must be >2 and <40 characters.")
|
||||
raise AlbumIncorrectError("title", "must be >2 and <40 characters.")
|
||||
|
||||
image = col_photos.find_one( {"_id": ObjectId(cover), "album": album["name"]} )
|
||||
cover = image["_id"].__str__() if image is not None else None # type: ignore
|
||||
@@ -134,7 +147,10 @@ async def album_put(id: str, name: str, title: str, cover: str, current_user: Us
|
||||
}
|
||||
)
|
||||
|
||||
@app.delete("/album/{id}", description="Delete album by id", response_class=UJSONResponse)
|
||||
album_delete_responses = {
|
||||
404: AlbumNotFoundError("id").openapi
|
||||
}
|
||||
@app.delete("/album/{id}", description="Delete album by id", status_code=HTTP_204_NO_CONTENT, responses=album_delete_responses)
|
||||
async def album_delete(id: str, current_user: User = Security(get_current_active_user, scopes=["albums.write"])):
|
||||
|
||||
try:
|
||||
@@ -142,7 +158,7 @@ async def album_delete(id: str, current_user: User = Security(get_current_active
|
||||
if album is None:
|
||||
raise InvalidId(id)
|
||||
except InvalidId:
|
||||
raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Could not find an album with such id.")
|
||||
raise AlbumNotFoundError(id)
|
||||
|
||||
col_photos.delete_many( {"album": album["name"]} )
|
||||
|
||||
|
Reference in New Issue
Block a user