-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackend.py
More file actions
108 lines (85 loc) · 2.97 KB
/
backend.py
File metadata and controls
108 lines (85 loc) · 2.97 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
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
from flask import Flask, request ,render_template
from markupsafe import escape
from awsses import Email
import os
from validate import emailValidation, isValidDomain
from flaskext.mysql import MySQL
from dotenv import load_dotenv
load_dotenv()
app = Flask(__name__)
mysql = MySQL()
print(os.getenv('MYSQL_DATABASE_USER'))
print(os.getenv('MYSQL_DATABASE_PASSWORD'))
print(os.getenv("MYSQL_DATABASE_HOST"))
app.config['MYSQL_DATABASE_USER'] = os.getenv('MYSQL_DATABASE_USER')
app.config['MYSQL_DATABASE_PASSWORD'] = os.getenv('MYSQL_DATABASE_PASSWORD')
app.config['MYSQL_DATABASE_DB'] = 'websiteStatus'
app.config['MYSQL_DATABASE_HOST'] = os.getenv("MYSQL_DATABASE_HOST")
mysql.init_app(app)
@app.route('/')
def home():
return "Home"
@app.route("/add/", methods=['GET', 'POST'])
def addWebsite():
if request.method == 'GET':
# Render the subscribe page.
return render_template("form.html")
#print(request)
userEmail = request.form['email'].lower()
websiteRequested = request.form['website'].lower()
print(userEmail + " " + websiteRequested)
# Entered email and webiste are None
if not userEmail or not websiteRequested:
return {
'status': 'failure',
'reason': 'Post request required fields not found'
}
# Entered email is not None but it is invalid
if not emailValidation(userEmail):
return {
'status': 'failure',
'reason': 'Entered email is invalid'
}
if not isValidDomain(websiteRequested):
return {
'status': 'failure',
'reason': 'Entered domain is invalid'
}
conn = mysql.connect()
cursor = conn.cursor()
print(userEmail, websiteRequested)
# Primary key is domain itself in lastStatus
try:
cursor.execute("INSERT INTO lastStatus(domain) VALUES (%s)", (websiteRequested,))
except:
print("The domain already exists in the lastStatus table")
# Primary key in subscriptions is id, need to avoid duplicate entries
cursor.execute("SELECT * FROM subscriptions where email = %s and domain = %s", (userEmail, websiteRequested))
if cursor.fetchone():
conn.commit()
conn.close()
return {
'status': 'success'
}
cursor.execute("INSERT INTO subscriptions (email, domain) VALUES (%s, %s)", (userEmail, websiteRequested))
conn.commit()
conn.close()
email = Email('New subscription')
email.body_text('Testing AWS SES')
html = """<html>
<head></head>
<body>
<h1>New subcription to our service</h1>
<p>Thank you for subscribing to the website
<a href='{websiteRequested}'>{websiteRequested}</a>.
You will be receiving the status of website every five minutes.
</body>
</html>
""".format(websiteRequested=websiteRequested)
email.body_html(html)
email.send([userEmail])
return {
'status': 'success'
}
if __name__ == '__main__':
app.run(debug=True)