JSFiddle - React, Tailwind, and code Playground

by Jessie Lau

JavaScript

from flask import Flask, render_template, request, redirect, flash
import re
import time
# import the Connector function
from mysqlconnection import MySQLConnector
EMAIL_REGEX = re.compile(r'^[a-zA-Z0-9\.\+_-]+@[a-zA-Z0-9\._-]+\.[a-zA-Z]*$')
app = Flask(__name__)
# connect and store the connection in "mysql" note that you pass the
# database name to the function
app.secret_key = "IsSrecretKey"
mysql = MySQLConnector('email_address')


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


@app.route("/add-email", methods=["POST"])
def add_email():
    if email_validations(request.form["email_addr"]):
        query = ("INSERT INTO email_address(email, created_at)VALUES('{}', '{}')".format(request.form["email_addr"], time.strftime("%Y/%m/%d %H:%M")))
        mysql.run_mysql_query(query)
        return redirect("/success")
    else:
        flash("Please enter a valid email address.")
        return redirect("/")


def email_validations(email):
    if len(request.form['email_addr']) < 1 or not EMAIL_REGEX.match(request.form['email_addr']):
        return False
    else:
        return True


@app.route("/success")
def success():
    emails = mysql.fetch("SELECT * FROM email_address")
    return render_template("success.html", emails=emails)

app.run(debug=True)