-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql-app.py
165 lines (140 loc) · 6.04 KB
/
mysql-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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
from flask import Flask, render_template, request
from flaskext.mysql import MySQL
app = Flask(__name__)
developer = "Jokerinya"
db_endpoint = open("/home/ec2-user/dbserver.endpoint", 'r', encoding='UTF-8')
db_password = open("/home/ec2-user/dbserver.passwd", 'r', encoding='UTF-8')
# Configure mysql database
app.config['MYSQL_DATABASE_HOST'] = db_endpoint.readline().strip()
app.config['MYSQL_DATABASE_USER'] = 'admin'
app.config['MYSQL_DATABASE_PASSWORD'] = db_password.readline().strip()
app.config['MYSQL_DATABASE_DB'] = 'clarusway'
app.config['MYSQL_DATABASE_PORT'] = 3306
db_endpoint.close()
db_password.close()
mysql = MySQL()
mysql.init_app(app)
connection = mysql.connect()
connection.autocommit(True)
cursor = connection.cursor()
# execute the code only ONCE
drop_table="""
DROP TABLE IF EXISTS phonebook;
"""
phonebook_table="""
CREATE TABLE phonebook (
id INT NOT NULL AUTO_INCREMENT,
person varchar(50) NOT NULL,
number varchar(50),
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
"""
sample_data="""
INSERT INTO phonebook(person, number)
VALUES
("Buddy Rich", "999999999999" ),
("Candido", "11111111111111"),
("Charlie Byrd", "123456789");
"""
cursor.execute(drop_table)
cursor.execute(phonebook_table)
cursor.execute(sample_data)
def find_persons(keyword):
query=f"SELECT person, number FROM phonebook WHERE person like '%{keyword}%';"
cursor.execute(query)
raw_result = cursor.fetchall() # tuple type
result = [{"name" : row[0], "number": row[1]} for row in raw_result]
if not any(result):
result = [{"name" : "Not Found", "number": "Not Found"}]
return result
def find_one(keyword):
query=f"SELECT id, person, number FROM phonebook WHERE person like '{keyword}';"
cursor.execute(query)
raw_result = cursor.fetchone()
return raw_result # tupple
def delete_person(keyword):
result = find_one(keyword)
if result == None:
return f"There is no record with {keyword}, no record deleted."
query=f"DELETE FROM phonebook WHERE id = {result[0]}"
cursor.execute(query)
return f"The record deleted, it was: {result[1]} -- {result[2]}."
def add_person(username, phonenumber):
query=f"""INSERT INTO phonebook(person, number) VALUES ("{username}", "{phonenumber}");"""
cursor.execute(query)
def validation(username, phonenumber):
if username=="" or username==None:
return "Invalid input: Name can not be empty"
elif phonenumber=="" or phonenumber==None:
return "Invalid input: Phone number can not be empty"
elif not username.isalpha():
return "Invalid input: Name of person should be text"
elif not phonenumber.isnumeric():
return "Invalid input: Phone number should be in numeric format"
else:
return "ok"
def update_person(id, new_phonenumber):
query=f"""
UPDATE phonebook SET number="{new_phonenumber}" WHERE id={id};
"""
cursor.execute(query)
# Routings
# index page
@app.route("/", methods=["GET", "POST"])
def main():
if request.method=="POST":
keyword = request.form["username"].strip().title()
persons = find_persons(keyword)
return render_template("index.html", developer_name=developer, show_result=True ,no_result=False, keyword=keyword, persons=persons)
else:
return render_template("index.html", developer_name=developer, show_result=False)
# delete
@app.route("/delete", methods = ["GET", "POST"])
def delete():
if request.method == "POST":
username=request.form["username"].strip().title()
if username.isnumeric():
return render_template("delete.html", developer_name=developer, not_valid=True, show_result=False, message=f"Username is cannot be numeric '{username}'")
result = delete_person(username)
return render_template("delete.html", developer_name=developer, not_valid=False,show_result=True, result=result)
else:
return render_template("delete.html", developer_name=developer, not_valid=False,show_result=False)
# add
@app.route("/add", methods=["GET", "POST"])
def add():
if request.method=="GET":
return render_template("add-update.html", developer_name=developer, action_name="Add", not_valid=False, show_result=False)
username=request.form["username"].strip().title()
phonenumber=request.form["phonenumber"].strip()
message = validation(username, phonenumber)
if message == "ok":
person_info = find_one(username)
if person_info == None:
add_person(username, phonenumber)
result=f"Record added ({username}, {phonenumber})"
else:
result=f"Record is already exist, ({person_info[1]}, {person_info[2]})"
return render_template("add-update.html", developer_name=developer, action_name="Add", not_valid=False, show_result=True, result=result)
else:
return render_template("add-update.html", developer_name=developer, action_name="Add", not_valid=True, show_result=False, message=message)
# update
@app.route("/update", methods=["GET", "POST"])
def update():
if request.method=="GET":
return render_template("add-update.html", developer_name=developer, action_name="Update", not_valid=False, show_result=False)
username=request.form["username"].strip().title()
phonenumber=request.form["phonenumber"].strip()
message = validation(username, phonenumber)
if message == "ok":
person_info = find_one(username)
if person_info == None:
result=f"There is no one with '{username}', no record updated."
else:
update_person(person_info[0], phonenumber)
result=f"Person number updated, ({person_info[2]}---> {phonenumber})"
return render_template("add-update.html", developer_name=developer, action_name="Update", not_valid=False, show_result=True, result=result)
else:
return render_template("add-update.html", developer_name=developer, action_name="Update", not_valid=True, show_result=False, message=message)
if __name__ == "__main__":
# app.run(debug=True)
app.run(host="0.0.0.0", port=80)