-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
129 lines (113 loc) · 2.85 KB
/
main.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
import flask
import rockset
import smtplib, ssl
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import os
from jinja2 import Template
from shortuuid import uuid
def send_mail(address, ver_code, body):
sender_email = os.getenv('EMAIL_EMAIL')
receiver_email = address
password = os.getenv('EMAIL_PW')
message = MIMEMultipart('alternative')
message['Subject'] = 'Harker Hackers Subscription'
message['From'] = sender_email
message['To'] = receiver_email
message.attach(
MIMEText(
body,
'html'
)
)
context = ssl.create_default_context()
with smtplib.SMTP_SSL('smtp.gmail.com', 465, context=context) as server:
server.login(sender_email, password)
server.sendmail(
sender_email, receiver_email, message.as_string()
)
app = flask.Flask(__name__)
rs = rockset.Client(
api_key=os.getenv('RS2_TOKEN'),
api_server='api.rs2.usw2.rockset.com'
)
collection = rs.Collection.retrieve(
'emails',
workspace='harker_hackers'
)
@app.route('/')
def login():
return(
flask.render_template(
'index.jinja'
)
)
@app.route('/authorized')
def after():
email = flask.request.args.get('email')
ver_code = uuid()
res = list(
rs.sql(
rockset.Q(
'harker_hackers.emails'
).where(
rockset.F['_id'] == email
).select('*')
)
)
if res == []:
body = Template(
open(
'email_templates/verification.jinja',
'r'
).read()
).render({
'code': ver_code,
'email': email
})
send_mail(email, ver_code, body)
print(
collection.add_docs([{
'_id': email,
'verified': False,
'code': ver_code
}])
)
else:
body = open(
'email_templates/already_subbed.html',
'r'
).read()
send_mail(email, ver_code, body)
return(
flask.render_template(
'please_verify.jinja',
email=email
)
)
@app.route('/authorized/<ver_code>/<ver_email>')
def verify(ver_code, ver_email):
res = rs.sql(
rockset.Q(
'select _id from harker_hackers.emails where code=\'{}\' and _id=\'{}\''.format(
ver_code,
ver_email
)
)
)
email = res[0]['_id']
if res != []:
collection.add_docs([{
'_id': email,
'verified': True,
}])
return(
flask.render_template(
'verified.jinja',
email=email
)
)
else:
return('Email not found')
if __name__ == '__main__':
app.run()