def home(): return render_template( 'home.html', random_movies=utilities.get_random_movies(), common_director_urls=utilities.get_most_common_directors_and_urls(), common_actor_urls=utilities.get_most_common_actors_and_urls(), common_genre_urls=utilities.get_most_common_genres_and_urls() )
def register(): form = RegistrationForm() username_not_unique = None if form.validate_on_submit(): # Successful POST results if the username and password have passed validation checking. # Using the service layer to attempt to add the new user. try: services.add_user(form.username.data, form.password.data, repo.repo_instance) # If there are no issues, the user is redirected to the login page. return redirect(url_for('authentication_bp.login')) except services.NameNotUniqueException: username_not_unique = 'Sorry that username is already taken. Please enter another.' # For a GET or a failed POST request, return the Registration Web page. return render_template('authentication/credentials.html', title='Register', form=form, username_error_message=username_not_unique, handler_url=url_for('authentication_bp.register'), random_movies=utilities.get_random_movies())
def login(): form = LoginForm() username_not_recognised = None password_does_not_match_username = None if form.validate_on_submit(): # Successful POST results if the username and password have passed validation checking. # Using the service layer to lookup the user. try: user = services.get_user(form.username.data, repo.repo_instance) # Authenticate user. services.authenticate_user(user.user_name, form.password.data, repo.repo_instance) # Initialise session and redirect the user to the home page. session.clear() session['username'] = user.user_name return redirect(url_for('home_bp.home')) except services.UnknownUserException: # Username not known to the system, set a suitable error message. username_not_recognised = 'Sorry that username not recognised. Please enter another.' except services.AuthenticationException: # Authentication failed, set a suitable error message. password_does_not_match_username = '******' \ 'Please check and try again.' # For a GET or a failed POST, return the Login Web page. return render_template( 'authentication/credentials.html', title='Login', username_error_message=username_not_recognised, password_error_message=password_does_not_match_username, form=form, selected_articles=utilities.get_random_movies())
def search_for_movies(): search_form = MovieSearchForm(request.form) if request.method == 'POST': choice = search_form.select.data.lower() if 'director' in choice: director_full_name = search_form.data['search'] return redirect( url_for('movie_bp.movies_by_director', director=director_full_name)) elif 'actor' in choice: actor_full_names_string = '/'.join([ name.strip() for name in search_form.data['search'].split(',') ]) return redirect( url_for('movie_bp.movies_by_actors', actors=actor_full_names_string)) elif 'genre' in choice: genre_names_string = '/'.join([ name.strip() for name in search_form.data['search'].split(',') ]) return redirect( url_for('movie_bp.movies_by_genres', genres=genre_names_string)) else: flash('No movies found with the given attributes!') return redirect('/search_for_movies') return render_template('movies/search_for_movies.html', form=search_form, handler_url=url_for('movie_bp.search_for_movies'), random_movies=utilities.get_random_movies())
def movies_by_director(): movies_per_page = 3 # Read query parameters director_full_name = request.args.get('director') cursor = request.args.get('cursor') movie_to_show_reviews = request.args.get('view_reviews_for') try: # Try converting movie_to_show_reviews from string to int. movie_to_show_reviews = int(movie_to_show_reviews) except TypeError: # No view_reviews_for query parameter, so set to a non-existent movie rank. movie_to_show_reviews = -1 try: # Try converting cursor from string to int. cursor = int(cursor) except TypeError: # No cursor query parameter, so initialise cursor to start at the beginning. cursor = 0 # Retrieve Movie ranks for Movies with the given director. try: director = services.get_director(director_full_name, repo.repo_instance) except services.ServicesException: director = None movie_ranks = services.get_movie_ranks_by_director(director, repo.repo_instance) # Retrieve only a batch of Movies to display on the Web page movies = services.get_movies_by_rank( movie_ranks[cursor:cursor + movies_per_page], repo.repo_instance) first_movie_url = None last_movie_url = None next_movie_url = None prev_movie_url = None director_urls = dict() actor_urls = dict() genre_urls = dict() view_review_urls = dict() add_review_urls = dict() add_watchlist_urls = dict() image_urls = dict() director_urls[director_full_name] = url_for('movie_bp.movies_by_director', director=director_full_name) for movie in movies: actor_urls.update(utilities.get_actor_urls_for_movie(movie)) genre_urls.update(utilities.get_genre_urls_for_movie(movie)) view_review_urls[movie.rank] = url_for('movie_bp.movies_by_director', director=director_full_name, cursor=cursor, view_reviews_for=movie.rank) add_review_urls[movie.rank] = url_for('movie_bp.create_movie_review', add_review_for=movie.rank) add_watchlist_urls[movie.rank] = url_for( 'user_activity_bp.browse_watchlist', movie=movie.rank) image_urls[movie.rank] = utilities.get_image_url_for_movie(movie.title) if cursor > 0: # There are preceding Movies, so generate URLs for the 'previous' and 'first' navigation buttons. prev_movie_url = url_for('movie_bp.movies_by_director', director=director_full_name, cursor=cursor - movies_per_page) first_movie_url = url_for('movie_bp.movies_by_director', director=director_full_name) if cursor + movies_per_page < len(movie_ranks): # There are further Movies, so generate URLs for the 'next' and 'last' navigation buttons. next_movie_url = url_for('movie_bp.movies_by_director', director=director_full_name, cursor=cursor + movies_per_page) last_cursor = movies_per_page * int(len(movie_ranks) / movies_per_page) if len(movie_ranks) % movies_per_page == 0: last_cursor -= movies_per_page last_movie_url = url_for('movie_bp.movies_by_director', director=director_full_name, cursor=last_cursor) # Generate the webpage to display the Movies. return render_template( 'movies/movies.html', title='Movies', movies_title='Movies directed by ' + director_full_name, movies=movies, random_movies=utilities.get_random_movies(len(movies) * 2), first_movie_url=first_movie_url, last_movie_url=last_movie_url, prev_movie_url=prev_movie_url, next_movie_url=next_movie_url, director_urls=director_urls, actor_urls=actor_urls, genre_urls=genre_urls, view_review_urls=view_review_urls, add_review_urls=add_review_urls, show_reviews_for_movie=movie_to_show_reviews, image_urls=image_urls, add_watchlist_urls=add_watchlist_urls)
def create_movie_review(): # Obtain the username of the currently logged in user. username = session['username'] review_id = request.args.get('review') form = ReviewForm() if review_id is not None: try: review_id = int(review_id) review = user_services.get_review(review_id, repo.repo_instance) form.movie_rank.data = review.movie.rank form.review_text.data = review.review_text form.rating.data = review.rating services.remove_review(review) except user_services.ServicesException: pass # Ignore exception and don't edit review if form.validate_on_submit(): # Successful POST results if the review text and rating has passed data validation. # Extract the Movie rank, representing the reviewed Movie, from the form. movie_rank = int(form.movie_rank.data) # Using the service layer to create and store the new review. services.create_review(movie_rank, form.review_text.data, form.rating.data, username, repo.repo_instance) # Retrieve the Movie that was reviewed. movie: Movie = services.get_movies_by_rank([movie_rank], repo.repo_instance)[0] return redirect( url_for('movie_bp.movies_by_director', director=movie.director.director_full_name, view_reviews_for=movie.rank)) if request.method == 'GET': # Request is a HTTP GET to display the form. # Extract the Movie rank, representing the Movie to review, from a query parameter of the GET request. movie_rank = int(request.args.get('add_review_for')) # Store the Movie rank in the form. form.movie_rank.data = movie_rank else: # Request is a HTTP POST where form validation has failed. # Extract the Movie rank of the Movie being reviewed from the form. movie_rank = int(form.movie_rank.data) movie: Movie = services.get_movies_by_rank([movie_rank], repo.repo_instance)[0] director_urls = dict() actor_urls = dict() genre_urls = dict() image_urls = dict() director_full_name = movie.director.director_full_name director_urls[director_full_name] = url_for('movie_bp.movies_by_director', director=director_full_name) actor_urls.update(utilities.get_actor_urls_for_movie(movie)) genre_urls.update(utilities.get_genre_urls_for_movie(movie)) image_urls[movie.rank] = utilities.get_image_url_for_movie(movie.title) return render_template('movies/create_movie_review.html', title='Create review', movie=movie, form=form, handler_url=url_for('movie_bp.create_movie_review'), random_movies=utilities.get_random_movies(), director_urls=director_urls, actor_urls=actor_urls, genre_urls=genre_urls, image_urls=image_urls)