Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 fix query get duplicate movie_ids #98

Merged
merged 3 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions flask_backend/repository/movies.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,15 @@ def get_all(include_drafts: bool = False) -> List[Optional[Movie]]:
def get_paginated(
current_page: int, per_page: int, include_drafts: bool = False
) -> List[Optional[Movie]]:
if current_page == 1:
offset_value = 0
else:
offset_value = current_page * per_page
offset_value = (current_page - 1) * per_page

query = db_session.query(Movie).join(Screening).order_by(Screening.id.desc())
query = db_session.query(Screening).order_by(Screening.id.desc())

if not include_drafts:
query = query.filter(Screening.draft == False) # noqa: E712

query = query.limit(per_page)
query = query.offset(offset_value)
query = query.limit(per_page).offset(offset_value)

return query.all()


Expand Down
70 changes: 33 additions & 37 deletions flask_backend/routes/movie.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,46 +39,43 @@ def posters_cubism():

@bp.route("/movies/posters/images")
def poster_images():
lazy_loading = request.headers.get("X-LAZY-LOAD", None)
if lazy_loading is None or lazy_loading != "1":
lazy_loading = request.headers.get("X-LAZY-LOAD", "0")
if lazy_loading != "1":
Gpocas marked this conversation as resolved.
Show resolved Hide resolved
abort(400)
page = request.args.get("page", 0)
limit = 4

try:
page = int(page)
page = int(request.args.get("page", 1))
limit = int(request.args.get("limit", 4))
except ValueError:
abort(400)

user_logged_in = g.user is not None
movies = get_paginated(page, limit, user_logged_in)
screenings_list = get_paginated(page, limit, user_logged_in)

if len(movies) == 0:
if len(screenings_list) == 0:
abort(404)

imgDisplayWidth = 325
images = []
image_urls = set()
for movie in movies:
for screening in movie.screenings:
if not screening.image:
continue
if screening.image in image_urls:
continue
image_urls.add(screening.image)
if screening.image:
images.append(
{
"screening_id": screening.id,
"url": screening.image,
"width": imgDisplayWidth,
"height": math.ceil(
imgDisplayWidth
/ screening.image_width
* screening.image_height
),
"image_alt": screening.image_alt,
}
)
for screening in screenings_list:
if not screening.image:
continue
if screening.image in image_urls:
continue

image_urls.add(screening.image)
if screening.image:
images.append(
{
"screening_id": screening.id,
"url": screening.image,
"width": imgDisplayWidth,
"height": math.ceil(
imgDisplayWidth / screening.image_width * screening.image_height
),
}
)
return render_template(
"movie/movie_posters.html", images=images, show_drafts=user_logged_in
)
Expand All @@ -94,19 +91,18 @@ def poster_images_urls():
abort(400)

user_logged_in = g.user is not None
movies = get_paginated(page, limit, user_logged_in)
screenings_list = get_paginated(page, limit, user_logged_in)

if len(movies) == 0:
if len(screenings_list) == 0:
abort(404)

image_urls = []
for movie in movies:
for screening in movie.screenings:
if not screening.image:
continue
if screening.image in image_urls:
continue
image_urls.append(screening.image)
for screening in screenings_list:
if not screening.image:
continue
if screening.image in image_urls:
continue
image_urls.append(screening.image)
return jsonify(image_urls)


Expand Down