def find_movie(): form = SearchForm() boolean = form.validate_on_submit() if boolean: posted_title = form.movie_title.data movie = repo.repo_instance.get_result(posted_title.title().strip()) # per_page = 15 # a = 1 # m_list = [] # m_index = [] # for m in movie: # m_list.append(m) # m_index.append(a) # a += 1 # total = len(m_list) # page = request.args.get(get_page_parameter(), type=int, default=1) # start = (page - 1) * per_page # end = start + per_page # pagination = Pagination(bs_version=3, per_page=per_page, page=page, total=total) # movie_per_page = m_list[start:end] return render_template( 'search/MovieResult.html', movies=movie, home_url=url_for('home_bp.home'), list_movie_url=url_for('movie_bp.movies_by_year'), list_actor_url=url_for('actor_bp.list_actor'), list_director_url=url_for('director_bp.list_director'), list_genre_url=url_for('genre_bp.list_genre'), search_url=url_for('search_bp.find_movie'), selected_movies=utilities.get_selected_movies(), register_url=url_for('authentication_bp.register'), # ids=m_index, # pagination=pagination ) return render_template( 'search/SearchMovie.html', home_url=url_for('home_bp.home'), list_movie_url=url_for('movie_bp.movies_by_year'), list_actor_url=url_for('actor_bp.list_actor'), list_director_url=url_for('director_bp.list_director'), list_genre_url=url_for('genre_bp.list_genre'), search_url=url_for('search_bp.find_movie'), handler_url = url_for('search_bp.find_movie'), register_url=url_for('authentication_bp.register'), selected_movies=utilities.get_selected_movies(), form=form, )
def list_director(): directors = repo.repo_instance.get_directors() per_page = 30 a = 1 director_list = [] director_index = [] for director in directors: director_list.append(director) director_index.append(a) a += 1 total = len(director_list) page = request.args.get(get_page_parameter(), type=int, default=1) start = (page - 1) * per_page end = page * per_page if total > page * per_page else total pagination = Pagination(bs_version=3, per_page=per_page, page=page, total=total) director_per_page = director_list[start:end] return render_template( 'others/list_director.html', home_url=url_for('home_bp.home'), list_movie_url=url_for('movie_bp.movies_by_year'), list_actor_url=url_for('actor_bp.list_actor'), list_director_url=url_for('director_bp.list_director'), list_genre_url=url_for('genre_bp.list_genre'), search_url=url_for('search_bp.find_movie'), register_url=url_for('authentication_bp.register'), directors=director_per_page, ids=director_index, pagination=pagination, selected_movies=utilities.get_selected_movies(), )
def review_on_movie(): username = session['username'] form = ReviewForm() if form.validate_on_submit(): movie_id = int(form.movie_id.data) #services.add_review(movie_id, username, form.review.data, repo.repo_instance) services.add_review(username, movie_id, form.review.data, repo.repo_instance) movie = services.get_movie_by_id(movie_id, repo.repo_instance) return redirect( url_for('movie_bp.movies_by_year', release_year=movie['release_year'], view_reviews_for=movie_id)) if request.method == 'GET': #movie_id = int(request.args.get('movie_id')) movie_id = int(request.args.get('movie')) form.movie_id.data = movie_id else: movie_id = int(form.movie_id.data) movie = services.get_movie_by_id(movie_id, repo.repo_instance) return render_template('movie/review_on_movie.html', title='Edit movie', movie=movie, form=form, handler_url=url_for('movie_bp.review_on_movie'), selected_movies=utilities.get_selected_movies(5), genre_urls=utilities.get_genres_and_urls())
def review_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'].date()) # 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_movie.html', title='Edit movie', movie=movie, form_review=form, form=SearchForm(), handler_url_review=url_for('movies_bp.review_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 review_on_director(): username = session['username'] form = DirectorReviewForm() if form.validate_on_submit(): director_name = form.director_name.data #services.add_review(movie_id, username, form.review.data, repo.repo_instance) services.add_director_review(username, director_name, form.review.data, repo.repo_instance) director = services.get_director(director_name, repo.repo_instance) return redirect( url_for('movie_bp.directors_list', view_director_reviews_for=director_name)) if request.method == 'GET': #movie_id = int(request.args.get('movie_id')) director_name = request.args.get('director_name') form.director_name.data = director_name else: director_name = form.director_name.data director = services.get_director(director_name, repo.repo_instance) return render_template( 'directors/review_on_director.html', director=director, form=form, handler_url=url_for('movie_bp.review_on_director'), selected_movies=utilities.get_selected_movies(5), )
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'), # handler 不确定 selected_movies=utilities.get_selected_movies(), genres_urls=utilities.get_genres_and_urls() )
def home(): return render_template( 'home/home.html', selected_movies=utilities.get_selected_movies(), year_urls=utilities.get_years_and_urls(), genre_urls=utilities.get_genres_and_urls(), rank_urls=utilities.get_rank_and_url(), )
def search_movie_by_actor(): form = SearchActorForm() if form.validate_on_submit(): actor_name = form.movie.data movies = services.get_movies_by_actor(actor_name, repo.repo_instance) for movie in movies: movie['hyperlink'] = url_for('movie_bp.movies_by_year', release_year=int( movie['release_year'])) if len(movies) == 0: movies = None return render_template( 'list_movie.html', selected_movies=utilities.get_selected_movies(5), movies=movies) return render_template('search_movie_by_actor.html', selected_movies=utilities.get_selected_movies(5), form=form)
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 review text has passed data validation. # Extract the movie rank, representing the reviewed movie, from the form. movie_rank = int(form.movie_rank.data) # Use the service layer to store the new review. services.add_review(movie_rank, form.review.data, form.rating.data, username, repo.repo_instance) # Retrieve the movie in dict form. movie = services.get_movie(movie_rank, 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.movies_by_rank', rank=movie['rank'], 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('movie')) # 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) # 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_rank, repo.repo_instance) return render_template( 'movies/review_on_movie.html', title='Review of 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(), year_urls=utilities.get_years_and_urls(), rank_urls=utilities.get_rank_and_url(), )
def search_movies(): form = SearchForm() # Retrieve movies ids for movies if form.validate_on_submit(): name = form.search_info.data movies = services.get_search_info(name, repo.repo_instance) if movies is not None: movie_ids = [] for movie in movies: movie_ids.append(int(movie['id'])) print("movies", movie_ids) return redirect(url_for('movies_bp.movies_by_search', movies=movie_ids)) else: return render_template("show_nothing.html", selected_movies=utilities.get_selected_movies() ) return render_template('search.html', search_url=url_for('movies_bp.search_movies'), form=form, selected_movies=utilities.get_selected_movies() )
def home(): return render_template( 'home/home.html', home_url=url_for('home_bp.home'), list_movie_url=url_for('movie_bp.movies_by_year'), list_actor_url=url_for('actor_bp.list_actor'), list_director_url=url_for('director_bp.list_director'), list_genre_url=url_for('genre_bp.list_genre'), search_url=url_for('search_bp.find_movie'), register_url=url_for('authentication_bp.register'), selected_movies=utilities.get_selected_movies(), )
def movies_by_search(): movies_per_page = 10 # Read query parameters. movie_ids = request.args.getlist('movies') for i in range(len(movie_ids)): movie_ids[i] = int(movie_ids[i]) # # print("movies id", movie_ids) cursor = request.args.get('cursor') 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) movies_to_show = services.get_movies_by_id(movie_ids[cursor:cursor + movies_per_page], repo.repo_instance) print(movies_to_show) 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_search', movies=movie_ids, cursor=cursor - movies_per_page) first_movie_url = url_for('movies_bp.movies_by_search', movies=movie_ids) 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_search', movies=movie_ids, 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_search', movies=movie_ids, cursor=last_cursor) return render_template( 'movies/movies.html', title='Movies', movies_title='Search Result', movies=movies_to_show, selected_movies=utilities.get_selected_movies(5), 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, )
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 movies id, when subsequently called with a HTTP POST request, the movies id remains in the # form. form = CommentForm() page = request.args.get('page') cursor = request.args.get('cursor') if form.validate_on_submit(): # Successful POST, i.e. the comment text has passed data validation. # Extract the movies id, representing the commented movies, 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 movies 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 commented movies, # and display all comments, including the new comment. if page == "genre": return redirect(url_for('movies_bp.movies_by_genre', genre=movie['genre'], view_comments_for=movie_id, cursor=cursor)) return redirect(url_for('movies_bp.movies_by_date', year=int(movie['year']), view_comments_for=movie_id)) if request.method == 'GET': # Request is a HTTP GET to display the form. # Extract the movies id, representing the movies to comment, from a query parameter of the GET request. movie_id = int(request.args.get('movie')) # Store the movies id in the form. form.movie_id.data = movie_id else: # Request is a HTTP POST where form validation has failed. # Extract the movies id of the movies being commented from the form. movie_id = int(form.movie_id.data) # For a GET or an unsuccessful POST, retrieve the movies 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/comment_on_movie.html', title='Edit movies', movie=movie, form=form, handler_url=url_for('movies_bp.comment_on_movies'), selected_movies=utilities.get_selected_movies(), genre_urls=utilities.get_genres_and_urls(), )
def search_movies_by_title(): target_title = request.args.get('title') movie_reviews = request.args.get('view_reviews_for') movies = list() if movie_reviews is None: # No view-reviews query parameter, so set to a non-existent article id. movie_reviews = -1 else: # Convert movie_reviews from string to int movie_reviews = int(movie_reviews) if target_title is None: target_title = "" try: movies = services.search_movie_by_title(title=target_title, repo=repo.repo_instance) except NonExistentException: pass first_page_url = None last_page_url = None previous_page_url = None next_page_url = None for movie in movies: movie['view_review_url'] = url_for('movies_bp.search_movies_by_title', title=target_title, view_reviews_for=movie['id']) movie['add_review_url'] = url_for('movies_bp.review_movie', movie=movie['id']) return render_template( 'movies/movies.html', title='Movies', movies_title="Search results of " + target_title + " - (" + str(len(movies)) + " results)", movies=movies, form=SearchForm(), handler_url=url_for('movies_bp.search'), selected_movies=utilities.get_selected_movies(len(movies) * 2), genre_urls=utilities.get_genres_and_urls(), first_page_url=first_page_url, last_page_url=last_page_url, previous_page_url=previous_page_url, next_page_url=next_page_url, show_reviews_for_movie=movie_reviews, title_form=SearchByTitleForm(), handler_url_title=url_for('movies_bp.search_by_title'))
def list_genre(): return render_template( 'others/list_genre.html', home_url=url_for('home_bp.home'), list_movie_url=url_for('movie_bp.movies_by_year'), list_actor_url=url_for('actor_bp.list_actor'), list_director_url=url_for('director_bp.list_director'), list_genre_url=url_for('genre_bp.list_genre'), search_url=url_for('search_bp.find_movie'), register_url=url_for('authentication_bp.register'), genres=repo.repo_instance.get_genres(), selected_movies=utilities.get_selected_movies(), genre_urls=utilities.get_genres_and_urls(), )
def suggest_movie(): # Obtain the username of the current user. username = session['username'] suggestions = services.user_recommendations_by_genre( username=username, repo=repo.repo_instance) num_of_suggested_movies = len(suggestions) if num_of_suggested_movies > 0: title_message = "Recommendations For You" movie_reviews = request.args.get('view_reviews_for') if movie_reviews is None: # No view-reviews query parameter, so set to a non-existent article id. movie_reviews = -1 else: # Convert movie_reviews from string to int movie_reviews = int(movie_reviews) first_page_url = None last_page_url = None previous_page_url = None next_page_url = None for movie in suggestions: movie['view_review_url'] = url_for('movies_bp.suggest_movie', view_reviews_for=movie['id']) movie['add_review_url'] = url_for('movies_bp.review_movie', movie=movie['id']) return render_template( "movies/movies.html", title='Movies', movies=suggestions, movies_title=title_message, form=SearchForm(), handler_url=url_for('movies_bp.search'), selected_movies=utilities.get_selected_movies(len(suggestions) * 2), genre_urls=utilities.get_genres_and_urls(), first_page_url=first_page_url, last_page_url=last_page_url, previous_page_url=previous_page_url, next_page_url=next_page_url, movie_reviews=movie_reviews, title_form=SearchByTitleForm(), handler_url_title=url_for('movies_bp.search_by_title'), )
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', home_url=url_for('home_bp.home'), list_movie_url=url_for('movie_bp.movies_by_year'), list_actor_url=url_for('actor_bp.list_actor'), list_director_url=url_for('director_bp.list_director'), list_genre_url=url_for('genre_bp.list_genre'), search_url=url_for('search_bp.find_movie'), register_url=url_for('authentication_bp.register'), title='Login', username_error_message=username_not_recognised, password_error_message=password_does_not_match_username, form=form, selected_movies=utilities.get_selected_movies(), tag_urls=url_for('genre_bp.list_genre'))
def directors_list(): # Read query parameters. #target_year = request.args.get('release_year') directors_to_show_reviews = request.args.get( 'view_reviews_for_director') # 不确定 if directors_to_show_reviews is None: directors_to_show_reviews = -1 else: directors_to_show_reviews = directors_to_show_reviews directors = services.get_all_directors(repo.repo_instance) for director in directors: director_name = director['director_name'] #print(["hhhhhhh"] + director['reviews']) dir_movies = services.get_movies_by_director(director_name, repo.repo_instance) #joined_movies_id = actor['joined_movies'] for movie in dir_movies: movie['hyperlink'] = url_for('movie_bp.movies_by_year', release_year=int( movie['release_year'])) director['dir_movies'] = dir_movies if len(directors) > 0: for director in directors: director['view_review_url'] = url_for( 'movie_bp.directors_list', view_reviews_for_director=director['director_name']) director['add_review_url'] = url_for( 'movie_bp.review_on_director', director_name=director['director_name']) return render_template( 'directors/directors.html', title='Movies', movie_title='directors', directors=directors, selected_movies=utilities.get_selected_movies(5), show_reviews_for_director=directors_to_show_reviews) return redirect(url_for('home_bp.home'))
def actors_list(): # Read query parameters. #target_year = request.args.get('release_year') actors_to_show_reviews = request.args.get('view_reviews_for_actor') # 不确定 if actors_to_show_reviews is None: actors_to_show_reviews = -1 else: actors_to_show_reviews = actors_to_show_reviews actors = services.get_all_actors(repo.repo_instance) #each_actor_joined_movie = {} for actor in actors: actor_name = actor['actor_name'] #print(actor['reviews']) joined_movies = services.get_movies_by_actor(actor_name, repo.repo_instance) #joined_movies_id = actor['joined_movies'] for movie in joined_movies: movie['hyperlink'] = url_for('movie_bp.movies_by_year', release_year=int( movie['release_year'])) actor['joined_movies'] = joined_movies #each_actor_joined_movie[actor] = joined_movies if len(actors) > 0: for actor in actors: actor['view_review_url'] = url_for( 'movie_bp.actors_list', view_reviews_for_actor=actor['actor_name']) actor['add_review_url'] = url_for('movie_bp.review_on_actor', actor_name=actor['actor_name']) return render_template( 'actors/actors.html', title='Movies', movie_title='Actors', actors=actors, selected_movies=utilities.get_selected_movies(5), show_reviews_for_actor=actors_to_show_reviews) return redirect(url_for('home_bp.home'))
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('movies_bp.suggest_movie')) except services.UnknownUserException: username_not_recognised = 'Username not recognised - please supply another' except services.AuthenticationException: password_does_not_match_username = '******' return render_template( 'authentication/credentials.html', title='Login', username_error_message=username_not_recognised, password_error_message=password_does_not_match_username, form_login=form, title_form=SearchByTitleForm(), form=SearchForm(), handler_url=url_for('movies_bp.search'), handler_url_title=url_for('movies_bp.search_by_title'), selected_movies=utilities.get_selected_movies(), genre_urls=utilities.get_genres_and_urls() )
def list_actor(): actors = repo.repo_instance.get_actors() per_page = 30 a = 1 actor_list = [] actor_index = [] for actor in actors: actor_list.append(actor) actor_index.append(a) a += 1 total = len(actor_list) page = request.args.get(get_page_parameter(), type=int, default=1) start = (page - 1) * per_page end = start + per_page pagination = Pagination(bs_version=3, per_page=per_page, page=page, total=total) actor_per_page = actor_list[start:end] # for actor in actor_per_page: # actor['view_review_url'] = url_for('movie_bp.movies_by_year', name=actor, view_reviews_for=actor['id']) # actor['add_review_url'] = url_for('movie_bp.review_on_movie', actor=actor['id']) return render_template( 'others/list_actor.html', home_url=url_for('home_bp.home'), list_movie_url=url_for('movie_bp.movies_by_year'), list_actor_url=url_for('actor_bp.list_actor'), list_director_url=url_for('director_bp.list_director'), list_genre_url=url_for('genre_bp.list_genre'), search_url=url_for('search_bp.find_movie'), register_url=url_for('authentication_bp.register'), selected_movies=utilities.get_selected_movies(), actors=actor_per_page, ids=actor_index, pagination=pagination)
def search_by_genre(): return render_template('movies/genre_search.html', genre_urls=utilities.get_genres_and_urls(), selected_movies=utilities.get_selected_movies() )
def movies_by_genre(): if 'username' not in session: username = None else: username = session['username'] id_list = [] # print("usernmae", username) if username is not None: user = services.get_user(username, repo.repo_instance) for movie in user['watch_list']: id_list.append(movie.id) else: user = None movies_per_page = 10 # Read query parameters. genre_name = request.args.get('genre') cursor = request.args.get('cursor') movies_to_show_comments = request.args.get('view_comments_for') if movies_to_show_comments is None: # No view-comments query parameter, so set to a non-existent movies id. movies_to_show_comments = -1 else: # Convert movies_to_show_comments from string to int. movies_to_show_comments = int(movies_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 movies ids for movies that are classified with genre_name. movie_ids = services.get_movie_ids_for_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) 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 movies comments and adding comments. for movie in movies: movie['view_comment_url'] = url_for('movies_bp.movies_by_genre', genre=genre_name, cursor=cursor, view_comments_for=movie['id']) movie['add_comment_url'] = url_for('movies_bp.comment_on_movies', movie=movie['id'], cursor=cursor, page='genre') # if movie not in; # print("movie id", movie['id']) if user is not None: if movie['id'] not in id_list: movie['add_to_watch_list_url'] = url_for('movies_bp.watch_list_genres', movie_id=movie['id'], genre=genre_name, cursor=cursor, show=movies_to_show_comments) else: movie['add_to_watch_list_url'] = "" movie['remove_from_watch_list_url'] = url_for('movies_bp.remove_movie_watch_list_genres', movie_id=movie['id'], genre=genre_name, cursor=cursor, show=movies_to_show_comments) else: movie['add_to_watch_list_url'] = None return render_template( 'movies/movies.html', title='Movies', movies_title='Movies are Classified by ' + genre_name, movies=movies, selected_movies=utilities.get_selected_movies(6), 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_comments_for_movies=movies_to_show_comments, id_list=id_list )
def movies_by_release_year(): # Read query parameters target_date = request.args.get('year') movie_to_show_reviews = request.args.get('view_reviews_for') # Fetch the first and the last movies in the series. first_movie = services.get_newest_movie(repo.repo_instance) last_movie = services.get_oldest_movie(repo.repo_instance) if target_date is None: # No date query parameter, so return articles from day 1 of the series. target_date = first_movie['release_year'] else: # Convert target_date to int target_date = int(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 year. This call also returns the previous and next years for movies # immediately before and after the target year movies, previous_year, next_year = services.get_movies_by_release_year( target_date, repo.repo_instance) movies_by_year = len(movies) last_page_url = url_for('movies_bp.movies_by_release_year', year=int(last_movie['release_year'])) first_page_url = url_for('movies_bp.movies_by_release_year', year=int(first_movie['release_year'])) if movies_by_year > 0: next_year = int(movies[0]['release_year']) - 1 previous_year = int(movies[0]['release_year']) + 1 next_page_url = url_for('movies_bp.movies_by_release_year', year=previous_year) previous_page_url = url_for('movies_bp.movies_by_release_year', year=next_year) # Construct urls for viewing movie reviews and adding reviews for movie in movies: movie['view_review_url'] = url_for( 'movies_bp.movies_by_release_year', year=target_date, view_reviews_for=movie['id']) movie['add_review_url'] = url_for('movies_bp.review_movie', movie=movie['id']) # Generate the webpage to display the movies return render_template( 'movies/movies.html', title='Movies', movies_title=str(target_date) + " Movies - (" + str(movies_by_year) + " results)", movies=movies, form=SearchForm(), handler_url=url_for('movies_bp.search'), selected_movies=utilities.get_selected_movies(len(movies) * 2), genre_urls=utilities.get_genres_and_urls(), first_page_url=first_page_url, last_page_url=last_page_url, previous_page_url=previous_page_url, next_page_url=next_page_url, show_reviews_for_movie=movie_to_show_reviews, title_form=SearchByTitleForm(), handler_url_title=url_for('movies_bp.search_by_title'), ) # No movies to show, so return the homepage return redirect(url_for('home_bp.home'))
def search_movies_by_actor_or_director(): # Read query parameters target_actor = request.args.get('actor') target_director = request.args.get('director') movie_reviews = request.args.get('view_reviews_for') movies = list() if movie_reviews is None: # No view-reviews query parameter, so set to a non-existent article id. movie_reviews = -1 else: # Convert movie_reviews from string to int movie_reviews = int(movie_reviews) title_message = "No results found" if target_actor is None: target_actor = "" if target_director is None: target_director = "" if len(target_director) == 0 and len(target_actor) != 0: title_message = "Movies played by " + target_actor try: movies = services.search_movie_by_actor_fullname( actor_fullname=target_actor, repo=repo.repo_instance) except NonExistentException: pass elif len(target_director) != 0 and len(target_actor) == 0: title_message = "Movies directed by " + target_director try: movies = services.search_movie_directed_by_director_fullname( director_fullname=target_director, repo=repo.repo_instance) except NonExistentException: pass first_page_url = None last_page_url = None previous_page_url = None next_page_url = None # There is at least one movie played by the target actor for movie in movies: movie['view_review_url'] = url_for( 'movies_bp.search_movies_by_actor_or_director', actor=target_actor, view_reviews_for=movie['id']) movie['add_review_url'] = url_for('movies_bp.review_movie', movie=movie['id']) return render_template( 'movies/movies.html', title='Movies', movies_title=title_message + " - (" + str(len(movies)) + " results)", movies=movies, form=SearchForm(), handler_url=url_for('movies_bp.search'), selected_movies=utilities.get_selected_movies(len(movies) * 2), genre_urls=utilities.get_genres_and_urls(), first_page_url=first_page_url, last_page_url=last_page_url, previous_page_url=previous_page_url, next_page_url=next_page_url, show_reviews_for_movie=movie_reviews, title_form=SearchForm(), handler_url_title=url_for('movies_bp.search_by_title'), )
def show_watchlist(): movies_per_page = 3 username = session['username'] # Read query parameters. cursor = request.args.get('cursor') movies_to_show_comments = request.args.get('view_comments_for') if movies_to_show_comments is None: # No view-comments query parameter, so set to a non-existent movies id. movies_to_show_comments = -1 else: # Convert movies_to_show_comments from string to int. movies_to_show_comments = int(movies_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 movies ids for movies that are classified with genre_name. # Retrieve the batch of movies to display on the Web page. watch_list = services.get_watch_list_for_user(username, repo.repo_instance) movie_ids = [] for movie in watch_list: movie_ids.append(int(movie['id'])) # 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) 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.show_watchlist', cursor=cursor - movies_per_page) first_movie_url = url_for('movies_bp.show_watchlist') 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.show_watchlist', 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.show_watchlist', cursor=last_cursor) # Construct urls for viewing movies comments and adding comments. for movie in movies: movie['view_comment_url'] = url_for('movies_bp.show_watchlist', cursor=cursor, view_comments_for=movie['id']) movie['remove_from_watch_list_url'] = url_for('movies_bp.remove_movie_watch_list_show', movie_id=movie['id'], cursor=cursor, show=movies_to_show_comments) # Generate the webpage to display the movies. return render_template( 'movies/watch_list.html', user=services.get_user(username, repo.repo_instance), title='Movies', movies_title='Watchlist for ' + username, movies=movies, selected_movies=utilities.get_selected_movies(5), 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_comments_for_movies=movies_to_show_comments )
def movies_by_genre(): movies_per_page = 10 # Read query parameter. 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 article 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: # None cursor query parameter, so initialise cursor to start the begining cursor = 0 else: # Convert cursor from string to int cursor = int(cursor) # Retrieve movie ids for movies that are classified with genre_name movie_ids = services.get_movie_ids_for_genre(genre_name, repo.repo_instance) num_of_movies_found = len(movie_ids) # Retrieve the batch of articles to display on the Web page. movies = services.get_movies_by_id( movie_ids[cursor:cursor + movies_per_page], repo.repo_instance) first_page_url = None last_page_url = None previous_page_url = None next_page_url = None if cursor > 0: # There are preceding movies, so generate URLs for the 'previous' and 'first' navigation buttons. previous_page_url = url_for('movies_bp.movies_by_genre', genre=genre_name, cursor=cursor - movies_per_page) last_page_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_page_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 first_page_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_review_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']) print(cursor) current_page = cursor if current_page + 10 < num_of_movies_found: current_page += 10 else: current_page += (num_of_movies_found - current_page) # Generate the webpage to display the movies return render_template( 'movies/movies.html', title='Movies', movies_title=genre_name + " Movies - (" + str(current_page) + " of " + str(num_of_movies_found) + " results)", movies=movies, form=SearchForm(), handler_url=url_for('movies_bp.search'), selected_movies=utilities.get_selected_movies(len(movies) * 2), genre_urls=utilities.get_genres_and_urls(), first_page_url=first_page_url, last_page_url=last_page_url, previous_page_url=previous_page_url, next_page_url=next_page_url, show_reviews_for_movie=movie_to_show_reviews, title_form=SearchByTitleForm(), handler_url_title=url_for('movies_bp.search_by_title'), )
def movies_by_year(): # Read query parameters. target_year = request.args.get('year') 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_year is None: # No year query parameter, so return movies from day 1 of the series. target_year = first_movie['year'] else: # Convert target_year from string to year. target_year = int(target_year) 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 year. This call also returns the previous and next years for movies immediately # before and after the target year. movies, previous_year, next_year = services.get_movies_by_year( target_year, 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 year. if previous_year is not None: # There are movies on a previous year, so generate URLs for the 'previous' and 'first' navigation buttons. prev_movie_url = url_for('movie_bp.movies_by_year', year=previous_year) first_movie_url = url_for('movie_bp.movies_by_year', year=2006) # There are movies on a subsequent year, so generate URLs for the 'next' and 'last' navigation buttons. if next_year is not None: next_movie_url = url_for('movie_bp.movies_by_year', year=next_year) last_movie_url = url_for('movie_bp.movies_by_year', year=last_movie['year']) # Construct urls for viewing movie reviews and adding reviews. username = session['username'] for movie in movies: movie['view_review_url'] = url_for('movie_bp.movies_by_year', year=target_year, view_reviews_for=movie['id']) movie['add_review_url'] = url_for('movie_bp.review_on_movie', movie=movie['id']) movie['add_to_watchlist_url'] = url_for( 'movie_bp.add_to_watchlist', user=username, movie=movie['id']) # Generate the webpage to display the movies. return render_template( 'movies/list_movie.html', title='movies', movies_title=target_year, movies=movies, selected_movies=utilities.get_selected_movies(6), 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_in_watchlist(): movies_per_page = 10 # Read query parameters. # genre_name = request.args.get('genre') # movie_id = int(request.args.get('movie')) username = session['username'] cursor = request.args.get('cursor') movie_to_show_reviews = request.args.get('view_reviews_for') # services.add_movie_to_watchlist(username, movie_id, repo.repo_instance) user = repo.repo_instance.get_user(username) # print("this is movie id", movie_id) # print(user.watched_movies) # print("user",user) 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 genreged with genre_name. movies_list = services.get_watchlist_for_user(username, repo.repo_instance) movie_ids = services.get_movie_ids(movies_list) # Movie Dict # movies = services.get_movies_by_id(movie_ids,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) 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('movie_bp.movies_in_watchlist', user=username, cursor=cursor - movies_per_page) first_movie_url = url_for('movie_bp.movies_in_watchlist', user=username) 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('movie_bp.movies_in_watchlist', user=username, 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('movie_bp.movies_in_watchlist', user=username, cursor=last_cursor) # Construct urls for viewing movie reviews and adding reviews. for movie in movies: movie['view_review_url'] = url_for('movie_bp.movies_in_watchlist', user=username, cursor=cursor, view_reviews_for=movie['id']) movie['add_review_url'] = url_for('movie_bp.review_on_movie', movie=movie['id']) # Generate the webpage to display the movies. return render_template( 'movies/list_movie.html', title='My favorite movie', movies_title='My favorite movie', movies=movies, selected_movies=utilities.get_selected_movies(6), 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, )
def search_by_year(): return render_template('movies/year_search.html', year_urls=utilities.get_year_and_urls(), selected_movies=utilities.get_selected_movies() )