from flask import render_template, request, flash, redirect, url_for, session
from flask_mysqldb import MySQL
from passlib.hash import sha256_crypt
from app.forms import RegistrationForm
from app import app

# MySQL connection.
mysql = MySQL(app)

# Route for the index page or home page.
@app.route("/")
def index():
    return render_template("home.html", title="Home")

# Route for the registration page.
@app.route("/register", methods=["GET", "POST"])
def register():
    form = RegistrationForm(request.form)
    if request.method == "POST" and form.validate():
        # Get user data from form.
        name = form.name.data
        username = form.username.data
        email = form.email.data
        password = sha256_crypt.encrypt(str(form.password.data))

        # Create a cursor and insert user into the database.
        curr = mysql.connection.cursor()
        curr.execute("INSERT INTO users(name, username, email, password) VALUES(%s, %s, %s, %s)", (name, username, email, password))

        # Close connection.
        mysql.connection.commit()
        curr.close()

        # Redirect the user.
        flash("Account created!", "success")    
        return redirect(url_for("login"))
        
    return render_template("register.html", title="Register", form=form)

# Route for the login page.
@app.route("/login", methods=["GET", "POST"])
def login():
    if request.method == "POST":
        # Get user data from form
        username = request.form['username']
        plain_pw = request.form['password']
        
        # Create a cursor and query the database.
        curr = mysql.connection.cursor()
        result = curr.execute("SELECT * FROM users WHERE username=%s", (username,))

        # Check for match.
        if result:
            # Save user data from query result.
            user_data = curr.fetchone()
            hashed_pw = user_data['password']

            # Validate password
            if sha256_crypt.verify(plain_pw, hashed_pw):
                # Save user session.
                session['logged_in'] = True
                session['username'] = user_data['username']
                
                # Redirect to dashboard.
                flash("Login success.", "success")
                return redirect(url_for("dashboard"))

        return render_template("login.html", title="Login", err="Invalid credentials.")

    return render_template("login.html", title="Login")

# Route for the logout page.
@app.route("/logout")
def logout():
    # Clear the user session then redirect them to login page.
    session.clear()
    flash("You are now logged out.", "info")
    return redirect(url_for("login"))

# Route for the user dashboard.
@app.route("/dashboard", methods=["GET", "POST"])
def dashboard():
    if request.method == "POST":
        pass
    return render_template("dashboard.html", title="Dashboard")
@@ -179,16 +179,26 @@
 tionForm
+, TaskForm
 %0Afrom ap
@@ -2875,16 +2875,880 @@
 tle=%22Dashboard%22)
+%0A%0A# Route for the add task page.%0A@app.route(%22/add_task%22, methods=%5B%22GET%22, %22POST%22%5D)%0Adef add_task():%0A    form = TaskForm(request.form)%0A%0A    if request.method == %22POST%22 and form.validate():%0A        # Get task information%0A        author = session%5B'username'%5D%0A        description = form.description.data%0A        status = form.status.data%0A%0A        # Create a cursor and query the database.%0A        curr = mysql.connection.cursor()%0A        curr.execute(%22INSERT INTO tasks(author, description, status) VALUES(%25s, %25s, %25s)%22, (author, description, status))%0A        %0A        # Commit and close connection.%0A        mysql.connection.commit()%0A        curr.close()%0A%0A        # Flash and redirect back to dashboard.%0A        flash(%22Task added.%22, %22success%22)%0A        return redirect(url_for(%22dashboard%22))%0A        %0A    return render_template(%22add_task.html%22, title=%22Add Task%22, form=form)

Handle GET and POST request for the add_task page.