-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
48 lines (40 loc) · 1.22 KB
/
app.py
File metadata and controls
48 lines (40 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import os
import re
from flask import Flask, request, redirect
from flask.ext.sqlalchemy import SQLAlchemy
from flask_cors import CORS
import requests
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ['DATABASE_URL']
CORS(app)
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(120))
def __init__(self, email):
self.email = email
def __repr__(self):
return '<Email %r>' % self.email
db.create_all()
db.session.commit()
@app.route("/", methods=['POST'])
def index():
pattern = re.compile(r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)")
email = request.form.get("email")
if pattern.match(email) is None:
return "Invalid email", 400
user = User(email)
db.session.add(user)
db.session.commit()
requests.post(
'https://{}.api.mailchimp.com/3.0/lists/{}/members'.format(os.environ['MAILCHIMP_DC'], os.environ['MAILCHIMP_LIST_ID']),
auth=(
'totality',
os.environ['MAILCHIMP_API_KEY']
),
json={
'email_address': email,
'status': 'subscribed'
}
)
return redirect("https://totalityhacks.com/")