This repository has been archived by the owner on Aug 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lister.py
131 lines (107 loc) · 4.09 KB
/
lister.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
from flask import Flask, render_template, request, make_response
import logging
from models import List, db, Person, Subscription
import sys
from blueprints import admin
from validate_email import validate_email_or_fail
from validate_email.exceptions import DomainBlacklistedError, EmailValidationError
from flask_cors import CORS
from flask.json import jsonify
from helpers.helpers import make_response_data, respond, get_uuid
app_logger = logging.getLogger('lister')
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
# app.config.update(DEBUG=True, SQLALCHEMY_TRACK_MODIFICATIONS=False)
CORS(app, origins="*", send_wildcard=True)
app.register_blueprint(admin.blueprint, url_prefix='/admin')
db.init_app(app)
@app.route("/")
def index():
return render_template("web/index.html")
@app.route("/confirm", methods=['GET'])
def confirm():
"""
handles the confirmation action from the link that was in the users confirmation email and return the success page
"""
# TODO: get the information from the DB for the confirmation token
# request.args.get("token")
# TODO: set token to NULL to indicate that the email was verified and return success page
return ""
@app.route("/subscribe", methods=['POST'])
def subscribe():
"""
receives the form data from the subscription form and triggers the sending of the confirmation email, along with all the database changes that go with it
"""
# POST parameters: list ID, or multiple list ID's, email address
# https://stackoverflow.com/a/28982264
if request.form.get('email') is None:
return respond(make_response_data(
"You must enter an email address",
request.form.get('listid')
),
code=400)
try:
validate_email_or_fail(email_address=request.form.get('email'), check_mx=False)
except EmailValidationError as e:
err_msg = ""
if isinstance(e, DomainBlacklistedError):
err_msg = "Disposable email addresses are not allowed."
else:
err_msg = "Invalid Email address. Please try again"
if request.args.get("redirect"):
return render_template("web/subscribeform.html", error=err_msg, list_id=request.form.get('listid')) #email=email
else:
return respond(
make_response_data(err_msg, request.form.get('listid')),
code=400)
#send confirmation email and add to DB
# create Person entry if not exists
person = Person.query.filter_by(email=request.form.get('email')).first()
if person is None:
#person doesnt exist, create aand add to DB
person = Person(email=request.form.get('email'))
db.session.add(person)
# get list that the user wants to subscribe to
list = List.query.filter_by(id=request.form.get('listid')).first()
if person is None:
return respond(make_response_data(
"The list you are trying to subscribe to was not found",
request.form.get('listid')), code=404)
# generate confirmation code and add the person to the list
confirm_code = get_uuid()
# TODO: create parent, append a child via association
# p = Parent()
# sub = Subscription(token=confirm_code)
# sub.child = list
# person.children.append(sub)
# list.child_associations.append(Subscription(child=c1))
# TODO: generate confirmation link and email from confirmation code
# TODO: send confirmation email
if request.args.get("redirect"):
return render_template("web/subscribesuccess.html")
else:
return respond(make_response_data(
"Check your email to confirm your subscription!",
request.form.get('listid')
)
)
@app.route("/embed", methods=['GET'])
def embed_subscribe():
"""
returns an embeddable form suitable for an iFrame.
accepts the URL parameter listid for the id of the list to subscribe the user to
"""
return render_template("web/subscribeform.html", list_id=request.args.get("listid"))
if len(sys.argv) > 1 and sys.argv[1] == "setup":
# https://stackoverflow.com/a/46541219
with app.app_context():
db.create_all()
if db.query(List).first() is None:
testList = List(name="Test List", description="a sample list for testing purposes")
db.session.add(testList)
print(testList.id)
db.session.commit()
print("Setup Complete.")
exit(0)
if __name__ == "__main__":
app.run()