feed WIP
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
from typing import Annotated
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from psycopg2._psycopg import connection
|
||||
|
||||
import db.feeds as db
|
||||
import settings.settings as settings
|
||||
from api.models import Feed, Group, User
|
||||
from api.utils import get_current_user
|
||||
from db.feeds import get_groupname_by_feed_id
|
||||
from db.groups import get_group
|
||||
from db.internal import get_db_connection
|
||||
from db.memberships import check_membership_exists
|
||||
|
||||
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)],
|
||||
current_user: Annotated[User, Depends(get_current_user)]
|
||||
):
|
||||
feed = Feed()
|
||||
feed_data = db.get_feed(conn, feed_id)
|
||||
if feed_data is None:
|
||||
return HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="No such feed",
|
||||
)
|
||||
feed.fill(feed_data)
|
||||
|
||||
if not 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 feed
|
||||
|
||||
|
||||
@feeds_router.post("/add")
|
||||
async def add_feed(
|
||||
feed_id: int,
|
||||
picture_id: int,
|
||||
value: int,
|
||||
conn: Annotated[connection, Depends(get_db_connection)],
|
||||
current_user: Annotated[User, Depends(get_current_user)]
|
||||
):
|
||||
groupname = 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 = Group()
|
||||
group_data = get_group(conn, groupname)
|
||||
if group_data is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="No such feed or feed is not linked to group",
|
||||
)
|
||||
group.fill(group_data)
|
||||
|
||||
if value == 0 and group.allow_skips is False:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Skips are disallowed",
|
||||
)
|
||||
return db.create_feed(conn, current_user.username, feed_id, picture_id, value)
|
||||
|
||||
|
||||
@feeds_router.post("/delete")
|
||||
async def delete_feed(
|
||||
username: str,
|
||||
feed_id: int,
|
||||
picture_id: int,
|
||||
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, username, feed_id, picture_id)
|
||||
else:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Not allowed",
|
||||
)
|
||||
|
||||
+2
-1
@@ -5,6 +5,7 @@ from fastapi import APIRouter, Depends, HTTPException, status
|
||||
import settings.settings as settings
|
||||
from api.models import User
|
||||
from api.utils import get_current_user
|
||||
from settings.consts import API_EDITABLE_SETTINGS_LIST
|
||||
from settings.settings import load_settings, reset_settings, save_settings
|
||||
|
||||
general_router = APIRouter(prefix="/api", tags=["general"])
|
||||
@@ -25,7 +26,7 @@ async def get_settings(current_user: Annotated[User, Depends(get_current_user)])
|
||||
return settings.settings
|
||||
|
||||
|
||||
@general_router.post("/settings/update")
|
||||
@general_router.post("/settings/update", description=API_EDITABLE_SETTINGS_LIST)
|
||||
async def update_settings(data: dict, current_user: Annotated[User, Depends(get_current_user)]):
|
||||
if current_user.role not in settings.settings.admin_roles:
|
||||
raise HTTPException(
|
||||
|
||||
@@ -56,6 +56,19 @@ async def read_group_invite_code(
|
||||
)
|
||||
return invite_code
|
||||
|
||||
@groups_router.post("/last_feed")
|
||||
async def read_group_last_feed_id(
|
||||
groupname: str,
|
||||
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:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Not allowed",
|
||||
)
|
||||
return db.get_group_last_feed_id(conn, groupname)
|
||||
|
||||
|
||||
@groups_router.post("/add")
|
||||
async def add_group(
|
||||
|
||||
@@ -72,3 +72,29 @@ class Picture(BaseModel):
|
||||
url: str = ""
|
||||
metadata: dict | None = None
|
||||
created_at: datetime | None = None
|
||||
|
||||
|
||||
class Swipe(BaseModel):
|
||||
def fill(self, params):
|
||||
self.username = params["username"]
|
||||
self.feed_id = params["feed_id"]
|
||||
self.picture_id = params["picture_id"]
|
||||
self.value = params["value"]
|
||||
self.created_at = params["created_at"]
|
||||
username: str = ""
|
||||
feed_id: int = -1
|
||||
picture_id: int = -1
|
||||
value: int = 0
|
||||
created_at: datetime | None = None
|
||||
|
||||
|
||||
class Feed(BaseModel):
|
||||
def fill(self, params):
|
||||
self.id = params["id"]
|
||||
self.groupname = params["groupname"]
|
||||
self.image_ids = params["image_ids"]
|
||||
self.created_at = params["created_at"]
|
||||
id: int = -1
|
||||
groupname: str = ""
|
||||
image_ids: list[int] = []
|
||||
created_at: datetime | None = None
|
||||
|
||||
@@ -62,12 +62,6 @@ async def add_picture(
|
||||
detail="Not allowed",
|
||||
)
|
||||
|
||||
# if db.check_picture_existence(conn, groupname):
|
||||
# raise HTTPException(
|
||||
# status_code=status.HTTP_409_CONFLICT,
|
||||
# detail="Picture already exists",
|
||||
# )
|
||||
|
||||
return {
|
||||
"id": db.create_picture(conn, source, external_id, url, metadata)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,147 @@
|
||||
from typing import Annotated
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from psycopg2._psycopg import connection
|
||||
|
||||
import db.swipes as db
|
||||
import settings.settings as settings
|
||||
from api.models import Group, Swipe, User
|
||||
from api.utils import get_current_user
|
||||
from db.feeds import get_groupname_by_feed_id
|
||||
from db.groups import get_group
|
||||
from db.internal import get_db_connection
|
||||
from db.memberships import check_membership_exists
|
||||
|
||||
swipes_router = APIRouter(prefix="/api/swipes", tags=["swipes"])
|
||||
|
||||
|
||||
# Maybe endpoints should be remade
|
||||
# to return id and then work with swipe id
|
||||
|
||||
@swipes_router.post("/swipe")
|
||||
async def read_swipe(
|
||||
username: str,
|
||||
feed_id: int,
|
||||
picture_id: int,
|
||||
conn: Annotated[connection, Depends(get_db_connection)],
|
||||
current_user: Annotated[User, Depends(get_current_user)]
|
||||
):
|
||||
groupname = 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:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Not allowed",
|
||||
)
|
||||
|
||||
swipe = Swipe()
|
||||
swipe_data = db.get_swipe(conn, username, feed_id, picture_id)
|
||||
if swipe_data is None:
|
||||
return HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="No such swipe",
|
||||
)
|
||||
swipe.fill(swipe_data)
|
||||
return swipe
|
||||
|
||||
|
||||
@swipes_router.post("/swipe/picture_id")
|
||||
async def read_swipes_by_picture_id(
|
||||
feed_id: int,
|
||||
picture_id: int,
|
||||
conn: Annotated[connection, Depends(get_db_connection)],
|
||||
current_user: Annotated[User, Depends(get_current_user)]
|
||||
):
|
||||
groupname = 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:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Not allowed",
|
||||
)
|
||||
return 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)],
|
||||
current_user: Annotated[User, Depends(get_current_user)]
|
||||
):
|
||||
groupname = 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:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Not allowed",
|
||||
)
|
||||
return db.get_swipes_by_user(conn, username, feed_id)
|
||||
|
||||
|
||||
@swipes_router.post("/add")
|
||||
async def add_swipe(
|
||||
feed_id: int,
|
||||
picture_id: int,
|
||||
value: int,
|
||||
conn: Annotated[connection, Depends(get_db_connection)],
|
||||
current_user: Annotated[User, Depends(get_current_user)]
|
||||
):
|
||||
groupname = 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 = Group()
|
||||
group_data = get_group(conn, groupname)
|
||||
if group_data is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="No such feed or feed is not linked to group",
|
||||
)
|
||||
group.fill(group_data)
|
||||
|
||||
# Check for trying to skip in
|
||||
# a group with skips disabled
|
||||
if value == 0 and group.allow_skips is False:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Skips are disallowed",
|
||||
)
|
||||
|
||||
result = db.create_swipe(conn, current_user.username, feed_id, picture_id, value)
|
||||
if result:
|
||||
# TODO: call function to like picture on the platform
|
||||
pass
|
||||
return result
|
||||
|
||||
|
||||
@swipes_router.post("/delete")
|
||||
async def delete_swipe(
|
||||
username: str,
|
||||
feed_id: int,
|
||||
picture_id: int,
|
||||
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)
|
||||
else:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Not allowed",
|
||||
)
|
||||
+1
-4
@@ -6,8 +6,6 @@ import jwt
|
||||
from fastapi import Depends, HTTPException, status
|
||||
from fastapi.security import OAuth2PasswordBearer
|
||||
from jwt.exceptions import InvalidTokenError
|
||||
|
||||
# from passlib.context import CryptContext
|
||||
from psycopg2._psycopg import connection
|
||||
|
||||
import db.groups
|
||||
@@ -16,8 +14,6 @@ import settings.startup_settings as startup_settings
|
||||
from api.models import Group, TokenData, User
|
||||
from db.internal import get_db_connection
|
||||
|
||||
# pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
||||
|
||||
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
|
||||
|
||||
|
||||
@@ -96,6 +92,7 @@ async def get_current_user(
|
||||
|
||||
return user
|
||||
|
||||
|
||||
def get_group_by_name(
|
||||
conn: connection,
|
||||
groupname: str
|
||||
|
||||
Reference in New Issue
Block a user