diff --git a/flask_backend/repository/movies.py b/flask_backend/repository/movies.py index 72a2d67..173ec16 100644 --- a/flask_backend/repository/movies.py +++ b/flask_backend/repository/movies.py @@ -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() diff --git a/flask_backend/routes/movie.py b/flask_backend/routes/movie.py index 99cf8ef..03c01f0 100644 --- a/flask_backend/routes/movie.py +++ b/flask_backend/routes/movie.py @@ -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": 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 ) @@ -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)