-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
67 lines (56 loc) · 1.62 KB
/
app.py
File metadata and controls
67 lines (56 loc) · 1.62 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from flask import Flask, request, render_template
from flask_sqlalchemy import SQLAlchemy
import os
app = Flask(__name__)
class Config(object):
DEBUG = False
TESTING = False
CSRF_ENABLED = True
SECRET_KEY = 'this-really-needs-to-be-changed'
SQLALCHEMY_DATABASE_URI = os.environ['DATABASE_URL']
app.config.from_object(Config)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
from models import Subscriber
@app.route("/",methods=['GET', 'POST'])
def subscribe():
error = None
results = None
if request.method == 'POST':
try:
name=request.form.get('name',None)
email=request.form.get('email',None)
if email:
person=Subscriber(
email=email,
name=name
)
db.session.add(person)
db.session.commit()
results = '{} has been sent a confirmation email.'.format(person.email)
else:
error = "Email is required."
except Exception as e:
print(e)
error = "Error creating subscriber."
return render_template("subscribe.html", error=error, results=results)
@app.route("/unsubscribe",methods=['GET', 'POST'])
def unsubscribe():
error=None
results=None
if request.method == 'POST':
try:
email=request.form.get('email',None)
person = Subscriber.query.filter_by(email=email).first()
results = '{} as been unsubscribed.'.format(person.email)
db.session.delete(person)
db.session.commit()
except Exception as e:
print(e)
error = "Could not find that email."
return render_template("unsubscribe.html", error=error, results=results)
@app.route("/confirm/<id>")
def confirm(id):
return "Confirmed"
if __name__ == '__main__':
app.run()