示例#1
0
文件: movies.py 项目: shaaen26/235-A3
def search_by_keyword():
    movies_per_page = 5

    # Read query parameters.
    search_type = request.args.get('type')
    search_keyword = request.args.get('keyword')
    cursor = request.args.get('cursor')

    movie_to_show_review = request.args.get('view_reviews')

    if movie_to_show_review is None:
        # No movie_to_show_review query parameter, so set to false
        movie_to_show_review = False
    else:
        # Convert article_to_show_comments from string to int.
        movie_to_show_review = True

    if cursor is None:
        # No cursor query parameter, so initialise cursor to start at the beginning.
        cursor = 0
    else:
        # Convert cursor from string to int.
        cursor = int(cursor)

    # Retrieve movies and parse them to dict
    all_target_movies = []
    if search_type.lower() == 'actor':
        all_target_movies = [services.movie_to_dict(i) for i in
                             services.get_movies_by_actor(search_keyword, repo.repo_instance)]
    else:
        all_target_movies = [services.movie_to_dict(i) for i in
                             services.get_movies_by_director(search_keyword, repo.repo_instance)]

    specific_movies = all_target_movies[cursor:cursor + movies_per_page]

    first_movie_url = None
    last_movie_url = None
    next_movie_url = None
    prev_movie_url = None

    if cursor > 0:
        # There are preceding movies, so generate URLs for the 'previous' and 'first' navigation buttons.
        prev_movie_url = url_for('movies_bp.search_by_keyword', type=search_type, keyword=search_keyword,
                                 cursor=cursor - movies_per_page)
        first_movie_url = url_for('movies_bp.search_by_keyword', type=search_type, keyword=search_keyword)

    if cursor + movies_per_page < len(all_target_movies):
        # There are further articles, so generate URLs for the 'next' and 'last' navigation buttons.
        next_movie_url = url_for('movies_bp.search_by_keyword', type=search_type, keyword=search_keyword,
                                 cursor=cursor + movies_per_page)

        last_cursor = movies_per_page * int(len(all_target_movies) / movies_per_page)
        if len(all_target_movies) % movies_per_page == 0:
            last_cursor -= movies_per_page
        last_movie_url = url_for('movies_bp.search_by_keyword', type=search_type, keyword=search_keyword,
                                 cursor=last_cursor)

    # Construct urls for viewing article comments and adding comments.
    for movie in specific_movies:
        movie['view_review_url'] = url_for('movies_bp.movies_by_genre', type=search_type, keyword=search_keyword,
                                           cursor=cursor,
                                           view_reviews=True)
        movie['add_review_url'] = url_for('movies_bp.review_on_movie', id=movie["_rank"], type=search_type, keyword=search_keyword, cursor=cursor)

    # Generate the webpage to display the articles.
    return render_template(
        'movies/movielist.html',
        movies_title='Movies search by ' + search_type + ' with keyword: ' + search_keyword,
        movies=specific_movies,
        first_movie_url=first_movie_url,
        last_movie_url=last_movie_url,
        prev_movie_url=prev_movie_url,
        next_movie_url=next_movie_url,
        show_reviews_for_movie=movie_to_show_review,
        genre_urls=utilities.get_all_genre_urls()
    )
示例#2
0
def movies_by_genre():
    movies_per_page = 5

    # Read query parameters.
    genre_name = request.args.get('genre')
    cursor = request.args.get('cursor')
    movie_to_show_review = request.args.get('view_reviews')

    if movie_to_show_review is None:
        # No movie_to_show_review query parameter, so set to false
        movie_to_show_review = False
    else:
        # Convert article_to_show_comments from string to int.
        movie_to_show_review = True

    if cursor is None:
        # No cursor query parameter, so initialise cursor to start at the beginning.
        cursor = 0
    else:
        # Convert cursor from string to int.
        cursor = int(cursor)

    # Retrieve movies and parse them to dict
    movies = [services.movie_to_dict(i) for i in services.get_movies_by_genre(genre_name)]

    specific_movies = movies[cursor:cursor + movies_per_page]

    first_movie_url = None
    last_movie_url = None
    next_movie_url = None
    prev_movie_url = None

    if cursor > 0:
        # There are preceding movies, so generate URLs for the 'previous' and 'first' navigation buttons.
        prev_movie_url = url_for('movies_bp.movies_by_genre', genre=genre_name, cursor=cursor - movies_per_page)
        first_movie_url = url_for('movies_bp.movies_by_genre', genre=genre_name)

    if cursor + movies_per_page < len(movies):
        # There are further articles, so generate URLs for the 'next' and 'last' navigation buttons.
        next_movie_url = url_for('movies_bp.movies_by_genre', genre=genre_name, cursor=cursor + movies_per_page)

        last_cursor = movies_per_page * int(len(movies) / movies_per_page)
        if len(movies) % movies_per_page == 0:
            last_cursor -= movies_per_page
        last_movie_url = url_for('movies_bp.movies_by_genre', genre=genre_name, cursor=last_cursor)

    # Construct urls for viewing article comments and adding comments.
    for movie in specific_movies:
        movie['view_review_url'] = url_for('movies_bp.movies_by_genre', genre=genre_name, cursor=cursor,
                                           view_reviews=True)
        movie['add_review_url'] = url_for('movies_bp.review_on_movie', id=movie["id"], genre=genre_name, cursor=cursor)

    # Generate the webpage to display the articles.
    return render_template(
        'movies/movielist.html',
        movies_title='Movies Genre: ' + genre_name,
        movies=specific_movies,
        first_movie_url=first_movie_url,
        last_movie_url=last_movie_url,
        prev_movie_url=prev_movie_url,
        next_movie_url=next_movie_url,
        show_reviews_for_movie=movie_to_show_review,
        genre_urls=utilities.get_all_genre_urls()
    )
示例#3
0
文件: home.py 项目: shaaen26/235-A3
def home():
    print(utilities.get_all_genre_urls())
    return render_template(
        'home/home.html',
        genre_urls=utilities.get_all_genre_urls()
    )