def review_a_movie(): username = session['username'] form = ReviewForm() if form.validate_on_submit(): movie_title = str(form.movie_title.data) services.add_review(movie_title, form.review.data, username, form.rating.data, repo.repo_instance) movie = services.get_movie(movie_title, repo.repo_instance) return redirect(url_for('movies_bp.list_movie', movie = movie_title)) if request.method == 'GET': movie_title = str(request.args.get('movie')) form.movie_title.data = movie_title movie = services.get_movie(movie_title, repo.repo_instance) else: movie_title = form.movie_title.data movie = services.get_movie(movie_title, repo.repo_instance) return render_template( 'movies/review_a_movie.html', title = 'Edit movie', movie = movie, form = form, movieSearch = movieByTitle(), )
def review_movie(): # Obtain the username of the currently logged in user. username = session['username'] recommendations = utilities.get_recommendations(username) # Create form. The form maintains state, e.g. when this method is called with a HTTP GET request and populates # the form with a movie id, when subsequently called with a HTTP POST request, the movie id remains in the # form. form = ReviewForm() if form.validate_on_submit(): # Successful POST, i.e. the review text has passed data validation. # Extract the movie id, representing the reviewed movie, from the form. movie_id = int(form.movie_id.data) # Use the service layer to store the new review. services.add_review(movie_id, form.review.data, form.rating.data, username, repo.repo_instance) # Retrieve the movie in dict form. movie = services.get_movie(movie_id, repo.repo_instance) # Cause the web browser to display the page of all movies that have the same title as the commented movie, # and display all reviews, including the new review. return redirect( url_for('movies_bp.movies_by_title', title=movie['title'], view_reviews_for=movie_id)) if request.method == 'GET': # Request is a HTTP GET to display the form. # Extract the movie id, representing the movie to review, from a query parameter of the GET request. movie_id = int(request.args.get('movie')) # Store the movie id in the form. form.movie_id.data = movie_id else: # Request is a HTTP POST where form validation has failed. # Extract the movie id of the movie being reviewed from the form. movie_id = int(form.movie_id.data) # For a GET or an unsuccessful POST, retrieve the movie to review in dict form, and return a Web page that allows # the user to enter a review. The generated Web page includes a form object. movie = services.get_movie(movie_id, repo.repo_instance) return render_template('movies/review_movie.html', title='Edit review', movie=movie, form=form, handler_url=url_for('movies_bp.review_movie'), selected_movies=utilities.get_selected_movies(), genre_urls=utilities.get_genres_and_urls(), recommendations=recommendations)
def test_reviews_to_dict(repository): review_dicts = services.reviews_to_dict(repository.get_reviews(), repository) assert review_dicts[0]['movie_title'] == "Moana" and review_dicts[0]['movie_release_year'] == 2016 and \ review_dicts[0]['user'] == "Myles Kennedy" and review_dicts[0]['review_text'] == "very nice" and \ review_dicts[0]['rating'] == 9 and len(review_dicts) == 2 review3 = Review(services.get_movie("Moana", 2016, repository), "Not my favourite.", 4) assert services.review_to_dict(review3, repository) is None assert services.review_to_dict("not a review", repository) is None
def comment_on_movies(): # Obtain the username of the currently logged in user. username = session['username'] # Create form. The form maintains state, e.g. when this method is called with a HTTP GET request and populates # the form with an article id, when subsequently called with a HTTP POST request, the article id remains in the # form. form = CommentForm() movie_title = request.args.get('movie') if form.validate_on_submit(): # Successful POST, i.e. the comment text has passed data validation. # Extract the article id, representing the commented article, from the form. movie_title = form.movie_title.data # Use the service layer to store the new comment. services.add_review( movie_title[movie_title.find(" ") + 1:movie_title.find(",")], form.comment.data, username, repo.repo_instance) # Retrieve the article in dict form. movie_in_dict = services.get_movie( movie_title[movie_title.find(" ") + 1:movie_title.find(",")], repo.repo_instance) # Cause the web browser to display the page of all articles that have the same date as the commented article, # and display all comments, including the new comment. return redirect( url_for('movies_bp.display_all_movies', view_comments_for=movie_title)) if request.method == 'GET': # Request is a HTTP GET to display the form. # Extract the article id, representing the article to comment, from a query parameter of the GET request. movie_title = request.args.get('movie') # Store the article id in the form. form.movie_title.data = movie_title else: # Request is a HTTP POST where form validation has failed. # Extract the article id of the article being commented from the form. movie_title = form.movie_title.data # For a GET or an unsuccessful POST, retrieve the article to comment in dict form, and return a Web page that allows # the user to enter a comment. The generated Web page includes a form object. return render_template('movies/movies_review.html', title='Edit article', movie=movie_title[movie_title.find(" ") + 1:movie_title.find(",")], form=form, handler_url=url_for('movies_bp.comment_on_movies'), mov_gen=utilities.get_movies_all(), genre_display=utilities.get_movies_with_genre())
def watched_movie(): username = session['username'] recommendations = utilities.get_recommendations(username) movie_id = int(request.args.get('movie')) movie = services.get_movie(movie_id, repo.repo_instance) user = services.get_user(repo.repo_instance, username) user.watch_movie(movie) movie['add_review_url'] = url_for('movies_bp.review_movie', movie=movie['id']) return render_template('movies/watched_movie.html', user=user, movie=movie, selected_movies=utilities.get_selected_movies(2), recommendations=recommendations)
def test_can_get_movie(in_memory_repo): movie_id = 2 movie_as_dict = movies_services.get_movie(movie_id, in_memory_repo) assert movie_as_dict['id'] == movie_id assert movie_as_dict[ 'description'] == "Following clues to the origin of mankind, a team finds a structure on a distant moon, but they soon realize they are not alone." assert movie_as_dict['title'] == "Prometheus" assert movie_as_dict['director'] == "Ridley Scott" assert movie_as_dict['actors'] == [ "Noomi Rapace", "Logan Marshall-Green", "Michael Fassbender", "Charlize Theron" ] assert movie_as_dict['genres'][0]["genre"] == "Adventure" assert movie_as_dict['runtime'] == 124 assert len(movie_as_dict['reviews']) == 0 assert movie_as_dict['year'] == 2012
def reviews(): target_title = request.args.get('title') target_year = request.args.get('release_year') if target_year is not None: target_year = int(target_year) target_movie = services.get_movie(target_title, target_year, repo.repo_instance) if target_movie is None: return redirect(url_for('home_bp.home')) review_list = services.get_reviews_for_movie(target_movie, repo.repo_instance) reviews_id = 0 if request.args.get('next_id') is not None: reviews_id = int(request.args.get('next_id')) next_id = min(reviews_id + 3, len(review_list)) prev_id = max(reviews_id - 3, 0) next_reviews = False prev_reviews = False if len(review_list) - (reviews_id + 1) >= 3: next_reviews = True if reviews_id >= 3: prev_reviews = True target_movie = services.movie_to_dict(target_movie) target_movie['url'] = url_for('movies_bp.movie', title=target_title, release_year=target_year) target_movie['review_url'] = url_for('movies_bp.review_movie', title=target_title, release_year=target_year) return render_template('movies/reviews.html', movie=target_movie, reviews=review_list[reviews_id:next_id], prev_reviews=prev_reviews, prev_reviews_url=url_for('movies_bp.reviews', title=target_title, release_year=target_year, next_id=prev_id), next_reviews=next_reviews, next_reviews_url=url_for('movies_bp.reviews', title=target_title, release_year=target_year, next_id=next_id), watchlist_empty=utilities.get_watchlist_empty())
def test_can_get_movie(in_memory_repo): movie_title = "Suicide Squad" movie_as_dict = movies_services.get_movie(movie_title, in_memory_repo) assert movie_as_dict['title'] == "Suicide Squad" assert movie_as_dict['release'] == 2016 assert movie_as_dict[ 'description'] == "A secret government agency recruits some of the most dangerous incarcerated super-villains to form a defensive task force. Their first mission: save the world from the apocalypse." assert repr(movie_as_dict['director']) == "<Director David Ayer>" assert repr( movie_as_dict['actors'] ) == "[<Actor Will Smith>, <Actor Jared Leto>, <Actor Margot Robbie>, <Actor Viola Davis>]" assert repr(movie_as_dict['genres'] ) == "[<Genre Action>, <Genre Adventure>, <Genre Fantasy>]" assert movie_as_dict['runtime'] == 123 assert movie_as_dict['revenue'] == 325.02 assert len(movie_as_dict['reviews']) == 0
def display_movie(): username = None try: username = session['username'] except: pass recommendations = utilities.get_recommendations(username) movie_id = int(request.args.get('movie')) movie = services.get_movie(movie_id, repo.repo_instance) movie['add_review_url'] = url_for('movies_bp.review_movie', movie=movie['id']) movie['watched_movie_url'] = url_for('movies_bp.watched_movie', movie=movie['id']) return render_template('movies/display_movie.html', movie=movie, genre_urls=utilities.get_genres_and_urls(), selected_movies=utilities.get_selected_movies(2), recommendations=recommendations)
def movie(): movie_id = int(request.args.get('movie_id')) movie_to_show_reviews = request.args.get('view_reviews_for') in_watchlist = request.args.get('in_watchlist') watchlist = services.get_watchlist(repo.repo_instance) movie_in_watchlist = services.movie_in_watchlist(watchlist, movie_id) if movie_to_show_reviews is None: movie_to_show_reviews = -1 else: movie_to_show_reviews = int(movie_to_show_reviews) if in_watchlist is not None: if int(in_watchlist) == 1: services.add_to_watchlist(movie_id, repo.repo_instance) return redirect( url_for("movies_bp.movie", movie_id=movie_id, view_reviews_for=movie_to_show_reviews)) if int(in_watchlist) == -1: services.remove_from_watchlist(movie_id, repo.repo_instance) return redirect( url_for("movies_bp.movie", movie_id=movie_id, view_reviews_for=movie_to_show_reviews)) movie_dict = services.get_movie(movie_id, repo.repo_instance) movie_dict["view_review_url"] = url_for('movies_bp.movie', movie_id=movie_id, view_reviews_for=movie_id) movie_dict["add_review_url"] = url_for('movies_bp.review_movie', movie_id=movie_id) return render_template('movies/movie.html', movie=movie_dict, review_page=0, show_reviews_for_movie=movie_to_show_reviews, watchlist=watchlist, movie_in_watchlist=movie_in_watchlist)
def test_can_get_movie(in_memory_repo): movie_id = 1 movie_as_dict = movies_services.get_movie(movie_id, in_memory_repo) assert movie_id == movie_as_dict['id'] assert movie_as_dict['title'] == "Guardians of the Galaxy" assert movie_as_dict[ 'description'] == 'A group of intergalactic criminals are forced to work together to stop a fanatical ' \ 'warrior from taking control of the universe. ' assert movie_as_dict['director'] == Director("James Gunn") actors = movie_as_dict['actors'] assert Actor("Chris Pratt") in actors assert Actor("Vin Diesel") in actors genres = movie_as_dict['genres'] assert Genre('Action') in genres assert Genre('Adventure') in genres assert movie_as_dict['release_year'] == 2014 assert movie_as_dict["runtime_minutes"] == 121 assert movie_as_dict["rating"] == 8.1 assert movie_as_dict["votes"] == 757074 assert movie_as_dict["revenue"] == 333.13 assert movie_as_dict["metascore"] == 76
def review_movie(): username = session['username'] form = ReviewForm() if form.validate_on_submit(): try: movie_title = form.movie_title.data release_year = int(form.release_year.data) print("movies: ", type(username)) services.add_review(movie_title, release_year, username, form.review.data, int(form.rating.data), repo.repo_instance) return redirect( url_for('movies_bp.reviews', title=movie_title, release_year=release_year)) except services.UnknownUserException: return redirect(url_for('authentication_bp.login')) if request.method == 'GET': movie_title = request.args.get('title') release_year = int(request.args.get('release_year')) form.movie_title.data = movie_title form.release_year.data = release_year else: movie_title = form.movie_title.data release_year = int(form.release_year.data) reviewed_movie = services.get_movie(movie_title, release_year, repo.repo_instance) return render_template('movies/review_movie.html', movie=services.movie_to_dict(reviewed_movie), handler_url=url_for('movies_bp.review_movie', title=movie_title, release_year=release_year), form=form, watchlist_empty=utilities.get_watchlist_empty())
def review_movie(): # Obtain the username of the currently logged in user. username = session['username'] form = ReviewForm() if form.validate_on_submit(): # Successful POST, i.e. the review text has passed data validation. # Extract the movie id, representing the reviewed article, from the form. movie_id = int(form.movie_id.data) rating = int(form.rating.data) services.add_review(movie_id, form.review.data, rating, username, repo.repo_instance) return redirect(url_for('movies_bp.movie', movie_id=movie_id)) if request.method == 'GET': # Request is a HTTP GET to display the form. # Extract the movie id, representing the movie to review, from a query parameter of the GET request. movie_id = int(request.args.get('movie_id')) # Store movie id in the form form.movie_id.data = movie_id else: # Request is a HTTP POST where form validation has failed. movie_id = int(form.movie_id.data) # For a GET or an unsuccessful POST movie_dict = services.get_movie(movie_id, repo.repo_instance) watchlist = services.get_watchlist(repo.repo_instance) return render_template('movies/review_movie.html', title='Review Movie', form=form, movie=movie_dict, review_page=1, handler_url=url_for('movies_bp.review_movie'), watchlist=watchlist)
def test_can_get_movie(in_memory_repo): movie_id = 1 movie_as_dict = movie_library_services.get_movie(movie_id, in_memory_repo) assert movie_as_dict['id'] == movie_id assert movie_as_dict['year'] == 2014 assert movie_as_dict['title'] == 'Guardians of the Galaxy' assert movie_as_dict['description'] == ( 'A group of intergalactic criminals are forced to work together to stop a fanatical warrior from taking control of the universe.' ) assert len(movie_as_dict['reviews']) == 0 assert movie_as_dict['actors'] == [ Actor("Chris Pratt"), Actor("Vin Diesel"), Actor("Bradley Cooper"), Actor("Zoe Saldana") ] assert movie_as_dict['genres'] == [ Genre("Action"), Genre("Adventure"), Genre("Sci-Fi") ] assert movie_as_dict['director'] == Director("James Gunn")
def search(): form = SearchForm() watchlist = services.get_watchlist(repo.repo_instance) search_result = [] search = [ request.args.get('search_genre'), request.args.get('search_actor'), request.args.get('search_director') ] cursor = request.args.get('cursor') movies_per_page = 10 first_movie_url = None last_movie_url = None next_movie_url = None prev_movie_url = None 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) if form.validate_on_submit(): genre = form.genre.data actor = form.actor.data director = form.director.data return redirect( url_for('movies_bp.search', search_genre=genre, search_actor=actor, search_director=director)) if search is not None: movies_genre = [] movies_actor = [] movies_director = [] search_list = [] genre = search[0] if genre != "" and genre is not None: genre = genre[0].upper() + genre[1:].lower() movies_genre = services.get_movies_from_genre( genre, repo.repo_instance) actor = search[1] if actor != "" and actor is not None: actor = services.get_actor(actor, repo.repo_instance) if actor is not None: movies_actor = actor['movies'] director = search[2] if director != "" and director is not None: director = services.get_director(director, repo.repo_instance) if director is not None: movies_director = director['movies'] search_list = [movies_genre, movies_actor, movies_director] # Remove search elements that are empty for i in range(len(search_list) - 1, -1, -1): if not search_list[i]: search_list.pop(i) # Find common movies for search parameters search_result = services.elements_in_common(search_list) 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', search_genre=search[0], search_actor=search[1], search_director=search[2], cursor=cursor - movies_per_page) first_movie_url = url_for('movies_bp.search', search_genre=search[0], search_actor=search[1], search_director=search[2]) if cursor + movies_per_page < len(search_result): # There are further movies, so generate URLs for the 'next' and 'last' navigation buttons. next_movie_url = url_for('movies_bp.search', search_genre=search[0], search_actor=search[1], search_director=search[2], cursor=cursor + movies_per_page) last_cursor = movies_per_page * int( len(search_result) / movies_per_page) if len(search_result) % movies_per_page == 0: last_cursor -= movies_per_page last_movie_url = url_for('movies_bp.search', search_genre=search[0], search_actor=search[1], search_director=search[2], cursor=last_cursor) # Get Movie for current page last_movie_index = cursor + movies_per_page search_result = search_result[cursor:last_movie_index] # Get movies from movie ids for i in range(0, len(search_result)): search_result[i] = services.get_movie(search_result[i], repo.repo_instance) return render_template('movies/search.html', search_result=search_result, watchlist=watchlist, form=form, handler_url=url_for('movies_bp.search'), prev_movie_url=prev_movie_url, first_movie_url=first_movie_url, next_movie_url=next_movie_url, last_movie_url=last_movie_url)
def test_cannot_get_movie_with_non_existent_id(in_memory_repo): movie_id = 12 with pytest.raises(movies_services.NonExistentMovieException): movies_services.get_movie(movie_id, in_memory_repo)
def test_get_movie(repository): assert services.get_movie("Moana", 2016, repository) == Movie("Moana", 2016) assert services.get_movie("A Silent Voice", 2016, repository) is None
def test_cannot_get_non_existent_movie(in_memory_repo): movie_title = "fake movie" with pytest.raises(NonExistentMovieException): movies_services.get_movie(movie_title, in_memory_repo)
def test_cannot_get_movie_with_non_existent_id(in_memory_repo): movie_id = 1002 # Call the service layer to attempt to retrieve the movie. with pytest.raises(movie_library_services.NonExistentMovieException): movie_library_services.get_movie(movie_id, in_memory_repo)
def movie(): target_title = request.args.get('title') target_year = request.args.get('release_year') if target_year is not None: target_year = int(target_year) target_movie = services.get_movie(target_title, target_year, repo.repo_instance) if target_movie is None: return redirect(url_for('home_bp.home')) in_watchlist = False if 'username' in session: username = session['username'] try: user = services.get_user(username, repo.repo_instance) if target_movie in user.watchlist: in_watchlist = True except services.UnknownUserException: return redirect(url_for('authentication_bp.login')) add_to_watchlist = request.args.get('add_to_watchlist') remove_from_watchlist = request.args.get('remove_from_watchlist') watch_now = request.args.get('watch_now') if add_to_watchlist == "True" or in_watchlist or watch_now == "True": if 'username' in session: try: username = session['username'] user = services.get_user(username, repo.repo_instance) except services.UnknownUserException: return redirect(url_for('authentication_bp.login')) else: return redirect((url_for('authentication_bp.login'))) if add_to_watchlist == "True": user.watchlist.add_movie(target_movie) in_watchlist = True elif remove_from_watchlist == "True": user.watchlist.remove_movie(target_movie) in_watchlist = False if watch_now == "True": user.watch_movie(target_movie) if in_watchlist: user.watchlist.remove_movie(target_movie) in_watchlist = False if in_watchlist: list_id = 0 for i in range(user.watchlist.size()): if user.watchlist.select_movie_to_watch(i) == target_movie: list_id = i break next_id = min(list_id + 1, user.watchlist.size()) prev_id = max(list_id - 1, 0) next_watchlist = False prev_watchlist = False if user.watchlist.size() - (list_id + 1) >= 1: next_watchlist = True next_movie = user.watchlist.select_movie_to_watch(next_id) next_movie_url = None if next_movie is not None: next_movie_url = url_for('movies_bp.movie', title=next_movie.title, release_year=next_movie.release_year) if list_id >= 1: prev_watchlist = True prev_movie = user.watchlist.select_movie_to_watch(prev_id) prev_movie_url = None if prev_movie is not None: prev_movie_url = url_for('movies_bp.movie', title=prev_movie.title, release_year=prev_movie.release_year) target_movie = services.movie_to_dict(target_movie) target_movie['url'] = url_for('movies_bp.movie', title=target_title, release_year=target_year) target_movie['review_url'] = url_for('movies_bp.review_movie', title=target_title, release_year=target_year) return render_template( 'movies/movie.html', movie=target_movie, next_watchlist=next_watchlist, next_movie_url=next_movie_url, prev_watchlist=prev_watchlist, prev_movie_url=prev_movie_url, watch_now_url=url_for('movies_bp.movie', title=target_title, release_year=target_year, watch_now=True), in_watchlist=True, add_to_watchlist_url=url_for('movies_bp.movie', title=target_title, release_year=target_year, add_to_watchlist=True), remove_from_watchlist_url=url_for('movies_bp.movie', title=target_title, release_year=target_year, remove_from_watchlist=True), reviews_url=url_for('movies_bp.reviews', title=target_title, release_year=target_year), watchlist_empty=False) target_movie = services.movie_to_dict(target_movie) target_movie['url'] = url_for('movies_bp.movie', title=target_title, release_year=target_year) target_movie['review_url'] = url_for('movies_bp.review_movie', title=target_title, release_year=target_year) return render_template( 'movies/movie.html', movie=target_movie, next_watchlist=False, next_movie_url=None, prev_watchlist=False, prev_movie_url=None, watch_now_url=url_for('movies_bp.movie', title=target_title, release_year=target_year, watch_now=True), in_watchlist=in_watchlist, add_to_watchlist_url=url_for('movies_bp.movie', title=target_title, release_year=target_year, add_to_watchlist=True), remove_from_watchlist_url=url_for('movies_bp.movie', title=target_title, release_year=target_year, remove_from_watchlist=True), reviews_url=url_for('movies_bp.reviews', title=target_title, release_year=target_year), watchlist_empty=utilities.get_watchlist_empty())