-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
88 lines (62 loc) · 2.78 KB
/
app.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
import sys
import sendgrid
from flask import Flask, render_template, request, session
from sendgrid.helpers.mail import Content, From, Mail, To
from validate_email import validate_email
# for Docker Secrets
sys.path.append('/run/secrets')
import config
app = Flask(__name__)
app.secret_key = config.config["secret_key"]
app.jinja_env.add_extension("pyjade.ext.jinja.PyJadeExtension")
@app.route("/")
def index():
if session.get("nominee_name", None):
return render_template("nominated.jade", name=session["nominee_name"])
return render_template("home.jade")
@app.route("/submit", methods=["POST"])
def nominate():
if session.get("nominee_name", None):
return render_template("nominated.jade", name=session["nominee_name"])
form = request.form
sender_name = form.get("name", None)
sender_email = form.get("email", None)
nominee_name = form.get("nominee_name", None)
reason = form.get("reason", None)
errors = [] # store any input validation errors
if sender_name is None or len(sender_name) == 0:
errors.append("You forgot to enter your name.")
if sender_email is None or len(sender_email) == 0:
errors.append("You forgot to enter your email.")
elif not validate_email(sender_email):
errors.append("The email you entered in your personal details isn't valid.")
if nominee_name is None or len(nominee_name) == 0:
errors.append("You forgot to tell us who you're nominating.")
if reason is None or len(reason) == 0:
errors.append("You forgot to tell us why your nominee should receive HLM.")
elif len(reason) > 2000:
errors.append("Please limit your reasons for nominating to a maximum of 2000 characters.")
if len(errors) > 0:
return render_template("nominated.jade", errors=errors)
sg = sendgrid.SendGridAPIClient(config.config["api_key"])
from_email = From(config.config["from_address"], name="HLM Nomination Service")
to_email = To(config.config["to_address"])
subject = "Nomination for %s" % nominee_name
content = Content("text/plain", """
[SENDER DETAILS]
Name: %s
Email: %s
[NOMINEE DETAILS]
Name: %s
[REASON]
\"%s\"
""" % (sender_name.strip(), sender_email.strip(), nominee_name.strip(), reason.strip()))
mail = Mail(from_email, to_email, subject, content)
response = sg.send(mail)
status_code = str(response.status_code)
if not status_code.startswith("20"):
error = "There was a problem sending your nomination. Please try again,"\
"or email your nomination to %s." % config.config["to_address"]
return render_template("nominated.jade", errors=[error])
session["nominee_name"] = nominee_name.strip()
return render_template("nominated.jade", name=nominee_name.strip())