<commit_before>"""Movie Ratings."""

from jinja2 import StrictUndefined

from flask import Flask, render_template, redirect, request, flash, session
from flask_debugtoolbar import DebugToolbarExtension

from model import User, Rating, Movie, connect_to_db, db


app = Flask(__name__)

# Required to use Flask sessions and the debug toolbar
app.secret_key = "ABC"

# Normally, if you use an undefined variable in Jinja2, it fails silently.
# This is horrible. Fix this so that, instead, it raises an error.
app.jinja_env.undefined = StrictUndefined


@app.route("/")
def index():
    """Homepage."""

    return render_template("homepage.html")


@app.route("/users")
def user_list():
    """Show list of users."""

    users = User.query.all()

    return render_template("user_list.html", users=users)


@app.route("/users/<int:user_id>")
def show_user_details(user_id):
    """Show user details."""

    user = User.query.get(user_id)

    return render_template("user_details.html",
                           user=user)


@app.route("/movies")
def movie_list():
    """Show list of movies."""

    movies = Movie.query.order_by(Movie.title).all()

    return render_template("movie_list.html", movies=movies)


@app.route("/movies/<int:movie_id>")
def show_movie_details(movie_id):
    """Show movie details."""

    movie = Movie.query.get(movie_id)

    return render_template("movie_details.html",
                           movie=movie)


@app.route("/movies/<int:movie_id>/show-rating-form")
def show_rating_form(movie_id):
    """Show rating form to add or update movie rating."""

    # If user is logged in, render rating_form.html.
    # Otherwise, redirect to login page.

    movie = Movie.query.get(movie_id)

    if session:
        return render_template("rating_form.html",
                               movie=movie)
    else:
        flash("Please login to add or update a movie rating.")
        return redirect("/login")


@app.route("/movies/<int:movie_id>/new-update-rating", methods=["POST"])
def process_rating(movie_id):
    """Update or add new user rating."""

    user_id = session.get("user_id")
    score = request.form.get("rating")

    rating = Rating.query.filter_by(user_id=user_id, movie_id=movie_id).first()

    if rating:
        # Update rating
        rating.score = score

        flash("Thank you. Your rating has been updated for this movie.")
        return redirect("/movies")

    else:
        # Add rating
        rating = Rating(user_id=user_id,
                        movie_id=movie_id,
                        score=score)

        db.session.add(rating)
        flash("Thank you. Your rating has been added.")

    db.session.commit()

    return redirect("/movies/%s" % movie_id)


@app.route("/login")
def show_login_form():
    """Show login form."""<commit_msg>Update redirect route after update rating<commit_after>"""Movie Ratings."""

from jinja2 import StrictUndefined

from flask import Flask, render_template, redirect, request, flash, session
from flask_debugtoolbar import DebugToolbarExtension

from model import User, Rating, Movie, connect_to_db, db


app = Flask(__name__)

# Required to use Flask sessions and the debug toolbar
app.secret_key = "ABC"

# Normally, if you use an undefined variable in Jinja2, it fails silently.
# This is horrible. Fix this so that, instead, it raises an error.
app.jinja_env.undefined = StrictUndefined


@app.route("/")
def index():
    """Homepage."""

    return render_template("homepage.html")


@app.route("/users")
def user_list():
    """Show list of users."""

    users = User.query.all()

    return render_template("user_list.html", users=users)


@app.route("/users/<int:user_id>")
def show_user_details(user_id):
    """Show user details."""

    user = User.query.get(user_id)

    return render_template("user_details.html",
                           user=user)


@app.route("/movies")
def movie_list():
    """Show list of movies."""

    movies = Movie.query.order_by(Movie.title).all()

    return render_template("movie_list.html", movies=movies)


@app.route("/movies/<int:movie_id>")
def show_movie_details(movie_id):
    """Show movie details."""

    movie = Movie.query.get(movie_id)

    return render_template("movie_details.html",
                           movie=movie)


@app.route("/movies/<int:movie_id>/show-rating-form")
def show_rating_form(movie_id):
    """Show rating form to add or update movie rating."""

    # If user is logged in, render rating_form.html.
    # Otherwise, redirect to login page.

    movie = Movie.query.get(movie_id)

    if session:
        return render_template("rating_form.html",
                               movie=movie)
    else:
        flash("Please login to add or update a movie rating.")
        return redirect("/login")


@app.route("/movies/<int:movie_id>/new-update-rating", methods=["POST"])
def process_rating(movie_id):
    """Update or add new user rating."""

    user_id = session.get("user_id")
    score = request.form.get("rating")

    rating = Rating.query.filter_by(user_id=user_id, movie_id=movie_id).first()

    if rating:
        # Update rating
        rating.score = score

        flash("Thank you. Your rating has been updated for this movie.")
        return redirect("/movies/%s" % movie_id)

    else:
        # Add rating
        rating = Rating(user_id=user_id,
                        movie_id=movie_id,
                        score=score)

        db.session.add(rating)
        flash("Thank you. Your rating has been added.")

    db.session.commit()

    return redirect("/movies/%s" % movie_id)


@app.route("/login")
def show_login_form():
    """Show login form."""
{'fraction_non_alphanumeric': 0.12785789658811114, 'fraction_numerical': 0.0007034822370735139, 'mean_word_length': 3.0190812720848057, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2293012', 'n_tokens_mistral': 1775, 'n_tokens_neox': 1672, 'n_words': 447}