-
Notifications
You must be signed in to change notification settings - Fork 0
/
myPhonebook.py
124 lines (107 loc) · 4.62 KB
/
myPhonebook.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
import os
from flask import Flask, render_template, request, redirect, url_for
from werkzeug.utils import secure_filename
import mysql.connector
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
#define connection
app = Flask("MyApp")
app.config['UPLOAD_FOLDER'] = "/Users/DSS-Mac/htdocs/myPhonebook/static/profilepics/"
app.config['MAX_CONTENT_LENGTH'] = 2 * 1024 * 1024
home_dir = os.path.join(app.config['UPLOAD_FOLDER'],"")
conn = mysql.connector.connect(
user='root',
password='',
host='127.0.0.1',
database='demo')
#opens connection
cur = conn.cursor()
activeTab={"home":"","addEntries":"","updateEntry":""}
#Ensure filetypes are OK. Added .lower() so uppercase exts will pass
def allowed_file(filename):
return '.' in filename and \
filename.lower().rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
#Grab all db entries, cast tuples to a list, then for each, check if photo_ext is anything. If so, concatenate the id, '.', and ext to generate image filename and store it as such in the new list 'l'. If no valid value, use placeholder.jpg instead for the filename.
@app.route("/")
def listings():
query = ("SELECT id,name,email,phone,photo_ext FROM phonebook")
cur.execute(query)
l = []
for each in cur.fetchall():
l.append(list(each))
print l
for i in l:
print i
if i[4]:
print i[0]
print i[4]
i[4]= str(i[0]) + "." +i[4]
print i[4]
else:
i[4]= "placeholder.jpg"
setActiveTab("home")
return render_template("listings.html", contact_list=l, title="My Phonebook",activeTab=activeTab)
@app.route("/update_entry")
def updateEntry(methods = ['GET']):
id = request.args.get('id')
query = ("SELECT id,name,email,phone,photo_ext FROM phonebook WHERE id = '%s'" % id)
cur.execute(query)
list = cur.fetchone()
setActiveTab("updateEntry")
return render_template("update_entry.html", phonebook=list, title="My Phonebook",activeTab=activeTab)
#set all active tab dictionaries value to "" then make the indicated tab from the flask route function active. This make class="active" for css styling dynamically.
def setActiveTab(tabName):
global activeTab
for tab in activeTab:
activeTab[tab] = ""
if activeTab.has_key(tabName):
activeTab[tabName] = "active"
print activeTab
@app.route("/new_entry")
def new_entry():
setActiveTab("addEntries")
return render_template("new_entry.html", title="My Phonebook",activeTab=activeTab)
#check if this page is being reached from a POST. If not, ignore functions. Otherwise check filename is safe, grab the ext of the image file for storage in the db and insert new entry.
#After entry is stored, get the new id from db and use it plus '.' plus ext to save the image to the server.
@app.route("/submit_new_entry", methods=['POST'])
def submit_new_student():
setActiveTab("addEntries")
if request.method == 'POST':
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
print filename
ext = filename.lower().rsplit('.', 1)[1]
else:
ext = ""
print ext
name=request.form.get('name')
email=request.form.get('email')
phone=request.form.get('phone')
query = "insert into phonebook (name,email,phone,photo_ext) values ('%s','%s','%s','%s')" % (name,email,phone,ext)
cur.execute(query)
conn.commit()
lastId=cur.lastrowid
file.save(os.path.join(app.config['UPLOAD_FOLDER'], str(lastId)+"."+ext))
# return render_template("submit_new_entry.html", name=name,email=email,phone=phone, activeTab=activeTab)
return redirect("/")
#Need to add ability to update images. Have to move on for now. To do: Extract image upload with overwrite to an outsidefunction either new_contact or update_contact can make use of.
@app.route("/submit_update_contact", methods=['POST'])
def submit_update_contact():
id = request.form.get('id')
setActiveTab("updateEntry")
name=request.form.get('name')
email=request.form.get('email')
phone=request.form.get('phone')
if request.form['submit'] == 'update':
query = "UPDATE phonebook SET name = '%s', email = '%s', phone = '%s' WHERE id = '%s'" % (name,email,phone, id)
cur.execute(query)
conn.commit()
if request.form['submit'] == 'delete':
query = "DELETE FROM phonebook WHERE id = '%s'" % id
cur.execute(query)
conn.commit()
return redirect("/")
if __name__=="__main__":
app.run(debug=True)
cur.close()
conn.close()