def new_acc():
    is_valid = True
    name_err = 0
    if not request.form['fname'].isalpha() or not len(
            request.form['fname']) >= 2:
        is_valid = False
        name_err = 1
        flash("Names can contain only letters and be at least two characters",
              "registration")
    if not request.form['lname'].isalpha() or not len(
            request.form['lname']) >= 2:
        is_valid = False
        if name_err == 1:
            flash(
                "Names can contain only letters and be at least two characters",
                "registration")
    if not password_reg.match(request.form["pass"]):
        is_valid = False
        flash(
            "Password should be at least 5 characters, have one number, one uppercase and one lowercase letter, and one symbol",
            "registration")
    if not EMAIL_REGEX.match(request.form["email"]):
        is_valid = False
        flash("Invalid Email Address", "registration")
    if request.form["pass"] != request.form["confirmpass"]:
        is_valid = False
        flash("Passwords do not match", "registration")
    if is_valid:
        pw_hash = bcrypt.generate_password_hash(request.form["pass"])
        new_user = User(f_name=request.form["fname"],
                        l_name=request.form["lname"],
                        email=request.form["email"],
                        admin_status=0,
                        password=pw_hash)
        db.session.add(new_user)
        db.session.commit()
        session["user_id"] = {
            "first": new_user.f_name,
            "last": new_user.l_name,
            "email": new_user.email,
            "id": new_user.user_id
        }
        print(session["user_id"])
        print("Account creation successful!")
        return redirect("/userpage")
    return redirect("/")
def editpassword():
    id = request.form["user_id"]
    if int(id) != session["user_id"]["id"]:
        return redirect("/logout")
    user = User.query.get(id)
    hashed_pw = user.password
    if bcrypt.check_password_hash(hashed_pw, request.form['current_pass']):
        if not password_reg.match(request.form["new_pass"]):
            flash(
                "Password should be at least 5 characters, have one number, one uppercase and one lowercase letter, and one symbol",
                "update_pass")
        if request.form["new_pass"] != request.form["confirm_pass"]:
            flash("Passwords do not match", "update_pass")
        else:
            new_pw_hash = bcrypt.generate_password_hash(
                request.form["new_pass"])
            user.password = new_pw_hash
            db.session.commit()
            flash("Password changed!", "update_pass")
    else:
        flash("Incorrect Password", "update_pass")
    return redirect(f"/userprofile/{id}")
def reg_pw():
    pw_check = True
    if not password_reg.match(request.form["pass"]):
        pw_check = False
    return render_template("partials/reg_error.html", pw_check=pw_check)
def register():
    switch = request.form["switch"]
    print(switch)
    is_valid = True
    if switch == "org":
        email = Organization.query.filter_by(email=request.form["email"]).all()
        email2 = Developer.query.filter_by(email=request.form["email"]).all()
        if len(request.form["orgname"]) == 0:
            is_valid = False
            flash("Organization name cannot be blank", "reg")
    elif switch == "dev":
        email = Developer.query.filter_by(email=request.form["email"]).all()
        email2 = Organization.query.filter_by(
            email=request.form["email"]).all()
    if email:
        is_valid = False
        flash("Email in use")
    if email2:
        is_valid = False
        if switch == "dev":
            flash("Email registered as an Organization")
        if switch == "org":
            flash("Email registered as a Developer")
    if not request.form["fname"].isalpha() or not len(
            request.form["fname"]) >= 2:
        is_valid = False
        flash(
            "First name can only contain letters and must be at least 2 characters long",
            "reg")
    if not request.form['lname'].isalpha() or not len(
            request.form['lname']) >= 2:
        is_valid = False
        flash(
            "Last name can only contain letters and must be at least 2 characters long",
            "reg")
    if not EMAIL_REGEX.match(request.form["email"]):
        is_valid = False
        flash("Invalid Email Address")
    if not len(request.form['address']) > 5:
        is_valid = False
        flash("Address must be longer than 5 characters", "reg")
    if not len(request.form['city']) >= 2:
        is_valid = False
        flash("City name must be at least 3 characters long", "reg")
    if not password_reg.match(request.form["password"]):
        is_valid = False
        flash(
            "Password should be at least 5 characters, have one number, one uppercase and one lowercase letter, and one symbol"
        )
    if request.form["password"] != request.form["confirmpass"]:
        is_valid = False
        flash("Passwords do not match", "reg")
    if is_valid:
        pw_hash = bcrypt.generate_password_hash(request.form["password"])
        if switch == "dev":
            user = Developer(fname=request.form["fname"],
                             lname=request.form["lname"],
                             email=request.form["email"],
                             address=request.form["address"],
                             city=request.form["city"],
                             state=request.form["state"],
                             password=pw_hash)
        elif switch == "org":
            user = Organization(orgname=request.form["orgname"],
                                fname=request.form["fname"],
                                lname=request.form["lname"],
                                email=request.form["email"],
                                address=request.form["address"],
                                city=request.form["city"],
                                state=request.form["state"],
                                password=pw_hash)
        db.session.add(user)
        db.session.commit()
        session["user_id"] = {
            "fname": user.fname,
            "lname": user.lname,
            "email": user.email,
            "id": user.id,
            "role": switch
        }
        return redirect("/{}_landing".format(switch))
    return redirect("/#{}_reg".format(switch))