-
Notifications
You must be signed in to change notification settings - Fork 0
/
init1.py
355 lines (319 loc) · 14.3 KB
/
init1.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#Import Flask Library
from flask import Flask, render_template, request, session, url_for, redirect
import pymysql.cursors
import os
import hashlib
from werkzeug.utils import secure_filename
from datetime import datetime
UPLOAD_FOLDER = 'static/'
ALLOWED_EXTENSIONS = {'pdf', 'png', 'jpg', 'jpeg', 'gif'}
salt = 'cs3083'
#Initialize the app from Flask
app = Flask(__name__, static_url_path='')
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
#Configure MySQL
conn = pymysql.connect(host='localhost',
port = 3306,
user='root',
password='',
db='FlaskDemo',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
#Define a route to hello function
@app.route('/')
def hello():
return render_template('index.html')
@app.route('/photopost', methods=['GET', 'POST'])
def photopost():
posted=0
print(request.method)
user = session['username']
cursor = conn.cursor();
query = 'SELECT groupname,owner_username FROM belongto WHERE member_username = %s'
cursor.execute(query, (user))
groups=cursor.fetchall()
for line in groups:
print(line['groupname']+line['owner_username'])
cursor.close()
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
# if user does not select file, browser also
# submit an empty part without filename
if file.filename == '':
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
if not os.path.exists("static/" + user):
os.makedirs("static/" + user)
offset=""
while os.path.isfile("static/" + user + "/" + filename[:-4]+offset+filename[-4:]):
offset+="_"
file.save(os.path.join(app.config['UPLOAD_FOLDER'], user + "/" + filename[:-4]+offset+filename[-4:]))
posted=1
username = session['username']
cursor = conn.cursor();
query = 'SELECT MAX(photoid) FROM photo'
now = datetime.now()
timestamp = now.strftime("%d/%m/%Y %H:%M:%S")
cursor.execute(query)
data = cursor.fetchall()
newID=data[0]['MAX(photoid)'] + 1
allFollowers = request.form['allFollowers']
caption = request.form['caption']
query = 'INSERT INTO photo (filepath,allFollowers,caption,photoPoster,photoid) VALUES(%s,%s,%s,%s,%s)'
cursor.execute(query, (username + "/" + filename,allFollowers,caption,username,newID))
conn.commit()
cursor.close()
for line in groups:
print(request.form[(line['groupname']+line['owner_username'])])
if(request.form[(line['groupname']+line['owner_username'])]=="1"):
print("entered")
cursor = conn.cursor();
query = 'INSERT INTO sharedwith (ownerusername,memberusername,photoid,groupname) VALUES(%s,%s,%s,%s)'
cursor.execute(query,(line['owner_username'],username,newID,line['groupname']))
conn.commit()
cursor.close()
if(posted==1):
return render_template('photopost.html', groups = groups, posted = "Photo posted!")
else:
return render_template('photopost.html', groups = groups, posted = "")
#Define route for login
@app.route('/login')
def login():
return render_template('login.html')
#Define route for register
@app.route('/register')
def register():
return render_template('register.html')
@app.route('/viewfollowers', methods=['GET', 'POST'])
def viewfollowers():
msg=""
username = session['username']
cursor = conn.cursor();
query = 'SELECT username_follower FROM follow WHERE username_followed = %s AND followstatus=1'
cursor.execute(query, (username))
followers=cursor.fetchall()
query = 'SELECT username_follower FROM follow WHERE username_followed = %s AND followstatus=0'
cursor.execute(query, (username))
requests=cursor.fetchall()
if request.method == 'POST':
msg="successfully updated!"
for line in requests:
if(request.form[line['username_follower']]=="0"):
query = 'DELETE FROM follow WHERE username_followed = %s AND username_follower=%s'
cursor.execute(query, (username,line['username_follower']))
print(username+line['username_follower'])
conn.commit()
else:
query = 'UPDATE follow SET followstatus=%s WHERE username_followed = %s AND username_follower = %s'
cursor.execute(query, (request.form[line['username_follower']],username,line['username_follower']))
conn.commit()
query = 'SELECT username_follower FROM follow WHERE username_followed = %s AND followstatus=1'
cursor.execute(query, (username))
followers=cursor.fetchall()
query = 'SELECT username_follower FROM follow WHERE username_followed = %s AND followstatus=0'
cursor.execute(query, (username))
requests=cursor.fetchall()
cursor.close()
return render_template('viewfollowers.html', followers=followers, requests=requests, msg=msg)
@app.route('/follow', methods=['GET', 'POST'])
def follow():
msg=""
if request.method == 'POST':
msg="error, likely user doesn't exist"
username = session['username']
cursor = conn.cursor();
query = 'SELECT username FROM person WHERE username = %s'
cursor.execute(query, (username))
data=cursor.fetchall()
cursor.execute(query, (request.form['userFollow']))
data2=cursor.fetchall()
if(bool(data)):
if(bool(data2)):
#checking if both user and who they're trying to follow exist
query = 'SELECT username_follower FROM follow WHERE username_follower = %s AND username_followed = %s'
cursor.execute(query, (username,request.form['userFollow']))
data=cursor.fetchall()
if(bool(data)):
msg="Already requested/accepted."
else:
msg="Sent follow request!"
query = 'INSERT INTO follow VALUES (%s,%s,%s)'
cursor.execute(query, (username,request.form['userFollow'],0))
conn.commit()
cursor.close()
return render_template('follow.html', msg=msg)
#Authenticates the login
@app.route('/loginAuth', methods=['GET', 'POST'])
def loginAuth():
#grabs information from the forms
username = request.form['username']
password = request.form['password'] + salt
hashed_password = hashlib.sha256(password.encode('utf-8')).hexdigest()
#cursor used to send queries
cursor = conn.cursor()
#executes query
query = 'SELECT * FROM person WHERE username = %s and password = %s'
cursor.execute(query, (username, hashed_password))
#stores the results in a variable
data = cursor.fetchone()
#use fetchall() if you are expecting more than 1 data row
cursor.close()
error = None
if(data):
#creates a session for the the user
#session is a built in
session['username'] = username
return redirect(url_for('home'))
else:
#returns an error message to the html page
error = 'Invalid login or username'
return render_template('login.html', error=error)
#Authenticates the register
@app.route('/registerAuth', methods=['GET', 'POST'])
def registerAuth():
#grabs information from the forms
fname = request.form['Fname']
lname = request.form['Lname']
bio = request.form['bio']
username = request.form['username']
password = request.form['password'] + salt
hashed_password = hashlib.sha256(password.encode('utf-8')).hexdigest()
#cursor used to send queries
cursor = conn.cursor()
#executes query
query = 'SELECT * FROM person WHERE username = %s'
cursor.execute(query, (username))
#stores the results in a variable
data = cursor.fetchone()
#use fetchall() if you are expecting more than 1 data row
error = None
if(data):
#If the previous query returns data, then user exists
error = "This user already exists"
return render_template('register.html', error = error)
else:
ins = 'INSERT INTO person VALUES(%s, %s, %s, %s,%s)'
cursor.execute(ins, (username, hashed_password,fname,lname,bio))
conn.commit()
cursor.close()
return render_template('index.html')
@app.route('/home')
def home():
user = session['username']
return render_template('home.html', username=user)
@app.route('/viewtag', methods=['GET', 'POST'])
def viewtag():
msg=""
username = session['username']
cursor = conn.cursor();
query = 'SELECT photoid FROM taggedin WHERE username = %s AND tagstatus=0'
cursor.execute(query, (username))
unconfirmedtags=cursor.fetchall()
if request.method == 'POST':
msg="successfully updated!"
for line in unconfirmedtags:
if(str(request.form[str(line['photoid'])])=="0"):
query = 'DELETE FROM taggedin WHERE username = %s AND photoid= %s'
cursor.execute(query, (username,line['photoid']))
conn.commit()
else:
query = 'UPDATE taggedin SET tagstatus=1 WHERE username = %s AND photoid = %s'
cursor.execute(query, (username,line['photoid']))
conn.commit()
query = 'SELECT photoid FROM taggedin WHERE username = %s AND tagstatus=0'
cursor.execute(query, (username))
unconfirmedtags=cursor.fetchall()
return render_template('viewtag.html', unconfirmedtags=unconfirmedtags, msg=msg)
@app.route('/tag', methods=['GET', 'POST'])
def tag():
return render_template('tag.html', photoid=request.form['photoidpost'])
@app.route('/tagprocess', methods=['GET', 'POST'])
def tagProccess():
msg=""
username = session['username']
cursor = conn.cursor();
photoidpost = request.form['photoidpost']
user = request.form['userTag']
if(user==username):
#this is someone tagging themselves
query = 'SELECT Username FROM taggedin WHERE username = %s AND photoid = %s'
cursor.execute(query, (user,photoidpost))
data = cursor.fetchall()
if(bool(data)):
msg="Already tagged!"
else:
msg="tagged you in the photo!"
query = 'INSERT INTO taggedin VALUES(%s, %s, %s)'
cursor.execute(query, (user, photoidpost,"1"))
conn.commit()
return render_template('tag.html', photoid=photoidpost, msg=msg)
query = "SELECT username FROM person WHERE username= %s"
cursor.execute(query, user)
data = cursor.fetchall()
if(not bool(data)):
msg = "User doesn't exist."
if(bool(data)):
user2 = [user,user,user,photoidpost]
query = "select photoid FROM photo NATURAL JOIN person WHERE (( %s IN (select username_follower from follow where username_followed = photoposter AND followStatus = 1) AND allFollowers=1) OR photoid IN (Select photoid from sharedwith as s1 where (memberusername = photoposter AND %s IN (select member_username from belongto as b2 where owner_username=s1.ownerUsername AND b2.groupName = s1.groupName))) OR photoposter = %s) AND photoid=%s GROUP BY photoID ORDER BY postingDate DESC "
cursor.execute(query, user2)
data = cursor.fetchall()
if(not bool(data)):
msg="User can't see this photo, can't tag this user."
if(bool(data)):
query = 'SELECT Username FROM taggedin WHERE username = %s AND photoid = %s'
cursor.execute(query, (user,photoidpost))
data = cursor.fetchall()
if(bool(data)):
msg="user already tagged! (or requested)"
else:
msg="tag request sent!"
query = 'INSERT INTO taggedin VALUES(%s, %s, %s)'
cursor.execute(query, (user, photoidpost,"0"))
conn.commit()
likes=cursor.fetchall()
cursor.close()
return render_template('tag.html', photoid=photoidpost, msg=msg)
@app.route('/post', methods=['GET', 'POST'])
def post():
username = session['username']
cursor = conn.cursor();
photoidpost = request.form['photoidpost']
print(photoidpost)
cursor = conn.cursor();
query = "select Username from taggedIn where photoID = %s AND tagStatus = 1"
cursor.execute(query,photoidpost)
taggedList=cursor.fetchall()
query = "select Username,rating from likes where photoID = %s"
cursor.execute(query,photoidpost)
likes=cursor.fetchall()
cursor.close()
print(taggedList)
return render_template('viewinfo.html', data=photoidpost, tagged=taggedList, likedBy=likes)
@app.route('/show_posts', methods=["GET", "POST"])
def show_posts():
user = session['username']
user2 = [user,user,user]
cursor = conn.cursor();
query = "select photoid,postingDate,photoposter,filepath,firstName,lastName FROM photo NATURAL JOIN person WHERE ( %s IN (select username_follower from follow where username_followed = photoposter AND followStatus = 1) AND allFollowers=1) OR photoid IN (Select photoid from sharedwith as s1 where (memberusername = photoposter AND %s IN (select member_username from belongto as b2 where owner_username=s1.ownerUsername AND b2.groupName = s1.groupName))) OR photoposter = %s GROUP BY photoID ORDER BY postingDate DESC "
cursor.execute(query, user2)
data = cursor.fetchall()
cursor.close()
return render_template('show_posts.html', poster_name=user, pictures=data)
@app.route('/logout')
def logout():
session.pop('username')
return redirect('/')
app.secret_key = 'some key that you will never guess'
#Run the app on localhost port 5000
#debug = True -> you don't have to restart flask
#for changes to go through, TURN OFF FOR PRODUCTION
if __name__ == "__main__":
app.run('127.0.0.1', 5000, debug = True)