From e858e7d7f407231c0bb5ebab095c1c9f783b55de Mon Sep 17 00:00:00 2001 From: profitroll Date: Thu, 23 Mar 2023 12:34:18 +0100 Subject: [PATCH] Changed token search logic --- extensions/photos.py | 55 +++++++++++++++++++++----------------------- extensions/videos.py | 49 ++++++++++++++++----------------------- 2 files changed, 46 insertions(+), 58 deletions(-) diff --git a/extensions/photos.py b/extensions/photos.py index 8604af3..3077ada 100644 --- a/extensions/photos.py +++ b/extensions/photos.py @@ -444,6 +444,7 @@ async def photo_delete( photo_find_responses = { 400: SearchPageInvalidError().openapi, + 401: SearchTokenInvalidError().openapi, 404: AlbumNameNotFoundError("name").openapi, 422: PhotoSearchQueryEmptyError().openapi, } @@ -451,7 +452,7 @@ photo_find_responses = { @app.get( "/albums/{album}/photos", - description="Find a photo by filename", + description="Find a photo by filename, caption, location or token", response_class=UJSONResponse, response_model=SearchResultsPhoto, responses=photo_find_responses, @@ -460,6 +461,7 @@ async def photo_find( album: str, q: Union[str, None] = None, caption: Union[str, None] = None, + token: Union[str, None] = None, page: int = 1, page_size: int = 100, lat: Union[float, None] = None, @@ -467,6 +469,24 @@ async def photo_find( radius: Union[int, None] = None, current_user: User = Security(get_current_active_user, scopes=["photos.list"]), ): + if token is not None: + found_record = col_tokens.find_one({"token": token}) + + if found_record is None: + raise SearchTokenInvalidError() + + return await photo_find( + album=album, + q=found_record["query"], + caption=found_record["caption"], + lat=found_record["lat"], + lng=found_record["lng"], + radius=found_record["radius"], + page=found_record["page"], + page_size=found_record["page_size"], + current_user=current_user, + ) + if col_albums.find_one({"user": current_user.user, "name": album}) is None: raise AlbumNameNotFoundError(album) @@ -543,39 +563,16 @@ async def photo_find( { "token": token, "query": q, - "album": album, + "caption": caption, + "lat": lat, + "lng": lng, + "radius": radius, "page": page + 1, "page_size": page_size, - "user": pickle.dumps(current_user), } ) - output["next_page"] = f"/albums/{album}/photos/token?token={token}" # type: ignore + output["next_page"] = f"/albums/{album}/photos/?token={token}" # type: ignore else: output["next_page"] = None # type: ignore return UJSONResponse(output) - - -photo_find_token_responses = {401: SearchTokenInvalidError().openapi} - - -@app.get( - "/albums/{album}/photos/token", - description="Find a photo by token", - response_class=UJSONResponse, - response_model=SearchResultsPhoto, - responses=photo_find_token_responses, -) -async def photo_find_token(token: str): - found_record = col_tokens.find_one({"token": token}) - - if found_record is None: - raise SearchTokenInvalidError() - - 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"]), - ) diff --git a/extensions/videos.py b/extensions/videos.py index 689b791..e19d2d7 100644 --- a/extensions/videos.py +++ b/extensions/videos.py @@ -260,6 +260,7 @@ async def video_delete( video_find_responses = { 400: SearchPageInvalidError().openapi, + 401: SearchTokenInvalidError().openapi, 404: AlbumNameNotFoundError("name").openapi, 422: VideoSearchQueryEmptyError().openapi, } @@ -267,7 +268,7 @@ video_find_responses = { @app.get( "/albums/{album}/videos", - description="Find a video by filename", + description="Find a video by filename, caption or token", response_class=UJSONResponse, response_model=SearchResultsVideo, responses=video_find_responses, @@ -276,10 +277,26 @@ async def video_find( album: str, q: Union[str, None] = None, caption: Union[str, None] = None, + token: Union[str, None] = None, page: int = 1, page_size: int = 100, current_user: User = Security(get_current_active_user, scopes=["videos.list"]), ): + if token is not None: + found_record = col_tokens.find_one({"token": token}) + + if found_record is None: + raise SearchTokenInvalidError() + + return await video_find( + album=album, + q=found_record["query"], + caption=found_record["caption"], + page=found_record["page"], + page_size=found_record["page_size"], + current_user=current_user, + ) + if col_albums.find_one({"user": current_user.user, "name": album}) is None: raise AlbumNameNotFoundError(album) @@ -341,39 +358,13 @@ async def video_find( { "token": token, "query": q, - "album": album, + "caption": caption, "page": page + 1, "page_size": page_size, - "user": pickle.dumps(current_user), } ) - output["next_page"] = f"/albums/{album}/videos/token?token={token}" # type: ignore + output["next_page"] = f"/albums/{album}/videos/?token={token}" # type: ignore else: output["next_page"] = None # type: ignore return UJSONResponse(output) - - -video_find_token_responses = {401: SearchTokenInvalidError().openapi} - - -@app.get( - "/albums/{album}/videos/token", - description="Find a video by token", - response_class=UJSONResponse, - response_model=SearchResultsVideo, - responses=video_find_token_responses, -) -async def video_find_token(token: str): - found_record = col_tokens.find_one({"token": token}) - - if found_record is None: - raise SearchTokenInvalidError() - - 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"]), - )