Skip to content

Commit

Permalink
test: tests and crud.py fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
suchencjusz committed Sep 8, 2024
1 parent 8556668 commit a4f25c6
Show file tree
Hide file tree
Showing 9 changed files with 509 additions and 47 deletions.
3 changes: 3 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[run]
omit =
tests/*
2 changes: 1 addition & 1 deletion src/filman_discord/endpoints/tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ async def stop_subcommand(ctx: lightbulb.SlashContext) -> None:
@lightbulb.implements(lightbulb.SlashSubCommand)
async def cancel_subcommand(ctx: lightbulb.SlashContext) -> None:
async with ctx.bot.d.client_session.post(
"http://filman_server:8000/users/remove_from_all_guilds", # SPRAWDZ ENDPONT
"http://filman_server:8000/users/remove_from_all_guilds", # SPRAWDZ ENDPONT
json={"id_discord": ctx.author.id},
) as resp:
if not resp.ok:
Expand Down
4 changes: 2 additions & 2 deletions src/filman_discord/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ async def send_discord_message(
except hikari.BadRequestError as e:
print(f"Error sending message: {e}")

allowed_tasks = ["send_discord"] #TODO: repair tasks endpoint here
allowed_tasks = ["send_discord"] # TODO: repair tasks endpoint here

async with bot.d.client_session.post(
"http://filman_server:8000/tasks/get",
Expand All @@ -143,7 +143,7 @@ async def send_discord_message(
if not resp.ok:
return

data = await resp.json() # you have to diverse watched from user letterbox/watched filmweb/watched etc.
data = await resp.json() # you have to diverse watched from user letterbox/watched filmweb/watched etc.

user_rate = data[0]
movie = data[1]
Expand Down
145 changes: 135 additions & 10 deletions src/filman_server/database/crud.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import logging
from datetime import datetime

import sqlalchemy.exc
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm import Session

import logging

from . import models, schemas

#
Expand Down Expand Up @@ -33,6 +33,12 @@ def get_user(
return None


def get_users(db: Session):

# link also filmweb_id from filmweb mapping
return db.query(models.User).all()


def create_user(
db: Session,
user: schemas.UserCreate,
Expand Down Expand Up @@ -266,6 +272,67 @@ def update_filmweb_series(db: Session, series: schemas.FilmWebSeries):
# FILMWEB WATCHED
#

# FILMWEB USER MAPPING


def get_filmweb_user_mapping(
db: Session,
user_id: int | None,
filmweb_id: str | None,
discord_id: int | None,
) -> models.FilmWebUserMapping | None:
user = get_user(db, user_id, filmweb_id, discord_id)

if user is None:
return None

return db.query(models.FilmWebUserMapping).filter(models.FilmWebUserMapping.user_id == user.id).first()


# sets filmweb user nickname to corelate with discord user (main user in db)
def set_filmweb_user_mapping(db: Session, mapping: schemas.FilmWebUserMappingCreate) -> models.FilmWebUserMapping | None:
user = get_user(db, mapping.user_id, None, None)

if user is None:
return None

db_mapping = db.query(models.FilmWebUserMapping).filter_by(user_id=user.id).first()

# creates
if db_mapping is None:
db_mapping = models.FilmWebUserMapping(user_id=user.id, filmweb_id=mapping.filmweb_id)
db.add(db_mapping)
# updates
else:
db_mapping.filmweb_id = mapping.filmweb_id

db.commit()
db.refresh(db_mapping)

return db_mapping


def delete_filmweb_user_mapping(
db: Session,
user_id: int | None,
discord_id: int | None,
filmweb_id: str,
) -> bool:
user = get_user(db, user_id, filmweb_id, discord_id)

if user is None:
return False

db_mapping = db.query(models.FilmWebUserMapping).filter_by(user_id=user.id).first()

if db_mapping is None:
return False

db.delete(db_mapping)
db.commit()
return True


# MOVIES


Expand All @@ -286,30 +353,88 @@ def create_filmweb_user_watched_movie(db: Session, user_watched_movie: schemas.F
db_movie_watched = (
db.query(models.FilmWebUserWatchedMovie)
.filter(
models.FilmWebUserWatchedMovie.id_filmweb == user_watched_movie.id_filmweb,
models.FilmWebUserWatchedMovie.filmweb_id == user_watched_movie.filmweb_id,
models.FilmWebUserWatchedMovie.id_media == user_watched_movie.id_media,
)
.first()
)

if db_movie_watched is not None:
return sqlalchemy.exc.IntegrityError("Movie already in user watched", None, None)

db_movie = models.FilmWebUserWatchedMovie(**user_watched_movie.model_dump())
raise IntegrityError("Movie already in user watched", None, None)

db_movie = models.FilmWebUserWatchedMovie(
id_media=user_watched_movie.id_media,
filmweb_id=user_watched_movie.filmweb_id,
date=user_watched_movie.date,
rate=user_watched_movie.rate,
comment=user_watched_movie.comment,
favorite=user_watched_movie.favorite,
)
db.add(db_movie)
db.commit()
db.refresh(db_movie)
return db_movie


def get_filmweb_user_watched_movies(db: Session, id_filmweb: str):
return db.query(models.FilmWebUserWatchedMovie).filter(models.FilmWebUserWatchedMovie.id_filmweb == id_filmweb).all()
def get_filmweb_user_watched_movie(
db: Session,
user_id: int | None,
filmweb_id: str | None,
discord_id: int | None,
id_media: int,
):
user = get_user(db, user_id, filmweb_id, discord_id)

logging.debug(f"{user.discord_id}")

if user is None:
return None

# Get the filmweb_id from the mapping if not provided
if filmweb_id is None:
filmweb_mapping = db.query(models.FilmWebUserMapping).filter(models.FilmWebUserMapping.user_id == user.id).first()
if filmweb_mapping is None:
return None
filmweb_id = filmweb_mapping.filmweb_id

return (
db.query(models.FilmWebUserWatchedMovie)
.filter(models.FilmWebUserWatchedMovie.filmweb_id == filmweb_id, models.FilmWebUserWatchedMovie.id_media == id_media)
.first()
)


def get_filmweb_user_watched_movies(
db: Session,
user_id: int | None,
filmweb_id: str | None,
discord_id: int | None,
):
# Fetch the user based on user_id, filmweb_id, or discord_id
user = get_user(db, user_id, filmweb_id, discord_id)

if user is None:
return None

# If filmweb_id is not provided, get it from the user mapping
if filmweb_id is None:
filmweb_mapping = db.query(models.FilmWebUserMapping).filter(models.FilmWebUserMapping.user_id == user.id).first()
if filmweb_mapping is None:
return None
filmweb_id = filmweb_mapping.filmweb_id

# Query the database for all watched movies for the given filmweb_id
watched_movies = (
db.query(models.FilmWebUserWatchedMovie).filter(models.FilmWebUserWatchedMovie.filmweb_id == filmweb_id).all()
)

return watched_movies


def get_filmweb_user_watched_movies_ids(db: Session, id_filmweb: str):
def get_filmweb_user_watched_movies_ids(db: Session, filmweb_id: str):
result = (
db.query(models.FilmWebUserWatchedMovie.id_media) # select only the id field
.filter(models.FilmWebUserWatchedMovie.id_filmweb == id_filmweb)
.filter(models.FilmWebUserWatchedMovie.filmweb_id == filmweb_id)
.all()
)
return [id_media for (id_media,) in result] # extract ids from tuples
Expand Down
21 changes: 11 additions & 10 deletions src/filman_server/database/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
class User(Base):
__tablename__ = "users"

id = Column(Integer, primary_key=True, index=True, autoincrement=True)
id = Column(Integer, primary_key=True, index=True, autoincrement=True, unique=True)
discord_id = Column(BIGINT, index=True, unique=True)

discord_destinations = relationship("DiscordDestinations", back_populates="user", cascade="all, delete-orphan")

filmweb_user_mapping = relationship("FilmWebUserMapping", back_populates="user", uselist=False, cascade="all, delete-orphan")

filmweb_user_mapping = relationship(
"FilmWebUserMapping", back_populates="user", uselist=False, cascade="all, delete-orphan"
)


class FilmWebUserMapping(Base):
Expand All @@ -33,7 +34,7 @@ class FilmWebUserMapping(Base):
id = Column(Integer, primary_key=True, index=True, autoincrement=True)
user_id = Column(Integer, ForeignKey("users.id"), index=True, unique=True)
filmweb_id = Column(String(128), index=True, unique=True)

user = relationship("User", back_populates="filmweb_user_mapping")


Expand Down Expand Up @@ -94,20 +95,20 @@ class __FilmwebWatched(Base):

class FilmWebUserWatchedMovie(__FilmwebWatched):
__tablename__ = "filmweb_user_watched_movies"

id_media = Column(Integer, ForeignKey("filmweb_movies.id"), primary_key=True, index=True)
id_filmweb = Column(String(128), ForeignKey("filmweb_user_mapping.filmweb_id"), primary_key=True, index=True)
filmweb_id = Column(String(128), ForeignKey("filmweb_user_mapping.filmweb_id"), primary_key=True, index=True)

movie = relationship("FilmWebMovie", backref="filmweb_user_watched_movies")
filmweb_user_mapping = relationship("FilmWebUserMapping", backref="watched_movies")


class FilmWebUserWatchedSeries(__FilmwebWatched):
__tablename__ = "filmweb_user_watched_series"

id_media = Column(Integer, ForeignKey("filmweb_series.id"), primary_key=True, index=True)
id_filmweb = Column(String(128), ForeignKey("filmweb_user_mapping.filmweb_id"), primary_key=True, index=True)
filmweb_id = Column(String(128), ForeignKey("filmweb_user_mapping.filmweb_id"), primary_key=True, index=True)

series = relationship("FilmWebSeries", backref="filmweb_user_watched_series")
filmweb_user_mapping = relationship("FilmWebUserMapping", backref="watched_series")

Expand Down
30 changes: 25 additions & 5 deletions src/filman_server/database/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,26 @@ class Config:
orm_mode = True


#
# MAPPINGS
#
class FilmWebUserMapping(BaseModel):
id: int
user_id: int
filmweb_id: str

class Config:
orm_mode = True


class FilmWebUserMappingCreate(BaseModel):
user_id: int
filmweb_id: str

class Config:
orm_mode = True


# class UserPreferencesCreate(BaseModel):
# discord_color: str

Expand Down Expand Up @@ -129,7 +149,7 @@ class Config:

class FilmWebUserWatchedMovie(BaseModel):
movie: FilmWebMovie
id_filmweb: str
filmweb_id: str

date: datetime
rate: int | None
Expand All @@ -142,7 +162,7 @@ class Config:

class FilmWebUserWatchedMovieCreate(BaseModel):
id_media: int
id_filmweb: str
filmweb_id: str

date: datetime
rate: int | None
Expand Down Expand Up @@ -182,7 +202,7 @@ class Config:

class FilmWebUserWatchedSeries(BaseModel):
series: FilmWebSeries
id_filmweb: str
filmweb_id: str

date: datetime
rate: int | None
Expand All @@ -195,7 +215,7 @@ class Config:

class FilmwebUserWatchedSeries(BaseModel):
series: FilmWebSeries
id_filmweb: str
filmweb_id: str

date: datetime
rate: int | None
Expand All @@ -208,7 +228,7 @@ class Config:

class FilmwebUserWatchedSeriesCreate(BaseModel):
id_media: int
id_filmweb: str
filmweb_id: str

date: datetime
rate: int | None
Expand Down
7 changes: 2 additions & 5 deletions src/filman_server/routes/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,9 @@ async def get_user(
# discord guild actions
#


@users_router.get("/get_all_guilds", response_model=List[schemas.DiscordDestinations])
async def get_guilds(
id: Optional[int] = None,
discord_id: Optional[int] = None,
db: Session = Depends(get_db)
):
async def get_guilds(id: Optional[int] = None, discord_id: Optional[int] = None, db: Session = Depends(get_db)):
user = crud.get_user(db, id, None, discord_id)
if user is None:
raise HTTPException(status_code=404, detail="User not found")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -446,9 +446,3 @@ def test_remove_user_from_all_guilds(test_client):
# discord_destinations = response.json()

# assert len(discord_destinations) == 5






Loading

0 comments on commit a4f25c6

Please sign in to comment.