WIP: Migration to async_pymongo
This commit is contained in:
@@ -47,12 +47,12 @@ async def album_create(
|
||||
if 2 > len(title) > 40:
|
||||
raise AlbumIncorrectError("title", "must be >2 and <40 characters.")
|
||||
|
||||
if col_albums.find_one({"name": name}) is not None:
|
||||
if (await col_albums.find_one({"name": name})) is not None:
|
||||
raise AlbumAlreadyExistsError(name)
|
||||
|
||||
makedirs(Path(f"data/users/{current_user.user}/albums/{name}"), exist_ok=True)
|
||||
|
||||
uploaded = col_albums.insert_one(
|
||||
uploaded = await col_albums.insert_one(
|
||||
{"user": current_user.user, "name": name, "title": title, "cover": None}
|
||||
)
|
||||
|
||||
@@ -67,9 +67,10 @@ async def album_find(
|
||||
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:
|
||||
async for album in col_albums.find(
|
||||
{"user": current_user.user, "name": re.compile(q)}
|
||||
):
|
||||
output["results"].append(
|
||||
{
|
||||
"id": album["_id"].__str__(),
|
||||
@@ -102,11 +103,11 @@ async def album_patch(
|
||||
current_user: User = Security(get_current_active_user, scopes=["albums.write"]),
|
||||
):
|
||||
try:
|
||||
album = col_albums.find_one({"_id": ObjectId(id)})
|
||||
album = await col_albums.find_one({"_id": ObjectId(id)})
|
||||
if album is None:
|
||||
raise InvalidId(id)
|
||||
except InvalidId:
|
||||
raise AlbumNotFoundError(id)
|
||||
except InvalidId as exc:
|
||||
raise AlbumNotFoundError(id) from exc
|
||||
|
||||
if title is None:
|
||||
title = album["title"]
|
||||
@@ -125,7 +126,7 @@ async def album_patch(
|
||||
Path(f"data/users/{current_user.user}/albums/{album['name']}"),
|
||||
Path(f"data/users/{current_user.user}/albums/{name}"),
|
||||
)
|
||||
col_photos.update_many(
|
||||
await col_photos.update_many(
|
||||
{"user": current_user.user, "album": album["name"]},
|
||||
{"$set": {"album": name}},
|
||||
)
|
||||
@@ -133,12 +134,14 @@ async def album_patch(
|
||||
name = album["name"]
|
||||
|
||||
if cover is not None:
|
||||
image = col_photos.find_one({"_id": ObjectId(cover), "album": album["name"]})
|
||||
image = await col_photos.find_one(
|
||||
{"_id": ObjectId(cover), "album": album["name"]}
|
||||
)
|
||||
cover = image["_id"].__str__() if image is not None else album["cover"]
|
||||
else:
|
||||
cover = album["cover"]
|
||||
|
||||
col_albums.update_one(
|
||||
await col_albums.update_one(
|
||||
{"_id": ObjectId(id)}, {"$set": {"name": name, "title": title, "cover": cover}}
|
||||
)
|
||||
|
||||
@@ -166,11 +169,11 @@ async def album_put(
|
||||
current_user: User = Security(get_current_active_user, scopes=["albums.write"]),
|
||||
):
|
||||
try:
|
||||
album = col_albums.find_one({"_id": ObjectId(id)})
|
||||
album = await col_albums.find_one({"_id": ObjectId(id)})
|
||||
if album is None:
|
||||
raise InvalidId(id)
|
||||
except InvalidId:
|
||||
raise AlbumNotFoundError(id)
|
||||
except InvalidId as exc:
|
||||
raise AlbumNotFoundError(id) from exc
|
||||
|
||||
if re.search(re.compile("^[a-z,0-9,_]*$"), name) is False:
|
||||
raise AlbumIncorrectError("name", "can only contain a-z, 0-9 and _ characters.")
|
||||
@@ -181,7 +184,7 @@ async def album_put(
|
||||
if 2 > len(title) > 40:
|
||||
raise AlbumIncorrectError("title", "must be >2 and <40 characters.")
|
||||
|
||||
image = col_photos.find_one({"_id": ObjectId(cover), "album": album["name"]})
|
||||
image = await col_photos.find_one({"_id": ObjectId(cover), "album": album["name"]})
|
||||
cover = image["_id"].__str__() if image is not None else None # type: ignore
|
||||
|
||||
rename(
|
||||
@@ -189,10 +192,10 @@ async def album_put(
|
||||
Path(f"data/users/{current_user.user}/albums/{name}"),
|
||||
)
|
||||
|
||||
col_photos.update_many(
|
||||
await col_photos.update_many(
|
||||
{"user": current_user.user, "album": album["name"]}, {"$set": {"album": name}}
|
||||
)
|
||||
col_albums.update_one(
|
||||
await col_albums.update_one(
|
||||
{"_id": ObjectId(id)}, {"$set": {"name": name, "title": title, "cover": cover}}
|
||||
)
|
||||
|
||||
@@ -213,13 +216,13 @@ async def album_delete(
|
||||
current_user: User = Security(get_current_active_user, scopes=["albums.write"]),
|
||||
):
|
||||
try:
|
||||
album = col_albums.find_one_and_delete({"_id": ObjectId(id)})
|
||||
album = await col_albums.find_one_and_delete({"_id": ObjectId(id)})
|
||||
if album is None:
|
||||
raise InvalidId(id)
|
||||
except InvalidId:
|
||||
raise AlbumNotFoundError(id)
|
||||
except InvalidId as exc:
|
||||
raise AlbumNotFoundError(id) from exc
|
||||
|
||||
col_photos.delete_many({"album": album["name"]})
|
||||
await col_photos.delete_many({"album": album["name"]})
|
||||
|
||||
rmtree(Path(f"data/users/{current_user.user}/albums/{album['name']}"))
|
||||
|
||||
|
Reference in New Issue
Block a user