from flask import Flask
from flask import render_template
from flask import request
from flask import redirect
from flask import session
from flask import flash

import login

app = Flask(__name__)


@app.route("/")
def home():
    loggedIn = False
    username = ""
    if "user" in session:
        loggedIn = True
        username = session["user"]

    # pass in values of username so that it shows up in navbar
    return render_template("home.html", username=username, loggedIn=loggedIn)


@app.route("/login", methods=["GET", "POST"])
def loginPage():
    if "user" in session:
        return redirect("/")

    errors = {"invalid login": "Login invalid"}
    if request.method == "GET":
        return render_template("login.html")
    else:
        fieldUsername = request.form["username"]
        fieldPassword = request.form["password"]

        success = login.login(fieldUsername, fieldPassword)

        if success:
            session["user"] = fieldUsername
            return handleRedirect()
        else:
            error = errors["invalid login"]
            return render_template("login.html",
                                   error=error,
                                   loggedIn=False)


@app.route("/logout")
def logoutPage():
    if "user" in session:
        username = session["user"]

        login.logout(username)
        session.pop("user")
    return handleRedirect()


@app.route("/register", methods=["GET", "POST"])
def registerPage():
    if "user" in session:
        return handleRedirect()

    errors = {"user-exists": "Email already exists",
              "phone-exists": "Phone number already exists",
              "password-short": "Password is too short"}
    if request.method == "GET":
        return render_template("register.html")
    else:
        fieldUsername = request.form["username"]
        fieldPhone = request.form["phone"]
        fieldPassword = request.form["password"]

        success = login.addUser(fieldUsername, fieldPassword, fieldPhone)

        if success:
            session["user"] = fieldUsername
            handleRedirect()
        else:
            error = errors["user-exists"]
            return render_template("register.html", error=error)


@app.route("/manage_account")
@app.route("/manage_account/<username>")
def manageAccountPage(username):
    if "user" not in session:
        session["nextpage"] = "/manage_account"
        return redirect("/login")
    if "user" in session:
        accountSettings = {}
        # accountSettings = db.getAccountSettings(session["user"])
        return render_template("account_settings.html", accountSettings)


def handleRedirect(redirectPage="/"):
    if "nextpage" in session:
        n = session["nextpage"]
        session.pop("nextpage")
    else:
        n = redirectPage

    return redirect(n)


def main():
    app.secret_key = "very secret"
    app.debug = True
    app.run()

if __name__ == '__main__':
    main()

@@ -20,145 +20,53 @@
 lask
-%0Afrom flask import render_template%0Afrom flask import request%0Afrom flask import redirect%0Afrom flask import session%0Afrom flask import flash
+, render_template, request, redirect, session
 %0A%0Aim

Consolidate imports from one package on one line