-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
executable file
·371 lines (324 loc) · 12 KB
/
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
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
from flask import Flask, redirect, render_template, request, url_for
import csv
import re
from fernet import *
#Main App Directory
app = Flask(__name__)
monitor = 0
users_symkey_fn = 'files/u_symkey.key'
posts_symkey_fn = 'files/p_symkey.key'
users_fn = 'files/users.csv'
posts_fn = 'files/posts.csv'
authorities_fn = 'files/authorities.csv'
def csv_to_str(reader):
new_string = ""
for row in reader:
joined_string = ",".join(map(str,row))
new_string = new_string + "\n" + joined_string
return new_string.lstrip()
#Start Page
@app.route("/")
def index():
return render_template('index.html')
#Registering for an Account
@app.route('/register', methods = ['GET', 'POST'])
def register():
symkey_fn = users_symkey_fn
target_fn = users_fn
error = None
if request.method == 'POST':
reader = fernet_read_file(symkey_fn, target_fn)
old_string = csv_to_str(reader)
fields = []
fields.append('Student')
regex = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
if (re.fullmatch(regex,request.form['username'])):
fields.append(request.form['username'])
if (request.form['pw1'] == request.form['pw2']):
fields.append(request.form['pw1'])
new_string = ""
for element in fields:
#print(element)
new_string = new_string + element + ","
new_string = old_string + "\n" + new_string
fernet_write_file(symkey_fn, target_fn, new_string.lstrip())
error = 'Account created. Proceed to Login.'
#Checking that the two passwords keyed in are the same
else:
error = 'Passwords do not match. Please try again'
#Checking whether the email is valid
else:
error = 'Invalid Email. Please try again'
return render_template('register.html', error=error)
#Login Page
@app.route('/login', methods=['GET', 'POST'])
def login():
symkey_fn = users_symkey_fn
target_fn = users_fn
error = None #Used to hold Alert Strings
if request.method == 'POST': #When the form is submitted
validated = 0 #flag to determine account validity
reader = fernet_read_file(symkey_fn, target_fn)
for row in reader:
if row[0] == request.form['user'] and row[1] == request.form['username'] and row[2] == request.form['password']:
validated = 1
continue
if validated == 1:
user = request.form['user']
username = request.form['username']
if user == 'Student':
return redirect(url_for('home',username=username))
elif user == 'Staff':
return redirect(url_for('staff',username=username))
else:
error = 'Invalid Credentials. Please try again.'
return render_template('login.html', error=error)
return render_template('login.html', error=error)
#Home Page for Students
@app.route('/home/<username>', methods = ['GET','POST'])
def home(username):
symkey_fn = posts_symkey_fn
target_fn = posts_fn
#when user likes a post
if request.method == 'POST':
lines = fernet_read_file(symkey_fn, target_fn)
#print('line',lines)
new_string = ""
for row in lines:
#print('row:',row)
if int(row[7]) == int(request.form['id']): #Identifies post that was liked
if request.form['category'] == 'post': #Checks whether post or redressal was liked
likeslist = row[3]+username+';' #Append current user into list of people who liked to ensure no duplicate likes
newrow = row
newrow[3] = likeslist
joined_string = ",".join(map(str,newrow))
new_string = new_string + "\n" + joined_string
elif request.form['category'] == 'redressal':
likeslist = row[6]+username+';'
newrow = row
newrow[6] = likeslist
joined_string = ",".join(map(str,newrow))
new_string = new_string + "\n" + joined_string
else:
joined_string = ",".join(map(str,row))
new_string = new_string + "\n" + joined_string
#print('new_string:',new_string)
fernet_write_file(symkey_fn, target_fn, new_string.lstrip())
#display posts
reader = fernet_read_file(symkey_fn, target_fn)
i = 1
postinfo = {}
username = username
for row in reader:
title = row[1]
text = row[2]
likesnumber = len(row[3].split(';'))-1
redressal = row[5]
redressallikes = len(row[6].split(';'))-1
id = row[7]
if username in row[3]:
likedbyme = 1
else:
likedbyme = 0
if username in row[6]:
r_likedbyme = 1
else:
r_likedbyme = 0
post = {}
post['Title'] = title
post['Text'] = text
post['Likes'] = likesnumber
post['Liked'] = likedbyme
post['Redressal'] = redressal
post['R_likes'] = redressallikes
post['R_likedbyme'] = r_likedbyme
post['id'] = id
postinfo[i] = post
i = i+1
return render_template('home.html', **locals())
#Home Page for Staff
@app.route('/staff/<username>', methods = ['GET','POST'])
def staff(username):
symkey_fn = posts_symkey_fn
target_fn = posts_fn
#when user likes a post
if request.method == 'POST':
if request.method == 'POST':
lines = fernet_read_file(symkey_fn, target_fn)
#print('line',lines)
new_string = ""
for row in lines:
#print('row:',row)
if int(row[7]) == int(request.form['id']):
if request.form['category'] == 'post':
likeslist = row[3]+username+';'
newrow = row
newrow[3] = likeslist
joined_string = ",".join(map(str,newrow))
new_string = new_string + "\n" + joined_string
elif request.form['category'] == 'redressal':
likeslist = row[6]+username+';'
newrow = row
newrow[6] = likeslist
joined_string = ",".join(map(str,newrow))
new_string = new_string + "\n" + joined_string
else:
joined_string = ",".join(map(str,row))
new_string = new_string + "\n" + joined_string
#print('new_string:',new_string)
fernet_write_file(symkey_fn, target_fn, new_string.lstrip())
#display posts
reader = fernet_read_file(symkey_fn, target_fn)
i = 1
postinfo = {}
username = username
for row in reader:
title = row[1]
text = row[2]
likesnumber = len(row[3].split(';'))-1
redressal = row[5]
redressallikes = len(row[6].split(';'))-1
id = row[7]
if username in row[3]:
likedbyme = 1
else:
likedbyme = 0
if username in row[6]:
r_likedbyme = 1
else:
r_likedbyme = 0
post = {}
post['Title'] = title
post['Text'] = text
post['Likes'] = likesnumber
post['Liked'] = likedbyme
post['Redressal'] = redressal
post['R_likes'] = redressallikes
post['R_likedbyme'] = r_likedbyme
post['id'] = id
postinfo[i] = post
i = i+1
return render_template('staff.html', **locals())
#Add Confession
@app.route('/add/<username>', methods = ['GET', 'POST'])
def add(username):
symkey_fn = posts_symkey_fn
target_fn = posts_fn
error = None
auths = []
#to generate list of authorities option
with open(authorities_fn, mode = 'r') as authlist:
reader = csv.reader(authlist)
for row in reader:
#print(row)
rowtext = str(row) [2:-2]
#print(rowtext)
auths.append(rowtext)
authlist.close()
#print(auths)
if request.method == 'POST':
#to determine ID
reader = fernet_read_file(symkey_fn, target_fn)
old_string = csv_to_str(reader)
i = 1
reader = fernet_read_file(symkey_fn, target_fn)
for row in reader:
i += 1
#print("i = ", i)
#Format: Authority,Title,Post,PostLikes,Email,Redressal,RedressalLikes,ID
new_string = ""
fields = []
#print(request.form['auth'])
fields.append(request.form['auth'])
fields.append(request.form['title'])
fields.append(request.form['confession'])
likes = ""
fields.append(likes)
fields.append(username)
redressal = ""
fields.append(redressal)
redressallikes = ""
fields.append(redressallikes)
fields.append(str(i))
#print(fields)
for element in fields:
#print(element)
new_string = new_string + element + ","
new_string = old_string + "\n" + new_string
fernet_write_file(symkey_fn, target_fn, new_string.lstrip())
error = 'Post created.'
return render_template('confession.html', **locals())
#Staff: Redress Confession
@app.route('/redress/<username>', methods = ['GET', 'POST'])
def redress(username):
p_symkey_fn = posts_symkey_fn
p_target_fn = posts_fn
u_symkey_fn = users_symkey_fn
u_target_fn = users_fn
#when authority redresses a post
if request.method == 'POST':
lines = fernet_read_file(p_symkey_fn, p_target_fn)
#print('line',lines)
new_string = ""
for row in lines:
#print('row:',row)
if int(row[7]) == int(request.form['id']):
redressal = request.form['redressal']
newrow = row
newrow[5] = redressal
#print('newrow',newrow)
joined_string = ",".join(map(str,newrow))
new_string = new_string + "\n" + joined_string
else:
joined_string = ",".join(map(str,row))
new_string = new_string + "\n" + joined_string
#print('new_string:',new_string)
fernet_write_file(p_symkey_fn, p_target_fn, new_string.lstrip())
name = ''
readuser = fernet_read_file(u_symkey_fn, u_target_fn)
for row in readuser:
if row[1] == username:
name = row[3]
#print(name, type(name))
continue
reader = fernet_read_file(p_symkey_fn, p_target_fn)
i = 1
postinfo = {}
username = username
pendingredressals = 0
for row in reader:
if row[0] == name:
title = row[1]
text = row[2]
likesnumber = len(row[3].split(';'))-1
redressal = row[5]
if redressal == "":
pendingredressals = pendingredressals + 1
redressallikes = len(row[6])
id = row[7]
if username in row[3]:
likedbyme = 1
else:
likedbyme = 0
if username in row[6]:
r_likedbyme = 1
else:
r_likedbyme = 0
post = {}
post['Title'] = title
post['Text'] = text
post['Likes'] = likesnumber
post['Liked'] = likedbyme
post['Redressal'] = redressal
post['R_likes'] = redressallikes
post['R_likedbyme'] = r_likedbyme
post['id'] = id
postinfo[i] = post
i = i+1
if len(postinfo) == 0:
postinfo['No Confessions for Me!'] = ''
return render_template('redress.html', **locals())
if __name__ == "__main__":
fernet_verify_file(posts_symkey_fn, posts_fn)
fernet_verify_file(users_symkey_fn, users_fn)
app.run(host='0.0.0.0', port=1025)