JSFiddle - React, Tailwind, and code Playground

by Jessie Lau

HTML

<body>
  <div class="container">
    <div class="row">
      <div class="col-md-12">
        <div>
          <h4 id="success">The email address you entered ( <span>{{ email }}</span> ) is a VALID email address! Thank you!</h4>
          <hr>
          <h3 id="email-list">Email Addresses Entered:</h3>
          <table class="table table-hover">
            <thead>
              <tr class="active">
                <td>ID:</td>
                <td>Email:</td>
                <td>Created at:</td>
                <td>Delete email</td>
              </tr>
            </thead>
            <tbody>
              {% for email in emails: %}
              <tr>
                <td>{{ email.id }}</td>
                <td>{{ email.email }}</td>
                <td>{{ email.created_at }}</td>
                <!-- choix 1 -->
                <form action="/delete" method="POST">
                <td><input type="submit" name="{{ email.id }}" value="Delete"></td>
                </form>
              </tr>
              {% endfor %}

            </tbody>
          </table>
        </div>
      </div>
    </div>
  </div>
</body>

JavaScript

from flask import Flask, render_template, request, redirect, flash, session
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():
    session["email"] = None
    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)
        session["email"] = request.form["email_addr"]
        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, email=session["email"])


@app.route("/delete", methods=["POST"])
def delete_email_from_DB():
    query = ("DELETE FROM email_address WHERE id = {}".format(request.form["name"]))
    mysql.run_mysql_query(query)
    return redirect("/success")


app.run(debug=True)