PhotosAPI/extensions/albums.py

139 lines
5.9 KiB
Python
Raw Normal View History

2022-12-20 02:22:32 +02:00
import re
2022-12-20 14:28:50 +02:00
from os import makedirs, path, rename
2022-12-20 02:22:32 +02:00
from shutil import rmtree
from typing import Union
2022-12-20 14:28:50 +02:00
from modules.app import app
2022-12-20 02:22:32 +02:00
from modules.database import col_photos, col_albums
2022-12-20 12:37:32 +02:00
from modules.security import User, get_current_active_user
2022-12-20 02:22:32 +02:00
from bson.objectid import ObjectId
from bson.errors import InvalidId
2022-12-20 14:28:50 +02:00
from fastapi import HTTPException, Security
2022-12-20 02:22:32 +02:00
from fastapi.responses import UJSONResponse, Response
2022-12-20 14:28:50 +02:00
from starlette.status import HTTP_204_NO_CONTENT, HTTP_404_NOT_FOUND, HTTP_406_NOT_ACCEPTABLE, HTTP_409_CONFLICT
2022-12-20 02:22:32 +02:00
2022-12-20 02:33:24 +02:00
@app.post("/albums", response_class=UJSONResponse, description="Create album with name and title")
2022-12-20 14:28:50 +02:00
async def album_create(name: str, title: str, current_user: User = Security(get_current_active_user, scopes=["albums.write"])):
2022-12-20 02:22:32 +02:00
2022-12-20 14:28:50 +02:00
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.")
if 2 > len(name) > 20:
return HTTPException(status_code=HTTP_406_NOT_ACCEPTABLE, detail="Album name must be >2 and <20 characters.")
2022-12-20 02:22:32 +02:00
2022-12-20 14:28:50 +02:00
if 2 > len(title) > 40:
return HTTPException(status_code=HTTP_406_NOT_ACCEPTABLE, detail="Album title must be >2 and <40 characters.")
2022-12-20 02:22:32 +02:00
2022-12-20 14:28:50 +02:00
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.")
2022-12-20 02:22:32 +02:00
2022-12-20 14:28:50 +02:00
makedirs(path.join("data", "users", current_user.user, "albums", name), exist_ok=True)
2022-12-20 02:22:32 +02:00
2022-12-20 14:28:50 +02:00
uploaded = col_albums.insert_one( {"user": current_user.user, "name": name, "title": title} )
2022-12-20 02:22:32 +02:00
2022-12-20 14:28:50 +02:00
return UJSONResponse(
{
"id": uploaded.inserted_id.__str__(),
"name": name,
"title": title
}
)
2022-12-20 02:22:32 +02:00
2022-12-20 12:37:32 +02:00
@app.get("/albums", description="Find album by name")
2022-12-20 14:28:50 +02:00
async def album_find(q: str, current_user: User = Security(get_current_active_user, scopes=["albums.list"])):
2022-12-20 02:22:32 +02:00
2022-12-20 12:37:32 +02:00
output = {"results": []}
albums = list(col_albums.find( {"user": current_user.user, "name": re.compile(q)} ))
2022-12-20 02:22:32 +02:00
2022-12-20 12:37:32 +02:00
for album in albums:
output["results"].append( {"id": album["_id"].__str__(), "name": album["name"]} )
2022-12-20 02:22:32 +02:00
2022-12-20 12:37:32 +02:00
return UJSONResponse(output)
2022-12-20 02:22:32 +02:00
2022-12-20 02:33:24 +02:00
@app.patch("/albums/{id}", response_class=UJSONResponse, description="Modify album's name or title by id")
2022-12-20 14:28:50 +02:00
async def album_patch(id: str, name: Union[str, None] = None, title: Union[str, None] = None, current_user: User = Security(get_current_active_user, scopes=["albums.write"])):
2022-12-20 02:22:32 +02:00
2022-12-20 14:28:50 +02:00
try:
album = col_albums.find_one( {"_id": ObjectId(id)} )
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.")
2022-12-20 02:22:32 +02:00
2022-12-20 14:28:50 +02:00
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.")
else:
title = album["title"]
2022-12-20 02:22:32 +02:00
2022-12-20 14:28:50 +02:00
if name is not None:
2022-12-20 02:22:32 +02:00
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.")
if 2 > len(name) > 20:
return HTTPException(status_code=HTTP_406_NOT_ACCEPTABLE, detail="Album name must be >2 and <20 characters.")
2022-12-20 14:28:50 +02:00
rename(
path.join("data", "users", current_user.user, "albums", album["name"]),
path.join("data", "users", current_user.user, "albums", name)
)
col_photos.update_many( {"user": current_user.user, "album": album["name"]}, {"$set": {"album": name}} )
else:
name = album["name"]
2022-12-20 02:22:32 +02:00
2022-12-20 14:28:50 +02:00
col_albums.update_one( {"_id": ObjectId(id)}, {"$set": {"name": name, "title": title}} )
return UJSONResponse(
{
"name": name,
"title": title
}
)
2022-12-20 02:22:32 +02:00
2022-12-20 14:28:50 +02:00
@app.put("/albums/{id}", response_class=UJSONResponse, description="Modify album's name and title by id")
async def album_put(id: str, name: str, title: str, current_user: User = Security(get_current_active_user, scopes=["albums.write"])):
2022-12-20 02:22:32 +02:00
2022-12-20 14:28:50 +02:00
try:
album = col_albums.find_one( {"_id": ObjectId(id)} )
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.")
2022-12-20 02:22:32 +02:00
2022-12-20 14:28:50 +02:00
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.")
2022-12-20 02:22:32 +02:00
2022-12-20 14:28:50 +02:00
if 2 > len(name) > 20:
return HTTPException(status_code=HTTP_406_NOT_ACCEPTABLE, detail="Album name must be >2 and <20 characters.")
2022-12-20 02:22:32 +02:00
2022-12-20 14:28:50 +02:00
if 2 > len(title) > 40:
return HTTPException(status_code=HTTP_406_NOT_ACCEPTABLE, detail="Album title must be >2 and <40 characters.")
2022-12-20 02:22:32 +02:00
2022-12-20 14:28:50 +02:00
rename(
path.join("data", "users", current_user.user, "albums", album["name"]),
path.join("data", "users", current_user.user, "albums", name)
)
2022-12-20 02:22:32 +02:00
2022-12-20 14:28:50 +02:00
col_photos.update_many( {"user": current_user.user, "album": album["name"]}, {"$set": {"album": name}} )
col_albums.update_one( {"_id": ObjectId(id)}, {"$set": {"name": name, "title": title}} )
2022-12-20 02:22:32 +02:00
2022-12-20 14:28:50 +02:00
return UJSONResponse(
{
"name": name,
"title": title
}
)
2022-12-20 02:22:32 +02:00
2022-12-20 14:28:50 +02:00
@app.delete("/album/{id}", response_class=UJSONResponse, description="Delete album by id")
async def album_delete(id: str, current_user: User = Security(get_current_active_user, scopes=["albums.write"])):
2022-12-20 02:22:32 +02:00
2022-12-20 14:28:50 +02:00
try:
album = col_albums.find_one_and_delete( {"_id": ObjectId(id)} )
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.")
col_photos.delete_many( {"album": album["name"]} )
rmtree(path.join("data", "users", current_user.user, "albums", album["name"]))
return Response(status_code=HTTP_204_NO_CONTENT)