<reponame>Elizandtech/email-validation-with-DB
from flask import Flask, render_template, request, redirect, session, flash
from mysqlconnection import connectToMySQL
import os
import re
import datetime

app = Flask(__name__)
app.secret_key = 'email validations'

dbname = 'email_mysql'
EMAIL_REGEX = re.compile(r'^[a-zA-Z0-9.+_-]+@[a-zA-Z0-9._-]+\.[a-zA-Z]+$') 

@app.route("/")
def mainpage():
    return render_template("index.html")

@app.route('/process', methods=["POST"])
def get_emails():
    if not EMAIL_REGEX.match(request.form["email"]):
        flash("INVALID email address")
        return redirect('/')
    
    connect = connectToMySQL(dbname)
    data = {"em": request.form["email"]}
    duplicates= connect.query_db("SELECT * FROM emails WHERE email = %(em)s", data)
    if len(duplicates) > 0:
        flash("This email is already taken")
        return redirect ('/')
    else:
        print("request after validation check", request.form)
        connect = connectToMySQL(dbname)
        insert_query = "INSERT INTO emails (email, created_at, updated_at)VALUES(%(em)s, NOW(), NOW());"
        data = {"em": request.form["email"]}
        connect.query_db(insert_query, data)
        session['email_add'] = request.form['email']
        print("insert query result: ", )
        return redirect('/success')

@app.route('/success')
def display_emails():
    flash("Your email " +session['email_add']+ " is a VALID email address")
    connect = connectToMySQL(dbname)
    emails = connect.query_db("SELECT id, email, date_format(created_at, '%m/%d/%Y at %r') as created_time from emails")
    print ("all_emails: ", emails)
    return render_template("success.html", get_emails = emails)

@app.route('/destroy/<id>')
def delete(id):
    connect = connectToMySQL(dbname)
    delete_query = connect.query_db("DELETE FROM emails WHERE id = %(id)s;", { "id": id})
    return redirect('/success')


if __name__ == "__main__":
    app.run(debug=True)
from flask import Flask, render_template, request, redirect, session, flash
from mysqlconnection import connectToMySQL
import os
import re
import datetime

app = Flask(__name__)
app.secret_key = 'email validations'

dbname = 'email_mysql'
EMAIL_REGEX = re.compile(r'^[a-zA-Z0-9.+_-]+@[a-zA-Z0-9._-]+\.[a-zA-Z]+$') 

@app.route("/")
def mainpage():
    return render_template("index.html")

@app.route('/process', methods=["POST"])
def get_emails():
    if not EMAIL_REGEX.match(request.form["email"]):
        flash("INVALID email address")
        return redirect('/')
    
    connect = connectToMySQL(dbname)
    data = {"em": request.form["email"]}
    duplicates= connect.query_db("SELECT * FROM emails WHERE email = %(em)s", data)
    if len(duplicates) > 0:
        flash("This email is already taken")
        return redirect ('/')
    else:
        print("request after validation check", request.form)
        connect = connectToMySQL(dbname)
        insert_query = "INSERT INTO emails (email, created_at, updated_at)VALUES(%(em)s, NOW(), NOW());"
        data = {"em": request.form["email"]}
        connect.query_db(insert_query, data)
        session['email_add'] = request.form['email']
        print("insert query result: ", )
        return redirect('/success')

@app.route('/success')
def display_emails():
    flash("Your email " +session['email_add']+ " is a VALID email address")
    connect = connectToMySQL(dbname)
    emails = connect.query_db("SELECT id, email, date_format(created_at, '%m/%d/%Y at %r') as created_time from emails")
    print ("all_emails: ", emails)
    return render_template("success.html", get_emails = emails)

@app.route('/destroy/<id>')
def delete(id):
    connect = connectToMySQL(dbname)
    delete_query = connect.query_db("DELETE FROM emails WHERE id = %(id)s;", { "id": id})
    return redirect('/success')


if __name__ == "__main__":
    app.run(debug=True)