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 test_cannot_add_comment_by_unknown_user(in_memory_repo): mov_title = '21' comment_text = 'The loonies are stripping the supermarkets bare!' username = '******' # Call the service layer to attempt to add the comment. with pytest.raises(movs_services.UnknownUserException): movs_services.add_review(mov_title, comment_text, username, in_memory_repo)
def test_cannot_add_review_by_unknown_user(in_memory_repo): movie_id = 2 review_text = "Hi this is my review" rating = 9 username = '******' with pytest.raises(movies_services.UnknownUserException): movies_services.add_review(movie_id, review_text, rating, username, in_memory_repo)
def test_cannot_add_review_by_unknown_user(in_memory_repo): movie_id = 54 review_text = 'Good acting' username = '******' # Call the service layer to attempt to add the review. with pytest.raises(movie_library_services.UnknownUserException): movie_library_services.add_review(movie_id, review_text, username, in_memory_repo)
def test_cannot_add_review_for_non_existent_movie(in_memory_repo): movie_id = 12 review_text = 'Favourite Movie!' rating = 10 user = '******' with pytest.raises(movies_services.NonExistentMovieException): movies_services.add_review(movie_id, review_text, rating, user, in_memory_repo)
def test_cannot_add_review_for_non_existent_user(in_memory_repo): movie_id = 2 review_text = 'Favourite Movie!' rating = 10 user = '******' with pytest.raises(movies_services.UnknownUserException): movies_services.add_review(movie_id, review_text, rating, user, in_memory_repo)
def test_cannot_add_review_by_unknown_user(in_memory_repo): movie_title = "Suicide Squad" review_text = "Lots of carnage" rating = 7 username = '******' with pytest.raises(movies_services.UnknownUserException): movies_services.add_review(movie_title, review_text, username, rating, in_memory_repo)
def test_cannot_add_review_for_non_existent_movie(in_memory_repo): movie_id = 1001 review_text = "good film" new_username = '******' new_password = '******' auth_services.add_user(new_username, new_password, in_memory_repo) # Call the service layer to attempt to add the comment. with pytest.raises(movie_library_services.NonExistentMovieException): movie_library_services.add_review(movie_id, review_text, new_username, in_memory_repo)
def test_cannot_add_review_for_non_existent_movie(in_memory_repo): movie_id = 12 review_text = "Hi this is my review" rating = 9 username = "******" password = "******" user = User(username, password) in_memory_repo.add_user(user) with pytest.raises(movies_services.NonExistentMovieException): movies_services.add_review(movie_id, review_text, rating, username, in_memory_repo)
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 test_can_add_review(in_memory_repo): movie_id = 1 movie = in_memory_repo.get_movie_by_id(1) review_text = "Hi this is my review" rating = 9 username = "******" password = "******" user = User(username, password) in_memory_repo.add_user(user) movies_services.add_review(movie_id, review_text, rating, username, in_memory_repo) assert Review(username, movie, review_text, rating) in movie.reviews
def test_cannot_add_comment_for_non_existent_movie(in_memory_repo): new_username = '******' new_password = '******' auth_services.add_user(new_username, new_password, in_memory_repo) mov_title = 'abcsac' comment_text = "what's that movie?" username = '******' # Call the service layer to attempt to add the comment. with pytest.raises(movs_services.NonExistentMovieException): movs_services.add_review(mov_title, comment_text, username, in_memory_repo)
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 text_cannot_add_review_for_non_existent_movie(in_memory_repo): new_username = '******' new_password = '******' auth_services.add_user(new_username, new_password, in_memory_repo) movie_title = "fake movie" review_text = "Lots of carnage" rating = 7 with pytest.raises(NonExistentMovieException): movies_services.add_review(movie_title, review_text, new_username, rating, in_memory_repo)
def test_can_add_review(in_memory_repo): movie_id = 1 review_text = "Wasn't a fan" rating = 4 username = '******' movies_services.add_review(movie_id, review_text, rating, username, in_memory_repo) comments_as_dict = movies_services.get_reviews_for_movie( movie_id, in_memory_repo) assert next((dictionary['review_text'] for dictionary in comments_as_dict if dictionary["review_text"] == review_text), None) is not None
def test_get_reviews_for_movie(in_memory_repo): movie_id = 1 movie = in_memory_repo.get_movie_by_id(1) review_text = "Hi this is my review" rating = 9 username = "******" password = "******" user = User(username, password) in_memory_repo.add_user(user) movies_services.add_review(movie_id, review_text, rating, username, in_memory_repo) review_dict_list = movies_services.get_reviews_for_movie( movie_id, in_memory_repo) assert len(review_dict_list) == 1 assert review_dict_list[0]["movie"] == movie
def test_get_reviews_for_movie(in_memory_repo): movie_id = 1 review_text = "Good as movie" new_username = '******' new_password = '******' auth_services.add_user(new_username, new_password, in_memory_repo) movie_library_services.add_review(movie_id, review_text, new_username, in_memory_repo) reviews_as_dict = movie_library_services.get_reviews_for_movie( 1, in_memory_repo) assert len(reviews_as_dict) == 1 # Check that the review relates to the movie whose id is 1. movie_ids = [review['movie_id'] for review in reviews_as_dict] movie_ids = set(movie_ids) assert 1 in movie_ids and len(movie_ids) == 1
def test_can_add_review(in_memory_repo): new_username = '******' new_password = '******' auth_services.add_user(new_username, new_password, in_memory_repo) movie_title = "Suicide Squad" review_text = "Lots of carnage" rating = 7 movies_services.add_review(movie_title, review_text, new_username, rating, in_memory_repo) reviews_as_dict = movies_services.get_reviews_for_movie( movie_title, in_memory_repo) assert next((dictionary['review_text'] for dictionary in reviews_as_dict if dictionary['review_text'] == review_text), None) is not None
def test_can_add_comment(in_memory_repo): new_username = '******' new_password = '******' auth_services.add_user(new_username, new_password, in_memory_repo) movie_title = '21' comment_text = 'The loonies are stripping the supermarkets bare!' username = '******' # Call the service layer to add the comment. movs_services.add_review(movie_title, comment_text, username, in_memory_repo) # Retrieve the comments for the article from the repository. comments_as_dict = movs_services.get_reviews_for_movie(movie_title, in_memory_repo) # Check that the comments include a comment with the new comment text. assert comments_as_dict != []
def test_can_add_review(in_memory_repo): new_username = '******' new_password = '******' auth_services.add_user(new_username, new_password, in_memory_repo) movie_id = 3 review_text = 'Average movie' # Call the service layer to add the review. movie_library_services.add_review(movie_id, review_text, new_username, in_memory_repo) # Retrieve the review for the movie from the repository. reviews_as_dict = movie_library_services.get_reviews_for_movie( movie_id, in_memory_repo) # Check that the review includes a review with the new review text. assert next((dictionary['review_text'] for dictionary in reviews_as_dict if dictionary['review_text'] == review_text), None) is not None
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_get_reviews_for_movie(in_memory_repo): new_username = '******' new_password = '******' auth_services.add_user(new_username, new_password, in_memory_repo) movie_title = "Suicide Squad" review_text = "Lots of carnage" rating = 7 movies_services.add_review(movie_title, review_text, new_username, rating, in_memory_repo) reviews_as_dict = movies_services.get_reviews_for_movie( movie_title, in_memory_repo) reviews_as_dict = movies_services.get_reviews_for_movie( "Suicide Squad", in_memory_repo) assert len(reviews_as_dict) == 1 movies = [review['movie'].title for review in reviews_as_dict] assert movie_title in movies and len(movies) == 1
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 test_add_review(repository): assert len(services.get_user("Mark Tremonti", repository).reviews) == 1 assert len(repository.get_reviews()) == 2 services.add_review("Moana", 2016, "Mark Tremonti", "Not my favourite.", 4, repository) assert len(services.get_user("Mark Tremonti", repository).reviews) == 2 assert len(repository.get_reviews()) == 3 with pytest.raises(services.NonExistentMovieException): services.add_review("A Silent Voice", 2016, "Mark Tremonti", "Not my favourite.", 4, repository) with pytest.raises(services.UnknownUserException): services.add_review("Moana", 2016, "Scott Phillips", "Not my favourite.", 4, repository)