diff --git a/extensions/albums.py b/extensions/albums.py index dc2f97a..7a80576 100644 --- a/extensions/albums.py +++ b/extensions/albums.py @@ -13,20 +13,20 @@ 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", response_class=UJSONResponse, description="Create album with name and title") # response_model=Album, +@app.post("/albums", response_class=UJSONResponse, response_model=Album, description="Create album with name and title") 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: - return HTTPException(status_code=HTTP_406_NOT_ACCEPTABLE, detail="Album name can only contain: a-z, 0-9 and _ characters.") + raise HTTPException(status_code=HTTP_406_NOT_ACCEPTABLE, detail="Album name can only contain: a-z, 0-9 and _ characters.") if 2 > len(name) > 20: - return HTTPException(status_code=HTTP_406_NOT_ACCEPTABLE, detail="Album name must be >2 and <20 characters.") + raise HTTPException(status_code=HTTP_406_NOT_ACCEPTABLE, detail="Album name must be >2 and <20 characters.") if 2 > len(title) > 40: - return HTTPException(status_code=HTTP_406_NOT_ACCEPTABLE, detail="Album title must be >2 and <40 characters.") + raise HTTPException(status_code=HTTP_406_NOT_ACCEPTABLE, detail="Album title must be >2 and <40 characters.") if col_albums.find_one( {"name": name} ) is not None: - return HTTPException(status_code=HTTP_409_CONFLICT, detail=f"Album with name '{name}' already exists.") + raise HTTPException(status_code=HTTP_409_CONFLICT, detail=f"Album with name '{name}' already exists.") makedirs(path.join("data", "users", current_user.user, "albums", name), exist_ok=True) @@ -59,19 +59,19 @@ async def album_patch(id: str, name: Union[str, None] = None, title: Union[str, if album is None: raise InvalidId(id) except InvalidId: - return HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Could not find an album with such id.") + raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Could not find an album with such id.") if title is not None: if 2 > len(title) > 40: - return HTTPException(status_code=HTTP_406_NOT_ACCEPTABLE, detail="Album title must be >2 and <40 characters.") + raise HTTPException(status_code=HTTP_406_NOT_ACCEPTABLE, detail="Album 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: - return HTTPException(status_code=HTTP_406_NOT_ACCEPTABLE, detail="Album name can only contain: a-z, 0-9 and _ characters.") + raise HTTPException(status_code=HTTP_406_NOT_ACCEPTABLE, detail="Album name can only contain: a-z, 0-9 and _ characters.") if 2 > len(name) > 20: - return HTTPException(status_code=HTTP_406_NOT_ACCEPTABLE, detail="Album name must be >2 and <20 characters.") + raise HTTPException(status_code=HTTP_406_NOT_ACCEPTABLE, detail="Album 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) @@ -104,16 +104,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: - return HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Could not find an album with such id.") + raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Could not find an album with such id.") if re.search(re.compile('^[a-z,0-9,_]*$'), name) is False: - return HTTPException(status_code=HTTP_406_NOT_ACCEPTABLE, detail="Album name can only contain: a-z, 0-9 and _ characters.") + raise HTTPException(status_code=HTTP_406_NOT_ACCEPTABLE, detail="Album name can only contain: a-z, 0-9 and _ characters.") if 2 > len(name) > 20: - return HTTPException(status_code=HTTP_406_NOT_ACCEPTABLE, detail="Album name must be >2 and <20 characters.") + raise HTTPException(status_code=HTTP_406_NOT_ACCEPTABLE, detail="Album name must be >2 and <20 characters.") if 2 > len(title) > 40: - return HTTPException(status_code=HTTP_406_NOT_ACCEPTABLE, detail="Album title must be >2 and <40 characters.") + raise HTTPException(status_code=HTTP_406_NOT_ACCEPTABLE, detail="Album 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 @@ -142,7 +142,7 @@ async def album_delete(id: str, current_user: User = Security(get_current_active if album is None: raise InvalidId(id) except InvalidId: - return HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Could not find an album with such id.") + raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Could not find an album with such id.") col_photos.delete_many( {"album": album["name"]} ) diff --git a/extensions/photos.py b/extensions/photos.py index 388937b..a8585d5 100644 --- a/extensions/photos.py +++ b/extensions/photos.py @@ -55,10 +55,10 @@ async def compress_image(image_path: str): async def photo_upload(file: UploadFile, album: str, ignore_duplicates: bool = False, compress: bool = True, caption: Union[str, None] = None, current_user: User = Security(get_current_active_user, scopes=["photos.write"])): if col_albums.find_one( {"user": current_user.user, "name": album} ) is None: - return HTTPException(status_code=HTTP_404_NOT_FOUND, detail=f"Provided album '{album}' does not exist.") + raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail=f"Provided album '{album}' does not exist.") # if not file.content_type.startswith("image"): - # return HTTPException(status_code=HTTP_406_NOT_ACCEPTABLE, detail="Provided file is not an image, not accepting.") + # raise HTTPException(status_code=HTTP_406_NOT_ACCEPTABLE, detail="Provided file is not an image, not accepting.") makedirs(path.join("data", "users", current_user.user, "albums", album), exist_ok=True) @@ -132,7 +132,7 @@ async def photo_get(id: str, current_user: User = Security(get_current_active_us if image is None: raise InvalidId(id) except InvalidId: - return HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Could not find an image with such id.") + raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Could not find an image with such id.") image_path = path.join("data", "users", current_user.user, "albums", image["album"], image["filename"]) @@ -150,10 +150,10 @@ async def photo_move(id: str, album: str, current_user: User = Security(get_curr if image is None: raise InvalidId(id) except InvalidId: - return HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Could not find an image with such id.") + raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Could not find an image with such id.") if col_albums.find_one( {"user": current_user.user, "name": album} ) is None: - return HTTPException(status_code=HTTP_404_NOT_FOUND, detail=f"Provided album '{album}' does not exist.") + raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail=f"Provided album '{album}' does not exist.") if path.exists(path.join("data", "users", current_user.user, "albums", album, image["filename"])): base_name = image["filename"].split(".")[:-1] @@ -184,7 +184,7 @@ async def photo_patch(id: str, caption: str, current_user: User = Security(get_c if image is None: raise InvalidId(id) except InvalidId: - return HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Could not find an image with such id.") + raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Could not find an image with such id.") col_photos.find_one_and_update( {"_id": ObjectId(id)}, {"$set": {"caption": caption, "dates.modified": datetime.now(tz=timezone.utc)}} ) @@ -203,7 +203,7 @@ async def photo_delete(id: str, current_user: User = Security(get_current_active if image is None: raise InvalidId(id) except InvalidId: - return HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Could not find an image with such id.") + raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Could not find an image with such id.") album = col_albums.find_one( {"name": image["album"]} ) @@ -218,10 +218,10 @@ async def photo_delete(id: str, current_user: User = Security(get_current_active async def photo_find(album: str, q: Union[str, None] = None, caption: Union[str, None] = None, page: int = 1, page_size: int = 100, lat: Union[float, None] = None, lng: Union[float, None] = None, radius: Union[int, None] = None, current_user: User = Security(get_current_active_user, scopes=["photos.list"])): if col_albums.find_one( {"user": current_user.user, "name": album} ) is None: - return HTTPException(status_code=HTTP_404_NOT_FOUND, detail=f"Provided album '{album}' does not exist.") + raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail=f"Provided album '{album}' does not exist.") if page <= 0 or page_size <= 0: - return HTTPException(status_code=HTTP_400_BAD_REQUEST, detail="Parameters 'page' and 'page_size' must be greater or equal to 1.") + raise HTTPException(status_code=HTTP_400_BAD_REQUEST, detail="Parameters 'page' and 'page_size' must be greater or equal to 1.") output = {"results": []} skip = (page-1)*page_size @@ -263,6 +263,6 @@ async def photo_find_token(token: str): found_record = col_tokens.find_one( {"token": token} ) if found_record is None: - return HTTPException(status_code=HTTP_401_UNAUTHORIZED, detail="Invalid search token.") + raise HTTPException(status_code=HTTP_401_UNAUTHORIZED, detail="Invalid search token.") return await photo_find(q=found_record["query"], album=found_record["album"], page=found_record["page"], page_size=found_record["page_size"], current_user=pickle.loads(found_record["user"])) \ No newline at end of file diff --git a/extensions/users.py b/extensions/users.py index f8bcf12..4b37601 100644 --- a/extensions/users.py +++ b/extensions/users.py @@ -44,7 +44,7 @@ if configGet("registration_requires_confirmation") is True: async def user_confirm(user: str, code: str): confirm_record = col_emails.find_one( {"user": user, "code": code, "used": False} ) if confirm_record is None: - return HTTPException(HTTP_400_BAD_REQUEST, detail=configGet("email_code_invalid", "messages")) + raise HTTPException(HTTP_400_BAD_REQUEST, detail=configGet("email_code_invalid", "messages")) col_emails.find_one_and_update( {"_id": confirm_record["_id"]}, {"$set": {"used": True}} ) col_users.find_one_and_update( {"user": confirm_record["user"]}, {"$set": {"disabled": False}} ) return UJSONResponse( {"detail": configGet("email_confirmed", "messages")} ) @@ -53,7 +53,7 @@ if configGet("registration_enabled") is True: @app.post("/users") async def user_create(user: str = Form(), email: str = Form(), password: str = Form()): if col_users.find_one( {"user": user} ) is not None: - return HTTPException(HTTP_406_NOT_ACCEPTABLE, detail=configGet("user_already_exists", "messages")) + raise HTTPException(HTTP_406_NOT_ACCEPTABLE, detail=configGet("user_already_exists", "messages")) col_users.insert_one( {"user": user, "email": email, "hash": get_password_hash(password), "disabled": configGet("registration_requires_confirmation")} ) if configGet("registration_requires_confirmation") is True: scheduler.add_job( send_confirmation, trigger="date", run_date=datetime.now()+timedelta(seconds=1), kwargs={"user": user, "email": email} ) @@ -66,7 +66,7 @@ async def user_delete(password: str = Form(), current_user: User = Depends(get_c if not user: return False if not verify_password(password, user.hash): - return HTTPException(HTTP_400_BAD_REQUEST, detail=configGet("credentials_invalid", "messages")) + raise HTTPException(HTTP_400_BAD_REQUEST, detail=configGet("credentials_invalid", "messages")) col_users.delete_many( {"user": current_user.user} ) col_emails.delete_many( {"user": current_user.user} ) col_photos.delete_many( {"user": current_user.user} ) diff --git a/extensions/videos.py b/extensions/videos.py index 69dfa16..780ae05 100644 --- a/extensions/videos.py +++ b/extensions/videos.py @@ -24,10 +24,10 @@ from starlette.status import HTTP_204_NO_CONTENT, HTTP_400_BAD_REQUEST, HTTP_401 async def video_upload(file: UploadFile, album: str, caption: Union[str, None] = None, current_user: User = Security(get_current_active_user, scopes=["videos.write"])): if col_albums.find_one( {"user": current_user.user, "name": album} ) is None: - return HTTPException(status_code=HTTP_404_NOT_FOUND, detail=f"Provided album '{album}' does not exist.") + raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail=f"Provided album '{album}' does not exist.") # if not file.content_type.startswith("video"): - # return HTTPException(status_code=HTTP_406_NOT_ACCEPTABLE, detail="Provided file is not a video, not accepting.") + # raise HTTPException(status_code=HTTP_406_NOT_ACCEPTABLE, detail="Provided file is not a video, not accepting.") makedirs(path.join("data", "users", current_user.user, "albums", album), exist_ok=True) @@ -88,7 +88,7 @@ async def video_get(id: str, current_user: User = Security(get_current_active_us if video is None: raise InvalidId(id) except InvalidId: - return HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Could not find a video with such id.") + raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Could not find a video with such id.") video_path = path.join("data", "users", current_user.user, "albums", video["album"], video["filename"]) @@ -106,10 +106,10 @@ async def video_move(id: str, album: str, current_user: User = Security(get_curr if video is None: raise InvalidId(id) except InvalidId: - return HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Could not find an video with such id.") + raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Could not find an video with such id.") if col_albums.find_one( {"user": current_user.user, "name": album} ) is None: - return HTTPException(status_code=HTTP_404_NOT_FOUND, detail=f"Provided album '{album}' does not exist.") + raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail=f"Provided album '{album}' does not exist.") if path.exists(path.join("data", "users", current_user.user, "albums", album, video["filename"])): base_name = video["filename"].split(".")[:-1] @@ -140,7 +140,7 @@ async def video_patch(id: str, caption: str, current_user: User = Security(get_c if video is None: raise InvalidId(id) except InvalidId: - return HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Could not find an video with such id.") + raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Could not find an video with such id.") col_videos.find_one_and_update( {"_id": ObjectId(id)}, {"$set": {"caption": caption, "dates.modified": datetime.now(tz=timezone.utc)}} ) @@ -159,7 +159,7 @@ async def video_delete(id: str, current_user: User = Security(get_current_active if video is None: raise InvalidId(id) except InvalidId: - return HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Could not find a video with such id.") + raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Could not find a video with such id.") album = col_albums.find_one( {"name": video["album"]} ) @@ -171,10 +171,10 @@ async def video_delete(id: str, current_user: User = Security(get_current_active async def video_find(album: str, q: Union[str, None] = None, caption: Union[str, None] = None, page: int = 1, page_size: int = 100, current_user: User = Security(get_current_active_user, scopes=["videos.list"])): if col_albums.find_one( {"user": current_user.user, "name": album} ) is None: - return HTTPException(status_code=HTTP_404_NOT_FOUND, detail=f"Provided album '{album}' does not exist.") + raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail=f"Provided album '{album}' does not exist.") if page <= 0 or page_size <= 0: - return HTTPException(status_code=HTTP_400_BAD_REQUEST, detail="Parameters 'page' and 'page_size' must be greater or equal to 1.") + raise HTTPException(status_code=HTTP_400_BAD_REQUEST, detail="Parameters 'page' and 'page_size' must be greater or equal to 1.") output = {"results": []} skip = (page-1)*page_size @@ -212,6 +212,6 @@ async def video_find_token(token: str): found_record = col_tokens.find_one( {"token": token} ) if found_record is None: - return HTTPException(status_code=HTTP_401_UNAUTHORIZED, detail="Invalid search token.") + raise HTTPException(status_code=HTTP_401_UNAUTHORIZED, detail="Invalid search token.") return await video_find(q=found_record["query"], album=found_record["album"], page=found_record["page"], page_size=found_record["page_size"], current_user=pickle.loads(found_record["user"])) \ No newline at end of file