Changed token search logic

This commit is contained in:
Profitroll 2023-03-23 12:34:18 +01:00
parent fcbbd4f2bf
commit e858e7d7f4
2 changed files with 46 additions and 58 deletions

View File

@ -444,6 +444,7 @@ async def photo_delete(
photo_find_responses = { photo_find_responses = {
400: SearchPageInvalidError().openapi, 400: SearchPageInvalidError().openapi,
401: SearchTokenInvalidError().openapi,
404: AlbumNameNotFoundError("name").openapi, 404: AlbumNameNotFoundError("name").openapi,
422: PhotoSearchQueryEmptyError().openapi, 422: PhotoSearchQueryEmptyError().openapi,
} }
@ -451,7 +452,7 @@ photo_find_responses = {
@app.get( @app.get(
"/albums/{album}/photos", "/albums/{album}/photos",
description="Find a photo by filename", description="Find a photo by filename, caption, location or token",
response_class=UJSONResponse, response_class=UJSONResponse,
response_model=SearchResultsPhoto, response_model=SearchResultsPhoto,
responses=photo_find_responses, responses=photo_find_responses,
@ -460,6 +461,7 @@ async def photo_find(
album: str, album: str,
q: Union[str, None] = None, q: Union[str, None] = None,
caption: Union[str, None] = None, caption: Union[str, None] = None,
token: Union[str, None] = None,
page: int = 1, page: int = 1,
page_size: int = 100, page_size: int = 100,
lat: Union[float, None] = None, lat: Union[float, None] = None,
@ -467,6 +469,24 @@ async def photo_find(
radius: Union[int, None] = None, radius: Union[int, None] = None,
current_user: User = Security(get_current_active_user, scopes=["photos.list"]), 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: if col_albums.find_one({"user": current_user.user, "name": album}) is None:
raise AlbumNameNotFoundError(album) raise AlbumNameNotFoundError(album)
@ -543,39 +563,16 @@ async def photo_find(
{ {
"token": token, "token": token,
"query": q, "query": q,
"album": album, "caption": caption,
"lat": lat,
"lng": lng,
"radius": radius,
"page": page + 1, "page": page + 1,
"page_size": page_size, "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: else:
output["next_page"] = None # type: ignore output["next_page"] = None # type: ignore
return UJSONResponse(output) 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"]),
)

View File

@ -260,6 +260,7 @@ async def video_delete(
video_find_responses = { video_find_responses = {
400: SearchPageInvalidError().openapi, 400: SearchPageInvalidError().openapi,
401: SearchTokenInvalidError().openapi,
404: AlbumNameNotFoundError("name").openapi, 404: AlbumNameNotFoundError("name").openapi,
422: VideoSearchQueryEmptyError().openapi, 422: VideoSearchQueryEmptyError().openapi,
} }
@ -267,7 +268,7 @@ video_find_responses = {
@app.get( @app.get(
"/albums/{album}/videos", "/albums/{album}/videos",
description="Find a video by filename", description="Find a video by filename, caption or token",
response_class=UJSONResponse, response_class=UJSONResponse,
response_model=SearchResultsVideo, response_model=SearchResultsVideo,
responses=video_find_responses, responses=video_find_responses,
@ -276,10 +277,26 @@ async def video_find(
album: str, album: str,
q: Union[str, None] = None, q: Union[str, None] = None,
caption: Union[str, None] = None, caption: Union[str, None] = None,
token: Union[str, None] = None,
page: int = 1, page: int = 1,
page_size: int = 100, page_size: int = 100,
current_user: User = Security(get_current_active_user, scopes=["videos.list"]), 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: if col_albums.find_one({"user": current_user.user, "name": album}) is None:
raise AlbumNameNotFoundError(album) raise AlbumNameNotFoundError(album)
@ -341,39 +358,13 @@ async def video_find(
{ {
"token": token, "token": token,
"query": q, "query": q,
"album": album, "caption": caption,
"page": page + 1, "page": page + 1,
"page_size": page_size, "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: else:
output["next_page"] = None # type: ignore output["next_page"] = None # type: ignore
return UJSONResponse(output) 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"]),
)