Beispiel #1
0
def comment_on_article():
    # 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 = CommentForm()

    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.
        title = form.title.data

        # Use the service layer to store the new comment.
        services.add_comment(title, form.comment.data, username,
                             repo.repo_instance)
        print(form.comment.data)

        # Retrieve the article in dict form.
        article = services.get_movie(title, repo.repo_instance)
        article["comments"].append(form.comment.data)
        tag_name = article['genres'][0].genre_name

        # 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('news_bp.articles_by_tag',
                    tag=tag_name,
                    tagview_comments_for=title))

    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.
        title = request.args.get('article')

        # Store the article id in the form.
        form.title.data = title
    else:
        # Request is a HTTP POST where form validation has failed.
        # Extract the article id of the article being commented from the form.
        title = form.title.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.
    article = services.get_movie(title, repo.repo_instance)
    return render_template('news/comment_on_article.html',
                           title='Edit article',
                           article=article,
                           form=form,
                           handler_url=url_for('news_bp.comment_on_article'),
                           selected_articles=utilities.get_selected_movies(),
                           tag_urls=utilities.get_genres_and_urls())
Beispiel #2
0
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 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 date as the reviewed movie,
        # and display all reviews, including the new review.
        return redirect(
            url_for('news_bp.movies_by_date',
                    date=movie['date'],
                    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('news/review_on_movie.html',
                           title='Edit movie',
                           movie=movie,
                           form=form,
                           handler_url=url_for('news_bp.review_on_movie'),
                           selected_movies=utilities.get_selected_movies(),
                           genre_urls=utilities.get_genres_and_urls())
Beispiel #3
0
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'),
                           genre_urls=utilities.get_genres_and_urls())
Beispiel #4
0
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_articles=utilities.get_selected_movies(),
        tag_urls=utilities.get_genres_and_urls())
Beispiel #5
0
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_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 genreged 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('news_bp.movies_by_genre',
                                 genre=genre_name,
                                 cursor=cursor - movies_per_page)
        first_movie_url = url_for('news_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('news_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('news_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('news_bp.movies_by_genre',
                                           genre=genre_name,
                                           cursor=cursor,
                                           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='Movies genreged by ' + 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)
Beispiel #6
0
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'))
Beispiel #7
0
def home():
    return render_template(
        'home/home.html',
        selected_articles=utilities.get_selected_movies(),
        tag_urls=utilities.get_genres_and_urls()
    )
Beispiel #8
0
def articles_by_tag():
    articles_per_page = 3

    # Read query parameters.
    tag_name = request.args.get('tag')
    cursor = request.args.get('cursor')
    article_to_show_comments = request.args.get('view_comments_for')

    if article_to_show_comments is None:
        # No view-comments query parameter, so set to a non-existent article id.
        article_to_show_comments = -1
    else:
        # Convert article_to_show_comments from string to int.
        # article_to_show_comments = article_to_show_comments
        pass

    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 article ids for articles that are tagged with tag_name.
    movies = services.get_movie_for_genre(tag_name, repo.repo_instance)
    movies = services.movies_to_dict(movies)

    articles = movies[cursor:cursor + articles_per_page]

    first_article_url = None
    last_article_url = None
    next_article_url = None
    prev_article_url = None

    if cursor > 0:
        # There are preceding articles, so generate URLs for the 'previous' and 'first' navigation buttons.
        prev_article_url = url_for('news_bp.articles_by_tag',
                                   tag=tag_name,
                                   cursor=cursor - articles_per_page)
        first_article_url = url_for('news_bp.articles_by_tag', tag=tag_name)

    if cursor + articles_per_page < len(movies):
        # There are further articles, so generate URLs for the 'next' and 'last' navigation buttons.
        next_article_url = url_for('news_bp.articles_by_tag',
                                   tag=tag_name,
                                   cursor=cursor + articles_per_page)

        last_cursor = articles_per_page * int(len(movies) / articles_per_page)
        if len(movies) % articles_per_page == 0:
            last_cursor -= articles_per_page
        last_article_url = url_for('news_bp.articles_by_tag',
                                   tag=tag_name,
                                   cursor=last_cursor)

    for article in articles:
        article['view_comment_url'] = url_for(
            'news_bp.articles_by_tag',
            tag=tag_name,
            cursor=cursor,
            view_comments_for=article['title'])
        article['add_comment_url'] = url_for('news_bp.comment_on_article',
                                             article=article['title'])

    # Generate the webpage to display the articles.
    return render_template('news/articles.html',
                           title='Articles',
                           articles_title='Movies genred by ' + tag_name,
                           articles=articles,
                           selected_articles=utilities.get_selected_movies(
                               len(articles) * 2),
                           tag_urls=utilities.get_genres_and_urls(),
                           first_article_url=first_article_url,
                           last_article_url=last_article_url,
                           prev_article_url=prev_article_url,
                           next_article_url=next_article_url,
                           show_comments_for_article=article_to_show_comments)
Beispiel #9
0
def movies_by_genre():
    movies_per_page = 35

    # Read query parameters.
    if 'g' not in request.args:
        genre_name = 'all'
    else:
        genre_name = request.args.get('g')
    s = request.args.get('s')
    if 'page' not in request.args:
        page = 0
    else:
        page = int(request.args.get('page'))
    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 page is None:
    #     # No page query parameter, so initialise page to start at the beginning.
    #     page = 0
    # else:
    #     # Convert page from string to int.
    #     page = int(page)

    # Retrieve movie ids for movies that are genreged with genre_name.
    movie_ids = services.get_movie_ids_for_genre(s, genre_name,
                                                 repo.repo_instance)
    # print(movie_ids)
    # Retrieve the batch of movies to display on the Web page.

    # print(movie_ids[page+movies_per_page:movies_per_page + movies_per_page])
    # movies = services.get_movies_by_id(movie_ids[page*movies_per_page:movies_per_page + movies_per_page], repo.repo_instance)
    movies = services.get_movies_by_id(
        movie_ids[page * movies_per_page:page * movies_per_page +
                  movies_per_page], repo.repo_instance)
    # print(movies)
    first_movie_url = None
    last_movie_url = None
    next_movie_url = None
    prev_movie_url = None

    if page > 0:
        # There are preceding movies, so generate URLs for the 'previous' and 'first' navigation buttons.
        prev_movie_url = url_for('home_bp.movies_by_genre',
                                 s=request.args.get('s'),
                                 g=genre_name,
                                 page=page - 1,
                                 id=request.args.get('id'))
        first_movie_url = url_for('home_bp.movies_by_genre',
                                  s=request.args.get('s'),
                                  g=genre_name,
                                  id=request.args.get('id'))

    if (page + 1) * 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('home_bp.movies_by_genre',
                                 s=request.args.get('s'),
                                 g=genre_name,
                                 page=page + 1,
                                 id=request.args.get('id'))

        last_page = int(len(movie_ids) / movies_per_page)
        if len(movie_ids) % movies_per_page == 0:
            last_page -= movies_per_page
        last_movie_url = url_for('home_bp.movies_by_genre',
                                 s=request.args.get('s'),
                                 g=genre_name,
                                 page=last_page,
                                 id=request.args.get('id'))

    # Construct urls for viewing movie reviews and adding reviews.
    for movie in movies:
        movie['view_review_url'] = url_for('home_bp.movies_by_genre',
                                           s=request.args.get('s'),
                                           g=genre_name,
                                           page=page,
                                           view_reviews_for=movie['id'])
        movie['add_review_url'] = url_for('home_bp.review_on_movie',
                                          movie=movie['id'])
    t = random.randint(1, 1000)
    if 'id' not in request.args:
        t = [random.randint(1, 1000)]
        choice = services.get_movie_by_id(t, repo.repo_instance)
    else:
        choice = services.get_movie_by_id([int(request.args.get('id'))],
                                          repo.repo_instance)
        t = int(request.args.get('id'))
    choice['add_review_url'] = url_for('home_bp.review_on_movie', movie=t)
    similar = like(
        choice,
        services.get_movie_by_id_similar([i for i in range(1000)],
                                         repo.repo_instance), 3)
    return render_template('home/home.html',
                           genre_urls=utilities.get_genres_and_urls(),
                           selected=choice,
                           year_urls=utilities.get_year_and_urls(),
                           genre=genre_name,
                           movies=movies,
                           first_movie_url=first_movie_url,
                           last_movie_url=last_movie_url,
                           prev_movie_url=prev_movie_url,
                           next_movie_url=next_movie_url,
                           similar=random.choices(similar, k=6),
                           show_reviews_for_movie=movie_to_show_reviews)
Beispiel #10
0
def home():
    # return redirect("/m?page=0", code=302)
    return render_template('front.html',
                           genre_urls=utilities.get_genres_and_urls())