-
Notifications
You must be signed in to change notification settings - Fork 0
/
phonebook-app.py
167 lines (144 loc) · 7.83 KB
/
phonebook-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
166
167
# Import Flask modules
from flask import Flask, request, render_template
from flaskext.mysql import MySQL
# Create an object named app
app = Flask(__name__)
# Configure mysql database
app.config['MYSQL_DATABASE_HOST'] = 'database'
app.config['MYSQL_DATABASE_USER'] = 'admin'
app.config['MYSQL_DATABASE_PASSWORD'] = 'Selman_10'
app.config['MYSQL_DATABASE_DB'] = 'phonebook_db'
app.config['MYSQL_DATABASE_PORT'] = 3306
mysql = MySQL()
mysql.init_app(app)
connection = mysql.connect()
connection.autocommit(True)
cursor = connection.cursor()
# Write a function named `find_persons` which finds persons' record using the keyword from the phonebook table in the db,
# and returns result as list of dictionary
# `[{'id': 1, 'name':'XXXX', 'number': 'XXXXXX'}]`.
def find_persons(keyword):
query = f"""
SELECT * FROM phonebook WHERE name like '%{keyword.strip().lower()}%';
"""
cursor.execute(query)
result = cursor.fetchall()
persons =[{'id':row[0], 'name':row[1].strip().title(), 'number':row[2]} for row in result]
if len(persons) == 0:
persons = [{'name':'No Result', 'number':'No Result'}]
return persons
# Write a function named `insert_person` which inserts person into the phonebook table in the db,
# and returns text info about result of the operation
def insert_person(name, number):
query = f"""
SELECT * FROM phonebook WHERE name like '{name.strip().lower()}';
"""
cursor.execute(query)
row = cursor.fetchone()
if row is not None:
return f'Person with name {row[1].title()} already exits.'
insert = f"""
INSERT INTO phonebook (name, number)
VALUES ('{name.strip().lower()}', '{number}');
"""
cursor.execute(insert)
result = cursor.fetchall()
return f'Person {name.strip().title()} added to Phonebook successfully'
# Write a function named `update_person` which updates the person's record in the phonebook table,
# and returns text info about result of the operation
def update_person(name, number):
query = f"""
SELECT * FROM phonebook WHERE name like '{name.strip().lower()}';
"""
cursor.execute(query)
row = cursor.fetchone()
if row is None:
return f'Person with name {name.strip().title()} does not exist.'
update = f"""
UPDATE phonebook
SET name='{row[1]}', number = '{number}'
WHERE id= {row[0]};
"""
cursor.execute(update)
return f'Phone record of {name.strip().title()} is updated successfully'
# Write a function named `delete_person` which deletes person record from the phonebook table in the db,
# and returns returns text info about result of the operation
def delete_person(name):
query = f"""
SELECT * FROM phonebook WHERE name like '{name.strip().lower()}';
"""
cursor.execute(query)
row = cursor.fetchone()
if row is None:
return f'Person with name {name.strip().title()} does not exist, no need to delete.'
delete = f"""
DELETE FROM phonebook
WHERE id= {row[0]};
"""
cursor.execute(delete)
return f'Phone record of {name.strip().title()} is deleted from the phonebook successfully'
# Write a function named `find_records` which finds phone records by keyword using `GET` and `POST` methods,
# using template files named `index.html` given under `templates` folder
# and assign to the static route of ('/')
@app.route('/', methods=['GET', 'POST'])
def find_records():
if request.method == 'POST':
keyword = request.form['username']
persons = find_persons(keyword)
return render_template('index.html', persons=persons, keyword=keyword, show_result=True, developer_name='Callahan')
else:
return render_template('index.html', show_result=False, developer_name='Callahan')
# Write a function named `add_record` which inserts new record to the database using `GET` and `POST` methods,
# using template files named `add-update.html` given under `templates` folder
# and assign to the static route of ('add')
@app.route('/add', methods=['GET', 'POST'])
def add_record():
if request.method == 'POST':
name = request.form['username']
if name is None or name.strip() == "":
return render_template('add-update.html', not_valid=True, message='Invalid input: Name can not be empty', show_result=False, action_name='save', developer_name='Callahan')
elif name.isdecimal():
return render_template('add-update.html', not_valid=True, message='Invalid input: Name of person should be text', show_result=False, action_name='save', developer_name='Callahan')
phone_number = request.form['phonenumber']
if phone_number is None or phone_number.strip() == "":
return render_template('add-update.html', not_valid=True, message='Invalid input: Phone number can not be empty', show_result=False, action_name='save', developer_name='Callahan')
elif not phone_number.isdecimal():
return render_template('add-update.html', not_valid=True, message='Invalid input: Phone number should be in numeric format', show_result=False, action_name='save', developer_name='Callahan')
result = insert_person(name, phone_number)
return render_template('add-update.html', show_result=True, result=result, not_valid=False, action_name='save', developer_name='Callahan')
else:
return render_template('add-update.html', show_result=False, not_valid=False, action_name='save', developer_name='Callahan')
# Write a function named `update_record` which updates the record in the db using `GET` and `POST` methods,
# using template files named `add-update.html` given under `templates` folder
# and assign to the static route of ('update')
@app.route('/update', methods=['GET', 'POST'])
def update_record():
if request.method == 'POST':
name = request.form['username']
if name is None or name.strip() == "":
return render_template('add-update.html', not_valid=True, message='Invalid input: Name can not be empty', show_result=False, action_name='update', developer_name='Callahan')
phone_number = request.form['phonenumber']
if phone_number is None or phone_number.strip() == "":
return render_template('add-update.html', not_valid=True, message='Invalid input: Phone number can not be empty', show_result=False, action_name='update', developer_name='Callahan')
elif not phone_number.isdecimal():
return render_template('add-update.html', not_valid=True, message='Invalid input: Phone number should be in numeric format', show_result=False, action_name='update', developer_name='Callahan')
result = update_person(name, phone_number)
return render_template('add-update.html', show_result=True, result=result, not_valid=False, action_name='update', developer_name='Callahan')
else:
return render_template('add-update.html', show_result=False, not_valid=False, action_name='update', developer_name='Callahan')
# Write a function named `delete_record` which updates the record in the db using `GET` and `POST` methods,
# using template files named `delete.html` given under `templates` folder
# and assign to the static route of ('delete')
@app.route('/delete', methods=['GET', 'POST'])
def delete_record():
if request.method == 'POST':
name = request.form['username']
if name is None or name.strip() == "":
return render_template('delete.html', not_valid=True, message='Invalid input: Name can not be empty', show_result=False, developer_name='Callahan')
result = delete_person(name)
return render_template('delete.html', show_result=True, result=result, not_valid=False, developer_name='Callahan')
else:
return render_template('delete.html', show_result=False, not_valid=False, developer_name='Callahan')
# Add a statement to run the Flask application which can be reached from any host on port 80.
if __name__== '__main__':
app.run(host='0.0.0.0', port=80)