def register(): form = RegistrationForm() username_not_unique = None if form.validate_on_submit(): # Successful POST, i.e. the username and password have passed validation checking. # Use the service layer to attempt to add the new user. try: services.add_user(form.username.data, form.password.data, repo.repo_instance) # All is well, redirect the user to the login page. return redirect(url_for('authentication_bp.login')) except services.NameNotUniqueException: username_not_unique = 'Your username is already taken - please supply 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'), selected_movies=utilities.get_selected_movies(), genre_urls=utilities.get_genres_and_urls() )
def home(): return render_template( 'home/home.html', genre_urls=utilities.get_genres_and_urls(), year_urls=utilities.get_year_and_urls(), director_urls=utilities.get_directors_and_urls(), actor_urls=utilities.get_actors_and_urls(), home_movies=utilities.get_selected_movies(6), )
def browse_genres(): username = None try: username = session['username'] except: pass recommendations = utilities.get_recommendations(username) return render_template('movies/browse_genres.html', genre_urls=utilities.get_genres_and_urls(), selected_movies=utilities.get_selected_movies(), recommendations=recommendations)
def stats(): username = session['username'] recommendations = utilities.get_recommendations(username) user = utilities.get_user_obj(username) for movie in user.watched_movies: movie['display_movie_url'] = url_for('movies_bp.display_movie', movie=movie['id']) return render_template('stats/stats.html', selected_movies=utilities.get_selected_movies(), genre_urls=utilities.get_genres_and_urls(), recommendations=recommendations, user=user)
def comment_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 rank, when subsequently called with a HTTP POST request, the movie rank remains in the # form. form = CommentForm() if form.validate_on_submit(): # Successful POST, i.e. the comment text has passed data validation. # Extract the movie rank, representing the commented movie, from the form. movie_id = int(form.movie_id.data) # Use the service layer to store the new comment. services.add_comment(movie_id, form.comment.data, username, repo.repo_instance) # Retrieve the movie in dict form. movie = services.get_movie(movie_id, repo.repo_instance) # Retrieve rating from user. rating = form.rating.data services.set_rating(movie_id, int(rating), username, repo.repo_instance) # Cause the web browser to display the page of all movies that have the same date as the commented movie, # and display all comments, including the new comment. return redirect(url_for('feed_bp.movies_by_year', date=movie['date'], view_comments_for=movie_id)) if request.method == 'GET': # Request is a HTTP GET to display the form. # Extract the movie rank, representing the movie to comment, from a query parameter of the GET request. movie_id = int(request.args.get('movie')) # Store the movie rank in the form. form.movie_id.data = movie_id else: # Request is a HTTP POST where form validation has failed. # Extract the movie rank of the movie being commented from the form. movie_id = int(form.movie_id.data) # For a GET or an unsuccessful POST, retrieve the movie 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( 'feed/comment_on_movie.html', title='Edit movie', movie=movie, form=form, handler_url=url_for('feed_bp.comment_on_movie'), selected_movies=utilities.get_selected_movies(), genre_urls=utilities.get_genres_and_urls() )
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 home(): movies = utilities.get_selected_movies() for list_movie in movies: list_movie['url'] = url_for('movies_bp.movie', title=list_movie['title'], release_year=list_movie['release_year']) list_movie['review_url'] = url_for( 'movies_bp.review_movie', title=list_movie['title'], release_year=list_movie['release_year']) return render_template('home/home.html', selected_movies=movies, watchlist_empty=utilities.get_watchlist_empty())
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 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 movies_by_search(): username = None try: username = session['username'] except: pass recommendations = utilities.get_recommendations(username) search_query = request.args.get('search_query') search_type = request.args.get('search_type') movie_to_show_reviews = request.args.get('view_reviews_for') if movie_to_show_reviews is None: # No view-reviews query parameter, so set to a non-existent movie id. movie_to_show_reviews = -1 else: # Convert movie_to_show_reviews from string to int. movie_to_show_reviews = int(movie_to_show_reviews) movies = [] if search_type == "title": # movies = list of dicts movies, previous_title, next_title = services.get_movies_by_title( search_query, repo.repo_instance) elif search_type == "director": movies = services.get_movies_by_director(search_query, repo.repo_instance) elif search_type == "actor": movies = services.get_movies_by_actor(search_query, repo.repo_instance) for movie in movies: movie['display_movie_url'] = url_for('movies_bp.display_movie', movie=movie['id']) return render_template( "movies/search_results.html", search_query=search_query, search_type=search_type, movies=movies, selected_movies=utilities.get_selected_movies(len(movies) + 2), recommendations=recommendations, genre_urls=utilities.get_genres_and_urls(), )
def search_by_actor(): username = None try: username = session['username'] except: pass recommendations = utilities.get_recommendations(username) form = SearchForm() search_invalid = None if form.validate_on_submit(): if services.check_if_actor_valid(form.search.data, repo.repo_instance): return redirect(url_for('movies_bp.movies_by_search', search_query=form.search.data, search_type="actor")) else: search_invalid = "Your search doesn't match any results" return render_template("search/make_search.html", title="Search by actor", search_invalid_message=search_invalid, form=form, selected_movies=utilities.get_selected_movies(3), recommendations=recommendations )
def login(): form = LoginForm() username_not_recognised = None password_does_not_match_username = None if form.validate_on_submit(): # Successful POST, i.e. the username and password have passed validation checking. # Use the service layer to lookup the user. try: user = services.get_user(form.username.data, repo.repo_instance) # Authenticate user. services.authenticate_user(user['username'], form.password.data, repo.repo_instance) # Initialise session and redirect the user to the home page. session.clear() session['username'] = user['username'] return redirect(url_for('home_bp.home')) except services.UnknownUserException: # Username not known to the system, set a suitable error message. username_not_recognised = 'Username not recognised - please supply another' except services.AuthenticationException: # Authentication failed, set a suitable error message. password_does_not_match_username = '******' # 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_movies=utilities.get_selected_movies(), genre_urls=utilities.get_genres_and_urls() )
def movies_by_genre(): movies_per_page = 4 # Read query parameters. genre_name = request.args.get('genre') cursor = request.args.get('cursor') movie_to_show_comments = request.args.get('view_comments_for') if movie_to_show_comments is None: # No view-comments query parameter, so set to a non-existent movie rank. movie_to_show_comments = -1 else: # Convert movie_to_show_comments from string to int. movie_to_show_comments = int(movie_to_show_comments) 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) # Retrieve movie ids for movies that are genreged with genre_name. movie_ranks = services.get_movie_ranks_for_genre(genre_name, repo.repo_instance) # Retrieve the 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 if cursor > 0: # There are preceding movies, so generate URLs for the 'previous' and 'first' navigation buttons. prev_movie_url = url_for('feed_bp.movies_by_genre', genre=genre_name, cursor=cursor - movies_per_page) first_movie_url = url_for('feed_bp.movies_by_genre', genre=genre_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('feed_bp.movies_by_genre', genre=genre_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('feed_bp.movies_by_genre', genre=genre_name, cursor=last_cursor) # Construct urls for viewing movie comments and adding comments. for movie in movies: movie['view_comment_url'] = url_for('feed_bp.movies_by_genre', genre=genre_name, cursor=cursor, view_comments_for=movie['rank']) movie['add_comment_url'] = url_for('feed_bp.comment_on_movie', movie=movie['rank']) # Generate the webpage to display the movies. return render_template( 'feed/movies.html', title='Movies', movies_title=genre_name + ' movies', movies=movies, selected_movies=utilities.get_selected_movies(len(movies) * 2), genre_urls=utilities.get_genres_and_urls(), year_urls=utilities.get_year_and_urls(), director_urls=utilities.get_directors_and_urls(), actor_urls=utilities.get_actors_and_urls(), first_movie_url=first_movie_url, last_movie_url=last_movie_url, prev_movie_url=prev_movie_url, next_movie_url=next_movie_url, show_comments_for_movie=movie_to_show_comments )
def home(): return render_template('home/home.html', selected_movies=utilities.get_selected_movies(), tag_urls=utilities.get_tags_and_urls())
def movies_by_title(): username = None try: username = session['username'] except: pass recommendations = utilities.get_recommendations(username) # Read query parameters. target_title = request.args.get('title') movie_to_show_reviews = request.args.get('view_reviews_for') # Fetch the first and last movies in the series. first_movie = services.get_first_movie(repo.repo_instance) last_movie = services.get_last_movie(repo.repo_instance) if target_title is None: # No title query parameter, so return first movie of the series. target_title = first_movie['title'] if movie_to_show_reviews is None: # No view-reviews query parameter, so set to a non-existent movie id. movie_to_show_reviews = -1 else: # Convert movie_to_show_reviews from string to int. movie_to_show_reviews = int(movie_to_show_reviews) # Fetch movie(s) for the target title. This call also returns the previous and next titles for movies immediately # before and after the target title. movies, previous_title, next_title = services.get_movies_by_exact_title( target_title, repo.repo_instance) first_movie_url = None last_movie_url = None next_movie_url = None prev_movie_url = None if len(movies) > 0: # There's at least one movie for the target title. if previous_title is not None: # There are movies for previous title, so generate URLs for the 'previous' and 'first' navigation buttons. prev_movie_url = url_for('movies_bp.movies_by_title', title=previous_title) first_movie_url = url_for('movies_bp.movies_by_title', title=first_movie['title']) if next_title is not None: # There are movies for subsequent title, so generate URLs for the 'next' and 'last' navigation buttons. next_movie_url = url_for('movies_bp.movies_by_title', title=next_title) last_movie_url = url_for('movies_bp.movies_by_title', title=last_movie['title']) # Construct urls for viewing movie reviews and adding reviews. for movie in movies: movie['view_reviews_url'] = url_for('movies_bp.movies_by_title', title=target_title, view_reviews_for=movie['id']) movie['add_review_url'] = url_for('movies_bp.review_movie', movie=movie['id']) movie['display_movie_url'] = url_for('movies_bp.display_movie', movie=movie['id']) # Generate the webpage to display the movies. return render_template( 'movies/movies.html', title='Movies', movies_title=target_title.title(), movies=movies, selected_movies=utilities.get_selected_movies(len(movies) + 2), genre_urls=utilities.get_genres_and_urls(), first_movie_url=first_movie_url, last_movie_url=last_movie_url, prev_movie_url=prev_movie_url, next_movie_url=next_movie_url, show_reviews_for_movie=movie_to_show_reviews, recommendations=recommendations) # No movies to show, so return the homepage. return redirect(url_for('home_bp.home'))
def movies_by_date(): # Read query parameters. target_date = request.args.get('date') movie_to_show_reviews = request.args.get('view_reviews_for') # Fetch the first and last movies in the series. first_movie = services.get_first_movie(repo.repo_instance) last_movie = services.get_last_movie(repo.repo_instance) if target_date is None: # No date query parameter, so return movies from day 1 of the series. target_date = first_movie['date'] else: # Convert target_date from string to date. target_date = date.fromisoformat(target_date) if movie_to_show_reviews is None: # No view-reviews query parameter, so set to a non-existent movie id. movie_to_show_reviews = -1 else: # Convert movie_to_show_reviews from string to int. movie_to_show_reviews = int(movie_to_show_reviews) # Fetch movie(s) for the target date. This call also returns the previous and next dates for movies immediately # before and after the target date. movies, previous_date, next_date = services.get_movies_by_date(target_date, repo.repo_instance) first_movie_url = None last_movie_url = None next_movie_url = None prev_movie_url = None if len(movies) > 0: # There's at least one movie for the target date. if previous_date is not None: # There are movies on a previous date, so generate URLs for the 'previous' and 'first' navigation buttons. prev_movie_url = url_for('news_bp.movies_by_date', date=previous_date.isoformat()) first_movie_url = url_for('news_bp.movies_by_date', date=first_movie['date'].isoformat()) # There are movies on a subsequent date, so generate URLs for the 'next' and 'last' navigation buttons. if next_date is not None: next_movie_url = url_for('news_bp.movies_by_date', date=next_date.isoformat()) last_movie_url = url_for('news_bp.movies_by_date', date=last_movie['date'].isoformat()) # Construct urls for viewing movie reviews and adding reviews. for movie in movies: movie['view_review_url'] = url_for('news_bp.movies_by_date', date=target_date, view_reviews_for=movie['id']) movie['add_review_url'] = url_for('news_bp.review_on_movie', movie=movie['id']) # Generate the webpage to display the movies. return render_template( 'news/movies.html', title='Movies', movies_title=target_date.strftime('%A %B %e %Y'), movies=movies, selected_movies=utilities.get_selected_movies(len(movies) * 2), genre_urls=utilities.get_genres_and_urls(), first_movie_url=first_movie_url, last_movie_url=last_movie_url, prev_movie_url=prev_movie_url, next_movie_url=next_movie_url, show_reviews_for_movie=movie_to_show_reviews ) # No movies to show, so return the homepage. return redirect(url_for('home_bp.home'))
def movies_by_genre(): username = None try: username = session['username'] except: pass recommendations = utilities.get_recommendations(username) movies_per_page = 3 # Read query parameters. genre_name = request.args.get('genre') cursor = request.args.get('cursor') movie_to_show_reviews = request.args.get('view_reviews_for') if movie_to_show_reviews is None: # No view-reviews query parameter, so set to a non-existent movie id. movie_to_show_reviews = -1 else: # Convert movie_to_show_reviews from string to int. movie_to_show_reviews = int(movie_to_show_reviews) 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) # Retrieve movie ids for movies that are tagged with genre_name. movie_ids = services.get_movie_ids_for_genre(Genre(genre_name), repo.repo_instance) # Retrieve the batch of movies to display on the Web page. movies = services.get_movies_by_id( movie_ids[cursor:cursor + movies_per_page], repo.repo_instance) # list of movies as dicts first_movie_url = None last_movie_url = None next_movie_url = None prev_movie_url = None if cursor > 0: # There are preceding movies, so generate URLs for the 'previous' and 'first' navigation buttons. prev_movie_url = url_for('movies_bp.movies_by_genre', genre=genre_name, cursor=cursor - movies_per_page) first_movie_url = url_for('movies_bp.movies_by_genre', genre=genre_name) if cursor + movies_per_page < len(movie_ids): # There are further movies, so generate URLs for the 'next' and 'last' navigation buttons. next_movie_url = url_for('movies_bp.movies_by_genre', genre=genre_name, cursor=cursor + movies_per_page) last_cursor = movies_per_page * int(len(movie_ids) / movies_per_page) if len(movie_ids) % movies_per_page == 0: last_cursor -= movies_per_page last_movie_url = url_for('movies_bp.movies_by_genre', genre=genre_name, cursor=last_cursor) # Construct urls for viewing movie reviews and adding reviews. for movie in movies: movie['view_reviews_url'] = url_for('movies_bp.movies_by_genre', genre=genre_name, cursor=cursor, view_reviews_for=movie['id']) movie['add_review_url'] = url_for('movies_bp.review_movie', movie=movie['id']) movie['display_movie_url'] = url_for('movies_bp.display_movie', movie=movie['id']) # Generate the webpage to display the movies. return render_template('movies/movies.html', title='Movies', movies_title='Movies of genre ' + genre_name, movies=movies, selected_movies=utilities.get_selected_movies( len(movies) * 2), genre_urls=utilities.get_genres_and_urls(), first_movie_url=first_movie_url, last_movie_url=last_movie_url, prev_movie_url=prev_movie_url, next_movie_url=next_movie_url, show_reviews_for_movie=movie_to_show_reviews, recommendations=recommendations)