def review_on_movie(): # 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 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 comment text has passed data validation. # Extract the movie id, representing the reviewed movie, from the form. movie_id = int(form.movie_id.data) rating = int(form.rating.data) # Use the service layer to store the new review services.add_review(form.review.data, username, movie_id, rating, repo.repo_instance) # Retrieve the movie in dict form. movie = services.get_movie(movie_id, repo.repo_instance) release_year = movie['release_year'] # genre_name = movie['genres'][0] print(movie['reviews'][0]['timestamp']) # Cause the web browser to display the page of all movies that have the same genre as the reviewed movie, and # display all reviews, including the new review. # return redirect(url_for('movies_bp.movies_by_release_year', year=release_year, view_reviews_for=movie_id)) return redirect( url_for('movies_bp.search_movies_by_title', title=movie['title'])) 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 article id of the article being commented 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 include a form object. movie = services.get_movie(movie_id, repo.repo_instance) return render_template( 'movies/review_on_movie.html', title='Edit movie', movie=movie, form_review=form, form=SearchForm(), handler_url_review=url_for('movies_bp.review_on_movie'), handler_url=url_for("movies_bp.search"), selected_movies=utilities.get_selected_movies(), genre_urls=utilities.get_genres_and_urls(), title_form=SearchByTitleForm(), handler_url_title=url_for('movies_bp.search_by_title'), )
def test_can_add_review(): mem_repo = MemoryRepository() up_movie = Movie("Up", 2009, 1) klaus_movie = Movie("Klaus", 2019, 2) dolittle_movie = Movie("Dolittle", 2019, 3) mem_repo.add_movie(up_movie) mem_repo.add_movie(klaus_movie) mem_repo.add_movie(dolittle_movie) # ["Dolittle", "Klaus", "Up"] movie_id = 3 review_text = "Very good!" username = "******" rating = 5 mem_repo.add_user(User(username, "CS235")) # call the service layer to add the comment movie_services.add_review(movie_id, review_text, rating, username, mem_repo) # retrieve the reviews for the movie from the repository reviews_as_dict = movie_services.get_reviews_for_movie(movie_id, mem_repo) # check that the reviews inclue 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 test_cannot_add_comment_by_unknown_user(in_memory_repo): rank = 2 comment_text = "COVID-19 - what's that?" username = '******' # Call the service layer to attempt to add the comment. with pytest.raises(movies_services.UnknownUserException): movies_services.add_review(10,rank,comment_text, username, in_memory_repo)
def test_cannot_add_comment_for_non_existent_article(in_memory_repo): rank = 1001 comment_text = "COVID-19 - what's that?" username = '******' # Call the service layer to attempt to add the comment. with pytest.raises(movies_services.NonExistentMovieException): movies_services.add_review(10,rank,comment_text, username, in_memory_repo)
def test_cannot_add_review_by_unknown_user(in_memory_repo): movie_id = 3 review_text = 'The loonies are stripping the supermarkets bare!' username = '******' # Call the service layer to attempt to add the comment. with pytest.raises(movie_services.UnknownUserException): movie_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 = 7 review_text = "COVID-19 - what's that?" username = '******' # Call the service layer to attempt to add the comment. with pytest.raises(movie_services.NonExistentMovieException): movie_services.add_review(movie_id, review_text, username, in_memory_repo)
def review_on_movie(): # 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 = ReviewForm() 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_id = int(form.movie_id.data) # Use the service layer to store the new comment. services.add_review(movie_id, form.comment.data, username, repo.repo_instance) # Retrieve the article in dict form. movie = services.get_movie(movie_id, 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.review_on_movie', date=movie['year'], view_reviews_for=movie_id)) 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_id = int(request.args.get('movie')) # Store the article id in the form. form.movie_id.data = movie_id else: # Request is a HTTP POST where form validation has failed. # Extract the article id of the article being commented from the form. movie_id = int(form.movie_id.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. movie = services.get_movie(movie_id, repo.repo_instance) return render_template( 'movies/review_on_movie.html', title='Review movie', movie=movie, form=form, handler_url=url_for('movie_bp.review_on_movie'), selected_articles=utilities.get_selected_movies(), genre_urls=utilities.get_genres_and_urls(), actor_urls=utilities.get_actors_and_urls(), director_urls=utilities.get_directors_and_urls(), )
def test_can_add_comment(in_memory_repo): comment_text = 'The loonies are stripping the supermarkets bare!' username = '******' # Call the service layer to add the comment. movies_services.add_review(10,2,comment_text, username, in_memory_repo) # Retrieve the reviews for the article from the repository. reviews_as_list = movies_services.get_reviews(2, in_memory_repo) # Check that the comments include a comment with the new comment text. assert str(reviews_as_list) == "[<Review (10/10):The loonies are stripping the supermarkets bare!>]"
def test_cannot_add_review_by_unknown_user(in_memory_repo): movie_id = 2 review_text = 'What a great movie!' username = '******' rating = 10 # Call the service layer to attempt add the review with pytest.raises(movies_services.UnknownUserException): movies_services.add_review(review_text=review_text, username=username, movie_id=movie_id, rating=rating, repo=in_memory_repo)
def test_cannot_add_review_for_non_existent_movie(in_memory_repo): movie_id = 20 review_text = 'What a great movie!' username = '******' rating = 10 # Call the service layer to attempt add the review with pytest.raises(movies_services.NonExistentMovieException): movies_services.add_review(review_text=review_text, username=username, movie_id=movie_id, rating=rating, repo=in_memory_repo)
def review_movie(): # obatin the username of the currently logged in user username = session['username'] # create form form = ReviewForm() if form.validate_on_submit(): # successful POST # extract movie id movie_id = int(form.movie_id.data) # use the service layer to store the new review services.add_review(movie_id, form.review.data, username, 0, 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 date as the reviewed movie, # and display all reviews, including the new review. return redirect( url_for('movies_bp.movies_by_date', year=movie['release_year'], view_reviews_for=movie_id)) if request.method == 'GET': # request is a HTTP GET to display the form # extract the movie 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 for 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 movie', 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())
def test_can_add_review(in_memory_repo): movie_id = 3 review_text = 'The loonies are stripping the supermarkets bare!' username = '******' # Call the service layer to add the comment. movie_services.add_review(movie_id, review_text, username, in_memory_repo) # Retrieve the comments for the article from the repository. reviews_as_dict = movie_services.get_reviews_for_movie( movie_id, in_memory_repo) # Check that the comments include a comment with the new comment text. assert next((dictionary['review_text'] for dictionary in reviews_as_dict if dictionary['review_text'] == review_text), None) is not None
def movie_play(movie_name, rank): # Obtain the username of the currently logged in user. username = session['username'] form = CommentForm() if form.validate_on_submit(): # Successful POST, i.e. the comment text has passed data validation. services.add_review(form.rate.data, rank, form.comment.data, username, repo.repo_instance) return redirect(url_for('movies_bp.login_home')) return render_template('Second/movie_info/play.html', title="(Play) " + movie_name, movie_name=movie_name, form=form, handler_url=url_for('movies_bp.movie_play', movie_name=movie_name, rank=rank))
def test_can_add_review(in_memory_repo): movie_id = 2 review_text = 'What a great movie!' username = '******' rating = 10 # Call the service layer to add the review movies_services.add_review(review_text=review_text, username=username, movie_id=movie_id, rating=rating, repo=in_memory_repo) # Retrieve the reviews for the movie from the repository reviews_as_dict = movies_services.get_reviews_for_movie( movie_id=movie_id, repo=in_memory_repo) # Check that the reviews include 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