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 1 commit
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
23 changes: 15 additions & 8 deletions flask_backend/repository/movies.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from typing import List, Optional

from sqlalchemy import func

from flask_backend.db import db_session
from flask_backend.models import Movie, Screening

Expand All @@ -22,18 +24,23 @@ 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

query = db_session.query(Movie).join(Screening).order_by(Screening.id.desc())
offset_value = (current_page - 1) * per_page

query = (
db_session.query(Screening)
.filter(
Screening.id.in_(
db_session.query(func.max(Screening.id)).group_by(Screening.movie_id)
)
)
.order_by(Screening.id.desc())
)
guites marked this conversation as resolved.
Show resolved Hide resolved

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
69 changes: 33 additions & 36 deletions flask_backend/routes/movie.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,45 +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)
scrennigs_list = get_paginated(page, limit, user_logged_in)
guites marked this conversation as resolved.
Show resolved Hide resolved

if len(movies) == 0:
if len(scrennigs_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
),
}
)
for screening in scrennigs_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 @@ -93,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