feature: now using uv + moved to asyncpg from psycopg2
This commit is contained in:
+13
-13
@@ -1,7 +1,7 @@
|
||||
from typing import Annotated
|
||||
|
||||
from asyncpg import Connection
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from psycopg2._psycopg import connection
|
||||
|
||||
import db.accounts as db
|
||||
import settings.settings as settings
|
||||
@@ -17,22 +17,22 @@ accounts_router = APIRouter(prefix="/api/accounts", tags=["accounts"])
|
||||
@accounts_router.post("/group")
|
||||
async def read_accounts_by_group(
|
||||
groupname: str,
|
||||
conn: Annotated[connection, Depends(get_db_connection)],
|
||||
conn: Annotated[Connection, Depends(get_db_connection)],
|
||||
current_user: Annotated[User, Depends(get_current_user)]
|
||||
):
|
||||
if not check_group_author(conn, groupname, current_user.username) and current_user.role not in settings.settings.admin_roles:
|
||||
if not await check_group_author(conn, groupname, current_user.username) and current_user.role not in settings.settings.admin_roles:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Not allowed",
|
||||
)
|
||||
return db.get_accounts_by_group(conn, groupname)
|
||||
return await db.get_accounts_by_group(conn, groupname)
|
||||
|
||||
|
||||
@accounts_router.post("/group/platform")
|
||||
async def read_accounts_by_group_platform(
|
||||
groupname: str,
|
||||
platform: str,
|
||||
conn: Annotated[connection, Depends(get_db_connection)],
|
||||
conn: Annotated[Connection, Depends(get_db_connection)],
|
||||
current_user: Annotated[User, Depends(get_current_user)]
|
||||
):
|
||||
if current_user.role not in settings.settings.admin_roles:
|
||||
@@ -40,7 +40,7 @@ async def read_accounts_by_group_platform(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Not allowed",
|
||||
)
|
||||
account_data = db.get_accounts_by_group_platform(conn, groupname, platform.lower())
|
||||
account_data = await db.get_accounts_by_group_platform(conn, groupname, platform.lower())
|
||||
if account_data is None:
|
||||
return HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
@@ -56,21 +56,21 @@ async def add_account(
|
||||
login: str,
|
||||
password: str,
|
||||
metadata: dict,
|
||||
conn: Annotated[connection, Depends(get_db_connection)],
|
||||
conn: Annotated[Connection, Depends(get_db_connection)],
|
||||
current_user: Annotated[User, Depends(get_current_user)]
|
||||
):
|
||||
if not check_group_author(conn, groupname, current_user.username) and current_user.role not in settings.settings.admin_roles:
|
||||
if not await check_group_author(conn, groupname, current_user.username) and current_user.role not in settings.settings.admin_roles:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Not allowed",
|
||||
)
|
||||
if db.check_account_existence(conn, groupname, platform.lower()):
|
||||
if await db.check_account_existence(conn, groupname, platform.lower()):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT,
|
||||
detail="Account already exists",
|
||||
)
|
||||
hashed_password = encode_str(password)
|
||||
return db.create_account(conn, groupname, current_user.username, platform.lower(), login, hashed_password, metadata)
|
||||
return await db.create_account(conn, groupname, current_user.username, platform.lower(), login, hashed_password, metadata)
|
||||
|
||||
|
||||
@accounts_router.post("/update")
|
||||
@@ -81,10 +81,10 @@ async def update_account(
|
||||
login: str,
|
||||
password: str,
|
||||
metadata: dict,
|
||||
conn: Annotated[connection, Depends(get_db_connection)],
|
||||
conn: Annotated[Connection, Depends(get_db_connection)],
|
||||
current_user: Annotated[User, Depends(get_current_user)]
|
||||
):
|
||||
account_data = db.get_accounts_by_group_platform(conn, groupname, platform.lower())
|
||||
account_data = await db.get_accounts_by_group_platform(conn, groupname, platform.lower())
|
||||
if account_data is None:
|
||||
return HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
@@ -97,4 +97,4 @@ async def update_account(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Not allowed",
|
||||
)
|
||||
return db.update_account(conn, groupname, author, platform.lower(), login, password, metadata)
|
||||
return await db.update_account(conn, groupname, author, platform.lower(), login, password, metadata)
|
||||
|
||||
+7
-7
@@ -1,7 +1,7 @@
|
||||
from typing import Annotated
|
||||
|
||||
from asyncpg import Connection
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from psycopg2._psycopg import connection
|
||||
|
||||
import db.users as db
|
||||
import settings.settings as settings
|
||||
@@ -15,36 +15,36 @@ anon_router = APIRouter(prefix="/api/anon", tags=["anon"])
|
||||
async def add_admin(
|
||||
username: str,
|
||||
password: str,
|
||||
conn: Annotated[connection, Depends(get_db_connection)]
|
||||
conn: Annotated[Connection, Depends(get_db_connection)]
|
||||
):
|
||||
if not settings.settings.allow_create_admins:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Not allowed",
|
||||
)
|
||||
if db.check_user_existence(conn, username):
|
||||
if await db.check_user_existence(conn, username):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT,
|
||||
detail="User already exists",
|
||||
)
|
||||
hashed_password = get_password_hash(password)
|
||||
return db.create_user(conn, username, hashed_password, "admin")
|
||||
return await db.create_user(conn, username, hashed_password, "admin")
|
||||
|
||||
@anon_router.post("/add/user")
|
||||
async def add_user(
|
||||
username: str,
|
||||
password: str,
|
||||
conn: Annotated[connection, Depends(get_db_connection)]
|
||||
conn: Annotated[Connection, Depends(get_db_connection)]
|
||||
):
|
||||
if not settings.settings.allow_create_users:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Not allowed",
|
||||
)
|
||||
if db.check_user_existence(conn, username):
|
||||
if await db.check_user_existence(conn, username):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT,
|
||||
detail="User already exists",
|
||||
)
|
||||
hashed_password = get_password_hash(password)
|
||||
return db.create_user(conn, username, hashed_password, "user")
|
||||
return await db.create_user(conn, username, hashed_password, "user")
|
||||
|
||||
+4
-4
@@ -1,9 +1,9 @@
|
||||
from datetime import timedelta
|
||||
from typing import Annotated
|
||||
|
||||
from asyncpg import Connection
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from fastapi.security import OAuth2PasswordRequestForm
|
||||
from psycopg2._psycopg import connection
|
||||
|
||||
from api.models import Token
|
||||
from api.utils import authenticate_user, create_access_token
|
||||
@@ -17,9 +17,9 @@ auth_router = APIRouter(prefix="/api", tags=["auth"])
|
||||
@auth_router.post("/token")
|
||||
async def login(
|
||||
form_data: Annotated[OAuth2PasswordRequestForm, Depends()],
|
||||
conn: Annotated[connection, Depends(get_db_connection)]
|
||||
conn: Annotated[Connection, Depends(get_db_connection)]
|
||||
) -> Token:
|
||||
password_correct = authenticate_user(conn, form_data.username, form_data.password)
|
||||
password_correct = await authenticate_user(conn, form_data.username, form_data.password)
|
||||
if not password_correct:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
@@ -27,7 +27,7 @@ async def login(
|
||||
headers={"WWW-Authenticate": "Bearer"},
|
||||
)
|
||||
|
||||
user_disabled = check_user_disabled(conn, form_data.username)
|
||||
user_disabled = await check_user_disabled(conn, form_data.username)
|
||||
if user_disabled:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
|
||||
+12
-12
@@ -1,7 +1,7 @@
|
||||
from typing import Annotated
|
||||
|
||||
from asyncpg import Connection
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from psycopg2._psycopg import connection
|
||||
|
||||
import db.feeds as db
|
||||
import settings.settings as settings
|
||||
@@ -20,10 +20,10 @@ feeds_router = APIRouter(prefix="/api/feeds", tags=["feeds"])
|
||||
@feeds_router.post("/feed")
|
||||
async def read_feed(
|
||||
feed_id: int,
|
||||
conn: Annotated[connection, Depends(get_db_connection)],
|
||||
conn: Annotated[Connection, Depends(get_db_connection)],
|
||||
current_user: Annotated[User, Depends(get_current_user)]
|
||||
):
|
||||
feed_data = db.get_feed(conn, feed_id)
|
||||
feed_data = await db.get_feed(conn, feed_id)
|
||||
if feed_data is None:
|
||||
return HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
@@ -31,14 +31,14 @@ async def read_feed(
|
||||
)
|
||||
feed = Feed().fill(feed_data)
|
||||
|
||||
groupname = get_groupname_by_feed_id(conn, feed_id)
|
||||
groupname = await get_groupname_by_feed_id(conn, feed_id)
|
||||
if groupname is None:
|
||||
return HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="No such feed",
|
||||
)
|
||||
|
||||
if not check_membership_exists(conn, current_user.username, groupname) and current_user.role not in settings.settings.admin_roles:
|
||||
if not await check_membership_exists(conn, current_user.username, groupname) and current_user.role not in settings.settings.admin_roles:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Not allowed",
|
||||
@@ -51,19 +51,19 @@ async def read_feed(
|
||||
@feeds_router.post("/new")
|
||||
async def new_feed(
|
||||
groupname: str,
|
||||
conn: Annotated[connection, Depends(get_db_connection)],
|
||||
conn: Annotated[Connection, Depends(get_db_connection)],
|
||||
current_user: Annotated[User, Depends(get_current_user)]
|
||||
):
|
||||
if not check_group_author(conn, groupname, current_user.username) and current_user.role not in settings.settings.admin_roles:
|
||||
if not await check_group_author(conn, groupname, current_user.username) and current_user.role not in settings.settings.admin_roles:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Not allowed",
|
||||
)
|
||||
|
||||
accounts = get_accounts_by_group(conn, groupname)
|
||||
feed = generate_feed(conn, accounts)
|
||||
accounts = await get_accounts_by_group(conn, groupname)
|
||||
feed = await generate_feed(conn, accounts)
|
||||
if feed:
|
||||
return db.create_feed(conn, groupname, feed)
|
||||
return await db.create_feed(conn, groupname, feed)
|
||||
else:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_424_FAILED_DEPENDENCY,
|
||||
@@ -75,11 +75,11 @@ async def new_feed(
|
||||
@feeds_router.post("/delete")
|
||||
async def delete_feed(
|
||||
feed_id: int,
|
||||
conn: Annotated[connection, Depends(get_db_connection)],
|
||||
conn: Annotated[Connection, Depends(get_db_connection)],
|
||||
current_user: Annotated[User, Depends(get_current_user)]
|
||||
):
|
||||
if current_user.role in settings.settings.admin_roles:
|
||||
return db.delete_feed(conn, feed_id)
|
||||
return await db.delete_feed(conn, feed_id)
|
||||
else:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
|
||||
+39
-39
@@ -1,8 +1,8 @@
|
||||
import secrets
|
||||
from typing import Annotated
|
||||
|
||||
from asyncpg import Connection
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from psycopg2._psycopg import connection
|
||||
|
||||
import db.groups as db
|
||||
import settings.settings as settings
|
||||
@@ -19,7 +19,7 @@ groups_router = APIRouter(prefix="/api/groups", tags=["groups"])
|
||||
@groups_router.post("/group")
|
||||
async def read_any_group(
|
||||
groupname: str,
|
||||
conn: Annotated[connection, Depends(get_db_connection)],
|
||||
conn: Annotated[Connection, Depends(get_db_connection)],
|
||||
current_user: Annotated[User, Depends(get_current_user)]
|
||||
):
|
||||
if current_user.role not in settings.settings.admin_roles:
|
||||
@@ -27,7 +27,7 @@ async def read_any_group(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Not allowed",
|
||||
)
|
||||
group_data = db.get_group(conn, groupname)
|
||||
group_data = await db.get_group(conn, groupname)
|
||||
if group_data is None:
|
||||
return HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
@@ -38,15 +38,15 @@ async def read_any_group(
|
||||
@groups_router.post("/invite_code")
|
||||
async def read_group_invite_code(
|
||||
groupname: str,
|
||||
conn: Annotated[connection, Depends(get_db_connection)],
|
||||
conn: Annotated[Connection, Depends(get_db_connection)],
|
||||
current_user: Annotated[User, Depends(get_current_user)]
|
||||
):
|
||||
if not check_membership_exists(conn, current_user.username, groupname) and current_user.role not in settings.settings.admin_roles:
|
||||
if not await check_membership_exists(conn, current_user.username, groupname) and current_user.role not in settings.settings.admin_roles:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Not allowed",
|
||||
)
|
||||
invite_code = db.get_group_invite_code(conn, groupname)
|
||||
invite_code = await db.get_group_invite_code(conn, groupname)
|
||||
if invite_code is None:
|
||||
return HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
@@ -57,20 +57,20 @@ async def read_group_invite_code(
|
||||
@groups_router.post("/last_feed")
|
||||
async def read_group_last_feed_id(
|
||||
groupname: str,
|
||||
conn: Annotated[connection, Depends(get_db_connection)],
|
||||
conn: Annotated[Connection, Depends(get_db_connection)],
|
||||
current_user: Annotated[User, Depends(get_current_user)]
|
||||
):
|
||||
if not check_membership_exists(conn, current_user.username, groupname) and current_user.role not in settings.settings.admin_roles:
|
||||
if not await check_membership_exists(conn, current_user.username, groupname) and current_user.role not in settings.settings.admin_roles:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Not allowed",
|
||||
)
|
||||
return db.get_group_last_feed_id(conn, groupname)
|
||||
return await db.get_group_last_feed_id(conn, groupname)
|
||||
|
||||
|
||||
@groups_router.post("/add")
|
||||
async def add_group(
|
||||
conn: Annotated[connection, Depends(get_db_connection)],
|
||||
conn: Annotated[Connection, Depends(get_db_connection)],
|
||||
current_user: Annotated[User, Depends(get_current_user)],
|
||||
groupname: str,
|
||||
allow_skips: bool = True,
|
||||
@@ -82,30 +82,30 @@ async def add_group(
|
||||
detail="Not allowed",
|
||||
)
|
||||
|
||||
if db.check_group_existence(conn, groupname):
|
||||
if await db.check_group_existence(conn, groupname):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT,
|
||||
detail="Group already exists",
|
||||
)
|
||||
|
||||
invite_code = "".join(secrets.choice(JOIN_CODE_SYMBOLS) for _ in range(startup_settings.join_code_length))
|
||||
while db.check_invite_code(conn, invite_code):
|
||||
while await db.check_invite_code(conn, invite_code):
|
||||
invite_code = "".join(secrets.choice(JOIN_CODE_SYMBOLS) for _ in range(startup_settings.join_code_length))
|
||||
return {
|
||||
"result": db.create_group(conn, groupname, invite_code, current_user.username, allow_skips, feed_interval_minutes),
|
||||
"result": await db.create_group(conn, groupname, invite_code, current_user.username, allow_skips, feed_interval_minutes),
|
||||
"invite code": invite_code
|
||||
}
|
||||
|
||||
@groups_router.post("/delete")
|
||||
async def delete_group(
|
||||
groupname: str,
|
||||
conn: Annotated[connection, Depends(get_db_connection)],
|
||||
conn: Annotated[Connection, Depends(get_db_connection)],
|
||||
current_user: Annotated[User, Depends(get_current_user)]
|
||||
):
|
||||
if current_user.role in settings.settings.admin_roles:
|
||||
return db.delete_group(conn, groupname)
|
||||
if db.check_group_author(conn, groupname, current_user.username):
|
||||
return db.delete_group(conn, groupname)
|
||||
return await db.delete_group(conn, groupname)
|
||||
if await db.check_group_author(conn, groupname, current_user.username):
|
||||
return await db.delete_group(conn, groupname)
|
||||
else:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
@@ -117,19 +117,19 @@ async def delete_group(
|
||||
async def update_groupname(
|
||||
groupname: str,
|
||||
new_groupname: str,
|
||||
conn: Annotated[connection, Depends(get_db_connection)],
|
||||
conn: Annotated[Connection, Depends(get_db_connection)],
|
||||
current_user: Annotated[User, Depends(get_current_user)]
|
||||
):
|
||||
if db.check_group_existence(conn, new_groupname):
|
||||
if await db.check_group_existence(conn, new_groupname):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT,
|
||||
detail="Groupname is already taken",
|
||||
)
|
||||
|
||||
if current_user.role in settings.settings.admin_roles:
|
||||
return db.update_group_groupname(conn, groupname, new_groupname)
|
||||
if db.check_group_author(conn, groupname, current_user.username):
|
||||
return db.update_group_groupname(conn, groupname, new_groupname)
|
||||
return await db.update_group_groupname(conn, groupname, new_groupname)
|
||||
if await db.check_group_author(conn, groupname, current_user.username):
|
||||
return await db.update_group_groupname(conn, groupname, new_groupname)
|
||||
else:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
@@ -140,13 +140,13 @@ async def update_groupname(
|
||||
async def update_author(
|
||||
groupname: str,
|
||||
new_author: str,
|
||||
conn: Annotated[connection, Depends(get_db_connection)],
|
||||
conn: Annotated[Connection, Depends(get_db_connection)],
|
||||
current_user: Annotated[User, Depends(get_current_user)]
|
||||
):
|
||||
if current_user.role in settings.settings.admin_roles:
|
||||
return db.update_group_author(conn, groupname, new_author)
|
||||
if db.check_group_author(conn, groupname, current_user.username):
|
||||
return db.update_group_author(conn, groupname, new_author)
|
||||
return await db.update_group_author(conn, groupname, new_author)
|
||||
if await db.check_group_author(conn, groupname, current_user.username):
|
||||
return await db.update_group_author(conn, groupname, new_author)
|
||||
else:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
@@ -156,21 +156,21 @@ async def update_author(
|
||||
@groups_router.get("/update/invite_code")
|
||||
async def update_invite_code(
|
||||
groupname: str,
|
||||
conn: Annotated[connection, Depends(get_db_connection)],
|
||||
conn: Annotated[Connection, Depends(get_db_connection)],
|
||||
current_user: Annotated[User, Depends(get_current_user)]
|
||||
):
|
||||
invite_code = "".join(secrets.choice(JOIN_CODE_SYMBOLS) for _ in range(startup_settings.join_code_length))
|
||||
while db.check_invite_code(conn, invite_code):
|
||||
while await db.check_invite_code(conn, invite_code):
|
||||
invite_code = "".join(secrets.choice(JOIN_CODE_SYMBOLS) for _ in range(startup_settings.join_code_length))
|
||||
|
||||
if current_user.role in settings.settings.admin_roles:
|
||||
return {
|
||||
"result": db.update_group_invite_code(conn, groupname, invite_code),
|
||||
"result": await db.update_group_invite_code(conn, groupname, invite_code),
|
||||
"invite code": invite_code
|
||||
}
|
||||
if db.check_group_author(conn, groupname, current_user.username):
|
||||
if await db.check_group_author(conn, groupname, current_user.username):
|
||||
return {
|
||||
"result": db.update_group_invite_code(conn, groupname, invite_code),
|
||||
"result": await db.update_group_invite_code(conn, groupname, invite_code),
|
||||
"invite code": invite_code
|
||||
}
|
||||
else:
|
||||
@@ -184,13 +184,13 @@ async def update_invite_code(
|
||||
async def update_allow_skips(
|
||||
groupname: str,
|
||||
allow_skips: bool,
|
||||
conn: Annotated[connection, Depends(get_db_connection)],
|
||||
conn: Annotated[Connection, Depends(get_db_connection)],
|
||||
current_user: Annotated[User, Depends(get_current_user)]
|
||||
):
|
||||
if current_user.role in settings.settings.admin_roles:
|
||||
return db.update_group_allow_skips(conn, groupname, allow_skips)
|
||||
if db.check_group_author(conn, groupname, current_user.username):
|
||||
return db.update_group_allow_skips(conn, groupname, allow_skips)
|
||||
return await db.update_group_allow_skips(conn, groupname, allow_skips)
|
||||
if await db.check_group_author(conn, groupname, current_user.username):
|
||||
return await db.update_group_allow_skips(conn, groupname, allow_skips)
|
||||
else:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
@@ -202,13 +202,13 @@ async def update_allow_skips(
|
||||
async def update_feed_interval(
|
||||
groupname: str,
|
||||
feed_interval: int,
|
||||
conn: Annotated[connection, Depends(get_db_connection)],
|
||||
conn: Annotated[Connection, Depends(get_db_connection)],
|
||||
current_user: Annotated[User, Depends(get_current_user)]
|
||||
):
|
||||
if current_user.role in settings.settings.admin_roles:
|
||||
return db.update_group_feed_interval(conn, groupname, feed_interval)
|
||||
if db.check_group_author(conn, groupname, current_user.username):
|
||||
return db.update_group_feed_interval(conn, groupname, feed_interval)
|
||||
return await db.update_group_feed_interval(conn, groupname, feed_interval)
|
||||
if await db.check_group_author(conn, groupname, current_user.username):
|
||||
return await db.update_group_feed_interval(conn, groupname, feed_interval)
|
||||
else:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
|
||||
+19
-19
@@ -1,7 +1,7 @@
|
||||
from typing import Annotated
|
||||
|
||||
from asyncpg import Connection
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from psycopg2._psycopg import connection
|
||||
|
||||
import db.memberships as db
|
||||
import settings.settings as settings
|
||||
@@ -20,18 +20,18 @@ memberships_router = APIRouter(prefix="/api/membership", tags=["memberships"])
|
||||
|
||||
@memberships_router.get("/me")
|
||||
async def read_users_groups(
|
||||
conn: Annotated[connection, Depends(get_db_connection)],
|
||||
conn: Annotated[Connection, Depends(get_db_connection)],
|
||||
current_user: Annotated[User, Depends(get_current_user)]
|
||||
):
|
||||
return db.get_memberships_by_username(conn, current_user.username)
|
||||
return await db.get_memberships_by_username(conn, current_user.username)
|
||||
|
||||
@memberships_router.post("/user")
|
||||
async def read_users_any_memberships(
|
||||
username: str,
|
||||
conn: Annotated[connection, Depends(get_db_connection)],
|
||||
conn: Annotated[Connection, Depends(get_db_connection)],
|
||||
current_user: Annotated[User, Depends(get_current_user)]
|
||||
):
|
||||
if not check_user_existence(conn, username):
|
||||
if not await check_user_existence(conn, username):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="User does not exist",
|
||||
@@ -43,35 +43,35 @@ async def read_users_any_memberships(
|
||||
detail="Not allowed",
|
||||
)
|
||||
|
||||
return db.get_memberships_by_username(conn, username)
|
||||
return await db.get_memberships_by_username(conn, username)
|
||||
|
||||
@memberships_router.post("/group")
|
||||
async def read_any_group_members(
|
||||
groupname: str,
|
||||
conn: Annotated[connection, Depends(get_db_connection)],
|
||||
conn: Annotated[Connection, Depends(get_db_connection)],
|
||||
current_user: Annotated[User, Depends(get_current_user)]
|
||||
):
|
||||
user_is_in_group = db.check_membership_exists(conn, current_user.username, groupname)
|
||||
user_is_in_group = await db.check_membership_exists(conn, current_user.username, groupname)
|
||||
if not user_is_in_group and current_user.role not in settings.settings.admin_roles:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Not allowed",
|
||||
)
|
||||
|
||||
if not check_group_existence(conn, groupname):
|
||||
if not await check_group_existence(conn, groupname):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="No such group",
|
||||
)
|
||||
|
||||
return db.get_memberships_by_groupname(conn, groupname)
|
||||
return await db.get_memberships_by_groupname(conn, groupname)
|
||||
|
||||
|
||||
@memberships_router.post("/add")
|
||||
async def add_membership(
|
||||
username: str,
|
||||
invite_code: str,
|
||||
conn: Annotated[connection, Depends(get_db_connection)],
|
||||
conn: Annotated[Connection, Depends(get_db_connection)],
|
||||
current_user: Annotated[User, Depends(get_current_user)]
|
||||
):
|
||||
if username != current_user.username and current_user.role not in settings.settings.admin_roles:
|
||||
@@ -80,39 +80,39 @@ async def add_membership(
|
||||
detail="Not allowed",
|
||||
)
|
||||
|
||||
groupname = get_groupname_by_invite_code(conn, invite_code)
|
||||
groupname = await get_groupname_by_invite_code(conn, invite_code)
|
||||
if groupname is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Invite code is incorrect",
|
||||
)
|
||||
|
||||
if not check_user_existence(conn, username):
|
||||
if not await check_user_existence(conn, username):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="User does not exist",
|
||||
)
|
||||
|
||||
if db.check_membership_exists(conn, username, groupname):
|
||||
if await db.check_membership_exists(conn, username, groupname):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT,
|
||||
detail="User is already a member",
|
||||
)
|
||||
|
||||
return db.create_membership(conn, username, groupname)
|
||||
return await db.create_membership(conn, username, groupname)
|
||||
|
||||
@memberships_router.post("/delete")
|
||||
async def delete_membership(
|
||||
username: str,
|
||||
groupname: str,
|
||||
conn: Annotated[connection, Depends(get_db_connection)],
|
||||
conn: Annotated[Connection, Depends(get_db_connection)],
|
||||
current_user: Annotated[User, Depends(get_current_user)]
|
||||
):
|
||||
if current_user.role in settings.settings.admin_roles:
|
||||
return db.delete_membership(conn, username, groupname)
|
||||
return await db.delete_membership(conn, username, groupname)
|
||||
|
||||
if check_group_author(conn, groupname, current_user.username):
|
||||
return db.delete_membership(conn, username, groupname)
|
||||
if await check_group_author(conn, groupname, current_user.username):
|
||||
return await db.delete_membership(conn, username, groupname)
|
||||
else:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
|
||||
+7
-7
@@ -1,7 +1,7 @@
|
||||
from typing import Annotated
|
||||
|
||||
from asyncpg import Connection
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from psycopg2._psycopg import connection
|
||||
|
||||
import db.pictures as db
|
||||
import settings.settings as settings
|
||||
@@ -15,10 +15,10 @@ pictures_router = APIRouter(prefix="/api/pictures", tags=["pictures"])
|
||||
@pictures_router.post("/picture")
|
||||
async def read_picture(
|
||||
id: int,
|
||||
conn: Annotated[connection, Depends(get_db_connection)],
|
||||
conn: Annotated[Connection, Depends(get_db_connection)],
|
||||
current_user: Annotated[User, Depends(get_current_user)]
|
||||
):
|
||||
picture_data = db.get_picture(conn, id)
|
||||
picture_data = await db.get_picture(conn, id)
|
||||
if picture_data is None:
|
||||
return HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
@@ -29,7 +29,7 @@ async def read_picture(
|
||||
|
||||
@pictures_router.post("/add")
|
||||
async def add_picture(
|
||||
conn: Annotated[connection, Depends(get_db_connection)],
|
||||
conn: Annotated[Connection, Depends(get_db_connection)],
|
||||
current_user: Annotated[User, Depends(get_current_user)],
|
||||
source: str,
|
||||
external_id: str,
|
||||
@@ -43,18 +43,18 @@ async def add_picture(
|
||||
)
|
||||
|
||||
return {
|
||||
"id": db.create_picture(conn, source, external_id, url, metadata)
|
||||
"id": await db.create_picture(conn, source, external_id, url, metadata)
|
||||
}
|
||||
|
||||
|
||||
@pictures_router.post("/delete")
|
||||
async def delete_picture(
|
||||
picture_id: int,
|
||||
conn: Annotated[connection, Depends(get_db_connection)],
|
||||
conn: Annotated[Connection, Depends(get_db_connection)],
|
||||
current_user: Annotated[User, Depends(get_current_user)]
|
||||
):
|
||||
if current_user.role in settings.settings.admin_roles:
|
||||
return db.delete_picture(conn, picture_id)
|
||||
return await db.delete_picture(conn, picture_id)
|
||||
else:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
|
||||
+19
-19
@@ -1,7 +1,7 @@
|
||||
from typing import Annotated
|
||||
|
||||
from asyncpg import Connection
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from psycopg2._psycopg import connection
|
||||
|
||||
import db.swipes as db
|
||||
import settings.settings as settings
|
||||
@@ -23,22 +23,22 @@ async def read_swipe(
|
||||
username: str,
|
||||
feed_id: int,
|
||||
picture_id: int,
|
||||
conn: Annotated[connection, Depends(get_db_connection)],
|
||||
conn: Annotated[Connection, Depends(get_db_connection)],
|
||||
current_user: Annotated[User, Depends(get_current_user)]
|
||||
):
|
||||
groupname = get_groupname_by_feed_id(conn, feed_id)
|
||||
groupname = await get_groupname_by_feed_id(conn, feed_id)
|
||||
if groupname is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="No such feed or feed is not linked to group",
|
||||
)
|
||||
if not check_membership_exists(conn, current_user.username, groupname) and current_user.role not in settings.settings.admin_roles:
|
||||
if not await check_membership_exists(conn, current_user.username, groupname) and current_user.role not in settings.settings.admin_roles:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Not allowed",
|
||||
)
|
||||
|
||||
swipe_data = db.get_swipe(conn, username, feed_id, picture_id)
|
||||
swipe_data = await db.get_swipe(conn, username, feed_id, picture_id)
|
||||
if swipe_data is None:
|
||||
return HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
@@ -51,42 +51,42 @@ async def read_swipe(
|
||||
async def read_swipes_by_picture_id(
|
||||
feed_id: int,
|
||||
picture_id: int,
|
||||
conn: Annotated[connection, Depends(get_db_connection)],
|
||||
conn: Annotated[Connection, Depends(get_db_connection)],
|
||||
current_user: Annotated[User, Depends(get_current_user)]
|
||||
):
|
||||
groupname = get_groupname_by_feed_id(conn, feed_id)
|
||||
groupname = await get_groupname_by_feed_id(conn, feed_id)
|
||||
if groupname is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="No such feed or feed is not linked to group",
|
||||
)
|
||||
if not check_membership_exists(conn, current_user.username, groupname) and current_user.role not in settings.settings.admin_roles:
|
||||
if not await check_membership_exists(conn, current_user.username, groupname) and current_user.role not in settings.settings.admin_roles:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Not allowed",
|
||||
)
|
||||
return db.get_swipes_by_picture_id(conn, picture_id, feed_id)
|
||||
return await db.get_swipes_by_picture_id(conn, picture_id, feed_id)
|
||||
|
||||
|
||||
@swipes_router.post("/swipe/user")
|
||||
async def read_user_swipes(
|
||||
username: str,
|
||||
feed_id: int,
|
||||
conn: Annotated[connection, Depends(get_db_connection)],
|
||||
conn: Annotated[Connection, Depends(get_db_connection)],
|
||||
current_user: Annotated[User, Depends(get_current_user)]
|
||||
):
|
||||
groupname = get_groupname_by_feed_id(conn, feed_id)
|
||||
groupname = await get_groupname_by_feed_id(conn, feed_id)
|
||||
if groupname is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="No such feed or feed is not linked to group",
|
||||
)
|
||||
if not check_membership_exists(conn, current_user.username, groupname) and current_user.role not in settings.settings.admin_roles:
|
||||
if not await check_membership_exists(conn, current_user.username, groupname) and current_user.role not in settings.settings.admin_roles:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Not allowed",
|
||||
)
|
||||
return db.get_swipes_by_user(conn, username, feed_id)
|
||||
return await db.get_swipes_by_user(conn, username, feed_id)
|
||||
|
||||
|
||||
@swipes_router.post("/add")
|
||||
@@ -94,17 +94,17 @@ async def add_swipe(
|
||||
feed_id: int,
|
||||
picture_id: int,
|
||||
value: int,
|
||||
conn: Annotated[connection, Depends(get_db_connection)],
|
||||
conn: Annotated[Connection, Depends(get_db_connection)],
|
||||
current_user: Annotated[User, Depends(get_current_user)]
|
||||
):
|
||||
groupname = get_groupname_by_feed_id(conn, feed_id)
|
||||
groupname = await get_groupname_by_feed_id(conn, feed_id)
|
||||
if groupname is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="No such feed or feed is not linked to group",
|
||||
)
|
||||
|
||||
group_data = get_group(conn, groupname)
|
||||
group_data = await get_group(conn, groupname)
|
||||
if group_data is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
@@ -120,7 +120,7 @@ async def add_swipe(
|
||||
detail="Skips are disallowed in the group",
|
||||
)
|
||||
|
||||
result = db.create_swipe(conn, current_user.username, feed_id, picture_id, value)
|
||||
result = await db.create_swipe(conn, current_user.username, feed_id, picture_id, value)
|
||||
if result:
|
||||
# TODO: call function to like picture on the platform
|
||||
pass
|
||||
@@ -132,11 +132,11 @@ async def delete_swipe(
|
||||
username: str,
|
||||
feed_id: int,
|
||||
picture_id: int,
|
||||
conn: Annotated[connection, Depends(get_db_connection)],
|
||||
conn: Annotated[Connection, Depends(get_db_connection)],
|
||||
current_user: Annotated[User, Depends(get_current_user)]
|
||||
):
|
||||
if current_user.role in settings.settings.admin_roles:
|
||||
return db.delete_swipe(conn, username, feed_id, picture_id)
|
||||
return await db.delete_swipe(conn, username, feed_id, picture_id)
|
||||
else:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
|
||||
+22
-22
@@ -1,7 +1,7 @@
|
||||
from typing import Annotated
|
||||
|
||||
from asyncpg import Connection
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from psycopg2._psycopg import connection
|
||||
|
||||
import db.users as db
|
||||
import settings.settings as settings
|
||||
@@ -19,7 +19,7 @@ async def read_users_me(current_user: Annotated[User, Depends(get_current_user)]
|
||||
@users_router.post("/user")
|
||||
async def read_users_any(
|
||||
username: str,
|
||||
conn: Annotated[connection, Depends(get_db_connection)],
|
||||
conn: Annotated[Connection, Depends(get_db_connection)],
|
||||
current_user: Annotated[User, Depends(get_current_user)]
|
||||
):
|
||||
if current_user.role not in settings.settings.admin_roles:
|
||||
@@ -27,7 +27,7 @@ async def read_users_any(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Not allowed",
|
||||
)
|
||||
user_data = db.get_user(conn, username)
|
||||
user_data = await db.get_user(conn, username)
|
||||
if user_data is None:
|
||||
return HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
@@ -41,7 +41,7 @@ async def read_users_any(
|
||||
async def add_admin(
|
||||
username: str,
|
||||
password: str,
|
||||
conn: Annotated[connection, Depends(get_db_connection)],
|
||||
conn: Annotated[Connection, Depends(get_db_connection)],
|
||||
current_user: Annotated[User, Depends(get_current_user)]
|
||||
):
|
||||
if not settings.settings.allow_create_admins_by_admins:
|
||||
@@ -54,19 +54,19 @@ async def add_admin(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Not allowed",
|
||||
)
|
||||
if db.check_user_existence(conn, username):
|
||||
if await db.check_user_existence(conn, username):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT,
|
||||
detail="User already exists",
|
||||
)
|
||||
hashed_password = get_password_hash(password)
|
||||
return db.create_user(conn, username, hashed_password, "admin")
|
||||
return await db.create_user(conn, username, hashed_password, "admin")
|
||||
|
||||
@users_router.post("/add/user")
|
||||
async def add_user(
|
||||
username: str,
|
||||
password: str,
|
||||
conn: Annotated[connection, Depends(get_db_connection)],
|
||||
conn: Annotated[Connection, Depends(get_db_connection)],
|
||||
current_user: Annotated[User, Depends(get_current_user)]
|
||||
):
|
||||
if not settings.settings.allow_create_users and current_user.role not in settings.settings.admin_roles:
|
||||
@@ -74,23 +74,23 @@ async def add_user(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Not allowed",
|
||||
)
|
||||
if db.check_user_existence(conn, username):
|
||||
if await db.check_user_existence(conn, username):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT,
|
||||
detail="User already exists",
|
||||
)
|
||||
hashed_password = get_password_hash(password)
|
||||
return db.create_user(conn, username, hashed_password, "user")
|
||||
return await db.create_user(conn, username, hashed_password, "user")
|
||||
|
||||
|
||||
@users_router.post("/delete")
|
||||
async def delete_user(
|
||||
username: str,
|
||||
conn: Annotated[connection, Depends(get_db_connection)],
|
||||
conn: Annotated[Connection, Depends(get_db_connection)],
|
||||
current_user: Annotated[User, Depends(get_current_user)]
|
||||
):
|
||||
if current_user.username == username or current_user.role in settings.settings.admin_roles:
|
||||
return db.delete_user(conn, username)
|
||||
return await db.delete_user(conn, username)
|
||||
else:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
@@ -102,11 +102,11 @@ async def delete_user(
|
||||
async def update_disabled(
|
||||
username: str,
|
||||
disabled: bool,
|
||||
conn: Annotated[connection, Depends(get_db_connection)],
|
||||
conn: Annotated[Connection, Depends(get_db_connection)],
|
||||
current_user: Annotated[User, Depends(get_current_user)]
|
||||
):
|
||||
if current_user.role in settings.settings.admin_roles:
|
||||
return db.update_user_disabled(conn, username, disabled)
|
||||
return await db.update_user_disabled(conn, username, disabled)
|
||||
else:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
@@ -117,11 +117,11 @@ async def update_disabled(
|
||||
async def update_role(
|
||||
username: str,
|
||||
role: str,
|
||||
conn: Annotated[connection, Depends(get_db_connection)],
|
||||
conn: Annotated[Connection, Depends(get_db_connection)],
|
||||
current_user: Annotated[User, Depends(get_current_user)]
|
||||
):
|
||||
if current_user.role in settings.settings.admin_roles:
|
||||
return db.update_user_role(conn, username, role)
|
||||
return await db.update_user_role(conn, username, role)
|
||||
else:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
@@ -133,16 +133,16 @@ async def update_role(
|
||||
async def update_username(
|
||||
username: str,
|
||||
new_username: str,
|
||||
conn: Annotated[connection, Depends(get_db_connection)],
|
||||
conn: Annotated[Connection, Depends(get_db_connection)],
|
||||
current_user: Annotated[User, Depends(get_current_user)]
|
||||
):
|
||||
if db.check_user_existence(conn, new_username):
|
||||
if await db.check_user_existence(conn, new_username):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT,
|
||||
detail="Username is already taken",
|
||||
)
|
||||
if current_user.username == username or current_user.role in settings.settings.admin_roles:
|
||||
return db.update_user_username(conn, username, new_username)
|
||||
return await db.update_user_username(conn, username, new_username)
|
||||
else:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
@@ -153,12 +153,12 @@ async def update_username(
|
||||
async def update_password(
|
||||
username: str,
|
||||
password: str,
|
||||
conn: Annotated[connection, Depends(get_db_connection)],
|
||||
conn: Annotated[Connection, Depends(get_db_connection)],
|
||||
current_user: Annotated[User, Depends(get_current_user)]
|
||||
):
|
||||
if current_user.username == username or current_user.role in settings.settings.admin_roles:
|
||||
hashed_password = get_password_hash(password)
|
||||
return db.update_user_password(conn, username, hashed_password)
|
||||
return await db.update_user_password(conn, username, hashed_password)
|
||||
else:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
@@ -168,7 +168,7 @@ async def update_password(
|
||||
|
||||
@users_router.get("/update/last_seen")
|
||||
async def update_last_seen(
|
||||
conn: Annotated[connection, Depends(get_db_connection)],
|
||||
conn: Annotated[Connection, Depends(get_db_connection)],
|
||||
current_user: Annotated[User, Depends(get_current_user)]
|
||||
):
|
||||
return db.update_user_last_seen(conn, current_user.username)
|
||||
return await db.update_user_last_seen(conn, current_user.username)
|
||||
|
||||
+9
-9
@@ -1,12 +1,12 @@
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from typing import Annotated
|
||||
|
||||
from asyncpg import Connection
|
||||
import bcrypt
|
||||
import jwt
|
||||
from fastapi import Depends, HTTPException, status
|
||||
from fastapi.security import OAuth2PasswordBearer
|
||||
from jwt.exceptions import InvalidTokenError
|
||||
from psycopg2._psycopg import connection
|
||||
|
||||
import db.groups
|
||||
import db.users
|
||||
@@ -30,15 +30,15 @@ def encode_token(payload):
|
||||
return jwt.encode(payload, startup_settings.secret_key, algorithm=startup_settings.algorithm)
|
||||
|
||||
|
||||
def authenticate_user(
|
||||
conn: connection,
|
||||
async def authenticate_user(
|
||||
conn: Connection,
|
||||
username: str,
|
||||
user_password: str
|
||||
):
|
||||
if not user_password:
|
||||
return False
|
||||
|
||||
db_user_password = db.users.get_user_password(conn, username)
|
||||
db_user_password = await db.users.get_user_password(conn, username)
|
||||
if db_user_password is None:
|
||||
return False
|
||||
|
||||
@@ -60,7 +60,7 @@ def create_access_token(
|
||||
|
||||
async def get_current_user(
|
||||
token: Annotated[str, Depends(oauth2_scheme)],
|
||||
conn: Annotated[connection, Depends(get_db_connection)]
|
||||
conn: Annotated[Connection, Depends(get_db_connection)]
|
||||
):
|
||||
credentials_exception = HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
@@ -77,7 +77,7 @@ async def get_current_user(
|
||||
except InvalidTokenError:
|
||||
raise credentials_exception
|
||||
|
||||
user_data = db.users.get_user(conn, token_data.username)
|
||||
user_data = await db.users.get_user(conn, token_data.username)
|
||||
if user_data is None:
|
||||
raise credentials_exception
|
||||
|
||||
@@ -92,15 +92,15 @@ async def get_current_user(
|
||||
return user
|
||||
|
||||
|
||||
def get_group_by_name(
|
||||
conn: connection,
|
||||
async def get_group_by_name(
|
||||
conn: Connection,
|
||||
groupname: str
|
||||
):
|
||||
group_exception = HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="No such group"
|
||||
)
|
||||
group_data = db.groups.get_group(conn, groupname)
|
||||
group_data = await db.groups.get_group(conn, groupname)
|
||||
if group_data is None:
|
||||
raise group_exception
|
||||
|
||||
|
||||
Reference in New Issue
Block a user