示例#1
0
def test_get_movies_by_id(in_memory_repo):
    target_movie_ids = [5, 6, 7, 8]
    movies_as_dict = movie_services.get_movies_by_id(target_movie_ids,
                                                     in_memory_repo)

    # Check that 2 movies were returned from the query.
    assert len(movies_as_dict) == 4

    # Check that the movie ids returned were 5 and 6.
    movie_ids = [movie['id'] for movie in movies_as_dict]
    assert set([5, 6]).issubset(movie_ids)
示例#2
0
def movies_by_search():
    movies_per_page = 10

    # Read query parameters.
    movie_ids = request.args.getlist('movies')
    for i in range(len(movie_ids)):
        movie_ids[i] = int(movie_ids[i])
    #
    # print("movies id", movie_ids)

    cursor = request.args.get('cursor')
    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)

    movies_to_show = services.get_movies_by_id(movie_ids[cursor:cursor + movies_per_page], repo.repo_instance)
    print(movies_to_show)
    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_search', movies=movie_ids, cursor=cursor - movies_per_page)
        first_movie_url = url_for('movies_bp.movies_by_search', movies=movie_ids)

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

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

    return render_template(
        'movies/movies.html',
        title='Movies',
        movies_title='Search Result',
        movies=movies_to_show,
        selected_movies=utilities.get_selected_movies(5),
        genre_urls=utilities.get_genres_and_urls(),
        first_movie_url=first_movie_url,
        last_movie_url=last_movie_url,
        prev_movie_url=prev_movie_url,
        next_movie_url=next_movie_url,
    )
示例#3
0
def show_watchlist():
    movies_per_page = 3
    username = session['username']

    # Read query parameters.
    cursor = request.args.get('cursor')
    movies_to_show_comments = request.args.get('view_comments_for')

    if movies_to_show_comments is None:
        # No view-comments query parameter, so set to a non-existent movies id.
        movies_to_show_comments = -1
    else:
        # Convert movies_to_show_comments from string to int.
        movies_to_show_comments = int(movies_to_show_comments)

    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 ids for movies that are classified with genre_name.
    # Retrieve the batch of movies to display on the Web page.
    watch_list = services.get_watch_list_for_user(username, repo.repo_instance)

    movie_ids = []
    for movie in watch_list:
        movie_ids.append(int(movie['id']))
    # Retrieve the batch of movies to display on the Web page.
    movies = services.get_movies_by_id(movie_ids[cursor:cursor + movies_per_page], repo.repo_instance)

    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.show_watchlist', cursor=cursor - movies_per_page)
        first_movie_url = url_for('movies_bp.show_watchlist')

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

        last_cursor = movies_per_page * int(len(movie_ids) / movies_per_page)
        if len(movie_ids) % movies_per_page == 0:
            last_cursor -= movies_per_page
        last_movie_url = url_for('movies_bp.show_watchlist', cursor=last_cursor)

    # Construct urls for viewing movies comments and adding comments.
    for movie in movies:
        movie['view_comment_url'] = url_for('movies_bp.show_watchlist', cursor=cursor,
                                            view_comments_for=movie['id'])

        movie['remove_from_watch_list_url'] = url_for('movies_bp.remove_movie_watch_list_show', movie_id=movie['id'],
                                                      cursor=cursor, show=movies_to_show_comments)

    # Generate the webpage to display the movies.
    return render_template(
        'movies/watch_list.html',
        user=services.get_user(username, repo.repo_instance),
        title='Movies',
        movies_title='Watchlist for  ' + username,
        movies=movies,
        selected_movies=utilities.get_selected_movies(5),
        genre_urls=utilities.get_genres_and_urls(),
        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_comments_for_movies=movies_to_show_comments
    )
示例#4
0
def movies_by_genre():
    if 'username' not in session:
        username = None
    else:
        username = session['username']

    id_list = []
    # print("usernmae", username)
    if username is not None:
        user = services.get_user(username, repo.repo_instance)

        for movie in user['watch_list']:
            id_list.append(movie.id)
    else:
        user = None
    movies_per_page = 10
    # Read query parameters.
    genre_name = request.args.get('genre')
    cursor = request.args.get('cursor')
    movies_to_show_comments = request.args.get('view_comments_for')

    if movies_to_show_comments is None:
        # No view-comments query parameter, so set to a non-existent movies id.
        movies_to_show_comments = -1
    else:
        # Convert movies_to_show_comments from string to int.
        movies_to_show_comments = int(movies_to_show_comments)

    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 ids for movies that are classified with genre_name.
    movie_ids = services.get_movie_ids_for_genre(genre_name, repo.repo_instance)

    # Retrieve the batch of movies to display on the Web page.
    movies = services.get_movies_by_id(movie_ids[cursor:cursor + movies_per_page], repo.repo_instance)

    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(movie_ids):
        # There are further movies, 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(movie_ids) / movies_per_page)
        if len(movie_ids) % 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 movies comments and adding comments.

    for movie in movies:
        movie['view_comment_url'] = url_for('movies_bp.movies_by_genre', genre=genre_name, cursor=cursor,
                                            view_comments_for=movie['id'])
        movie['add_comment_url'] = url_for('movies_bp.comment_on_movies', movie=movie['id'], cursor=cursor,
                                           page='genre')
        # if movie not in;
        # print("movie id", movie['id'])
        if user is not None:
            if movie['id'] not in id_list:
                movie['add_to_watch_list_url'] = url_for('movies_bp.watch_list_genres', movie_id=movie['id'],
                                                         genre=genre_name, cursor=cursor, show=movies_to_show_comments)
            else:
                movie['add_to_watch_list_url'] = ""
                movie['remove_from_watch_list_url'] = url_for('movies_bp.remove_movie_watch_list_genres',
                                                              movie_id=movie['id'], genre=genre_name,
                                                              cursor=cursor, show=movies_to_show_comments)
        else:
            movie['add_to_watch_list_url'] = None

    return render_template(
        'movies/movies.html',
        title='Movies',
        movies_title='Movies are Classified by ' + genre_name,
        movies=movies,
        selected_movies=utilities.get_selected_movies(6),
        genre_urls=utilities.get_genres_and_urls(),
        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_comments_for_movies=movies_to_show_comments,
        id_list=id_list
    )
示例#5
0
def movies_by_genre():
    movies_per_page = 100
    genre_name = request.args.get('genre')
    cursor = request.args.get('cursor')
    movies_to_show_reviews = request.args.get('view_reviews_for')

    if movies_to_show_reviews is None:
        movies_to_show_reviews = -1
    else:
        movies_to_show_reviews = int(movies_to_show_reviews)

    if cursor is None:
        cursor = 0

    else:
        cursor = int(cursor)

    movie_ids = services.get_movie_ids_for_genre(genre_name,
                                                 repo.repo_instance)
    movies = services.get_movies_by_id(
        movie_ids[cursor:cursor + movies_per_page], repo.repo_instance)

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

    if cursor > 0:
        prev_movie_url = url_for('movie_bp.movies_by_genre',
                                 genre=genre_name,
                                 cursor=cursor - movies_per_page)
        first_movie_url = url_for('movie_bp.movies_by_genre', genre=genre_name)

    if cursor + movies_per_page < len(movie_ids):
        next_movie_url = url_for('movie_bp.movies_by_genre',
                                 genre=genre_name,
                                 cursor=cursor + movies_per_page)

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

    for movie in movies:
        movie['view_review_url'] = url_for('movie_bp.movies_by_genre',
                                           genre=genre_name,
                                           cursor=cursor,
                                           view_reviews_for=movie['id'])
        movie['add_review_url'] = url_for('movie_bp.review_on_movie',
                                          movie=movie['id'])

    return render_template('movie/movie.html',
                           title='movies',
                           movie_title='Movies tagged by ' + genre_name,
                           movies=movies,
                           selected_movies=utilities.get_selected_movies(5),
                           genre_urls=utilities.get_genres_and_urls(),
                           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=movies_to_show_reviews)