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 article 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, 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 year as the reviewed movie, # and display all reviews, including the new review. return redirect(url_for('movies_bp.movies_by_year', 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 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_on_movie.html', title='Review movie', movie=movie, form=form, handler_url=url_for('movies_bp.review_on_movie'), selected_movies=utilities.get_selected_movies(), genre_urls=utilities.get_genres_and_urls(), username=username )
def test_can_add_review(in_memory_repo): movie_id = 3 review_text = 'Really Enjoyableeeee!!' user = User('fmercury', 'ASdwdc9') # Call the service layer to add the review. movies_services.add_review(movie_id, review_text, user, in_memory_repo) # Retrieve the reviews for the movie from the repository. reviews_as_dict = movies_services.get_reviews_for_movie( movie_id, 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
def test_get_reviews_for_movie(in_memory_repo): #add a review to movie id 1 movie_id = 1 review_text = 'Really Enjoyableeeee!!' user = User('fmercury', 'ASdwdc9') # Call the service layer to add the review. movies_services.add_review(movie_id, review_text, user, in_memory_repo) reviews_as_dict = movies_services.get_reviews_for_movie(1, in_memory_repo) # Check that 1 review were returned for movie with id 1. assert len(reviews_as_dict) == 1 # Check that the reviews relate to the movie whose id is 1. review_ids = [review['movie_id'] for review in reviews_as_dict] review_ids = set(review_ids) assert 1 in review_ids and len(review_ids) == 1