forked from cc-d/ieddit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.py
516 lines (402 loc) · 14.5 KB
/
user.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
from ieddit import *
import json
ubp = Blueprint('user', 'user', url_prefix='/user')
@ubp.route('/delete/post', methods=['POST'])
def user_delete_post():
if 'username' not in session:
flash('not logged in', 'error')
return redirect(url_for('login'))
pid = request.form.get('post_id')
post = db.session.query(Post).filter_by(id=pid).first()
sub_url = config.URL + '/i/' + post.sub
if post.author == session['username']:
post.deleted = True
db.session.commit()
flash('post deleted', category='success')
return redirect(sub_url)
else:
return '403'
@ubp.route('/delete/comment', methods=['POST'])
def user_delete_comment():
if 'username' not in session:
flash('not logged in', 'error')
return redirect(url_for('login'))
cid = request.form.get('comment_id')
comment = db.session.query(Comment).filter_by(id=cid).first()
post = db.session.query(Post).filter_by(id=comment.post_id).first()
if comment.author == session['username']:
comment.deleted = True
db.session.commit()
flash('post deleted', category='success')
return redirect(post.get_permalink())
else:
return '403'
#@cache.memoize(config.DEFAULT_CACHE_TIME, unless=only_cache_get)
@ubp.route('/edit/<itype>/<iid>/', methods=['GET'])
def user_get_edit(itype, iid):
if 'username' not in session:
flash('not logged in', 'error')
return redirect(url_for('login'))
if itype == 'post':
obj = db.session.query(Post).filter_by(id=iid).first()
if hasattr(obj, 'self_text'):
etext = obj.self_text
else:
return '403'
elif itype == 'comment':
obj = db.session.query(Comment).filter_by(id=iid).first()
etext = obj.text
else:
return 'bad itype'
if 'last_edit' in session:
lastedit = session['last_edit']
else:
lastedit = None
session['last_edit'] = None
return render_template('edit.html', itype=itype, iid=iid, etext=etext, lastedit=lastedit)
@ubp.route('/edit', methods=['POST'])
def user_edit_post():
if 'username' not in session:
flash('not logged in', 'error')
return redirect(url_for('login'))
itype = request.form.get('itype')
iid = request.form.get('iid')
etext = request.form.get('etext')
session['last_edit'] = etext
if len(etext) < 1 or len(etext) > 20000:
return 'invalid edit length'
if config.CAPTCHA_ENABLE:
if request.form.get('captcha') == '':
flash('no captcha', 'danger')
return redirect('/user/edit/%s/%s/' % (itype, iid))
if captcha.validate() == False:
flash('invalid captcha', 'danger')
return redirect('/user/edit/%s/%s/' % (itype, iid))
# if 'rate_limit' in session and config.RATE_LIMIT == True:
# rl = session['rate_limit'] - time.time()
# if rl > 0:
# flash('rate limited, try again in %s seconds' % str(rl))
# return redirect('/user/edit/%s/%s/' % (itype, iid))
if itype == 'post':
obj = db.session.query(Post).filter_by(id=iid).first()
elif itype == 'comment':
obj = db.session.query(Comment).filter_by(id=iid).first()
else:
return 'bad itype'
if obj.author != session['username']:
return '403'
if hasattr(obj, 'self_text'):
obj.self_text = etext
obj.edited = True
db.session.commit()
flash('post edited', category='success')
session['last_edit'] = None
return redirect(obj.get_permalink())
elif hasattr(obj, 'text'):
obj.edited = True
obj.text = etext
db.session.commit()
flash('comment edited', category='success')
session['last_edit'] = None
return redirect(obj.get_permalink())
else:
return '403'
@ubp.route('/nsfw', methods=['POST'])
def user_marknsfw(pid=None):
if 'username' not in session:
flash('not logged in', 'danger')
return redirect(url_for('login'))
post_id = request.form.get('post_id')
post = db.session.query(Post).filter_by(id=post_id).first()
if session['username'] != post.author:
return '403'
post.nsfw = True
flash('marked as nsfw')
db.session.commit()
return redirect(post.get_permalink())
@ubp.route('/darkmode', methods=['POST'])
def user_darkmode(username=None):
if 'username' not in session:
flash('not logged in', 'danger')
return redirect(url_for('login'))
if username is None:
user = db.session.query(Iuser).filter_by(username=session['username']).first()
action = request.form.get('action')
if action == 'disable':
user.darkmode = False
mode = 'light'
session['darkmode'] = False
elif action == 'enable':
user.darkmode = True
mode = 'dark'
session['darkmode'] = True
else:
return 'bad action'
flash('switched to %s mode' % mode, 'success')
db.session.add(user)
db.session.commit()
return redirect('/u/' + user.username)
@ubp.route('/anonymous', methods=['POST'])
def user_uanonymous(username=None):
if 'username' not in session:
flash('not logged in', 'danger')
return redirect(url_for('login'))
if username is None:
user = db.session.query(Iuser).filter_by(username=session['username']).first()
action = request.form.get('action')
if action == 'disable':
user.anonymous = False
session['anonymous'] = False
elif action == 'enable':
user.anonymous = True
session['anonymous'] = True
else:
return 'bad action'
flash('toggled anonymous', 'success')
db.session.add(user)
db.session.commit()
return redirect('/u/' + user.username)
@ubp.route('/reset_password/', methods=['GET'])
#@cache.memoize(config.DEFAULT_CACHE_TIME, unless=only_cache_get)
def reset_page():
return render_template('reset_password.html')
@limiter.limit(['5 per hour'])
@ubp.route('/password_reset', methods=['POST', 'GET'])
#@cache.memoize(config.DEFAULT_CACHE_TIME, unless=only_cache_get)
def password_reset(email=None):
if request.method == 'POST':
if config.CAPTCHA_ENABLE:
if request.form.get('captcha') == '':
flash('no captcha', 'danger')
return redirect('/user/reset_password/')
if captcha.validate() == False:
flash('invalid captcha', 'danger')
return redirect('/user/reset_password/')
email = request.form.get('email')
if email == None:
flash('no email', 'danger')
return redirect('/user/reset_password/')
user = db.session.query(Iuser).filter_by(email=email).first()
if user == None:
flash('no user with email', 'danger')
return redirect('/user/reset_password/')
key = rstring(30)
new_reset = Password_reset(username=user.username, rankey=key, expires=datetime.utcnow() + timedelta(hours=1))
link = config.URL + '/user/password_reset?reset=' + key
etext = 'Please visit this link to reset your password: <a href="%s/user/password_reset?reset=%s">LINK</a>' % (config.URL, key)
e = send_email(etext=etext, subject='Password Reset', to=email)
if e == True:
db.session.add(new_reset)
db.session.commit()
flash('password recovery email sent', 'success')
return redirect('/user/reset_password/')
if request.method == 'GET':
reset = request.args.get('reset')
r = db.session.query(Password_reset).filter_by(rankey=reset).first()
if r == None:
return 'invalid link'
return render_template('new_reset_password.html', reset=reset, username=r.username)
@ubp.route('/new_reset_password', methods=['POST'])
def new_reset_password():
password = request.form.get('password')
reset = request.form.get('reset')
username = request.form.get('username')
if len(password) < 1:
return 'no password'
r = db.session.query(Password_reset).filter_by(rankey=reset).first()
if r == None or r.valid == False:
return 'invalid key or key expired'
#generate_password_hash(password)
r.valid = False
user = db.session.query(Iuser).filter_by(username=username).first()
user.password = generate_password_hash(password)
db.session.add(user)
db.session.add(r)
db.session.commit()
flash('successfully reset password for %s' % username, 'success')
return redirect('/login/')
@ubp.route('/preferences/', methods=['GET'])
#@cache.memoize(config.DEFAULT_CACHE_TIME, unless=only_cache_get)
def user_preferences():
if 'username' not in session:
flash('not logged in', 'danger')
return redirect('/login/')
user = db.session.query(Iuser).filter_by(username=session['username']).first()
return render_template('preferences.html', user=user)
@ubp.route('/update_preferences', methods=['POST'])
def user_update_preferences():
if 'username' not in session:
flash('not logged in', 'danger')
return redirect('/login/')
user = db.session.query(Iuser).filter_by(username=session['username']).first()
new_email = request.form.get('new_email')
new_password = request.form.get('new_password')
con_new_password = request.form.get('con_new_password')
cur_password = request.form.get('cur_password')
update_email, update_password = False, False
if cur_password == None or cur_password == '':
flash('enter current password', 'danger')
return redirect('/user/preferences/')
if new_email != None and new_email != '':
if len(new_email.split('@')) != 2:
flash('invalid email format', 'danger')
return redirect('/user/preferences/')
else:
if len((new_email.split('@')[1]).split('.')) < 2:
flash('invalid email format', 'danger')
return redirect('/user/preferences/')
else:
if len(new_email) < 75:
update_email = True
else:
flash('email too long', 'danger')
return redirect('/user/preferences/')
if new_password != None and new_password != '':
if new_password == con_new_password:
if len(new_password) > 0 and len(new_password) < 200:
update_password = True
else:
flash('invalid password length', 'danger')
return redirect('/user/preferences/')
else:
flash('passwords do not match', 'danger')
return redirect('/user/preferences/')
hashed_pw = user.password
if check_password_hash(hashed_pw, cur_password):
if update_email:
user.email = new_email
if update_password:
user.password = generate_password_hash(new_password)
hss = request.form.get('hide_sub_style')
if user.hide_sub_style == True:
if hss == None:
user.hide_sub_style = False
elif user.hide_sub_style == False:
if hss != None:
user.hide_sub_style = True
session['hide_sub_style'] = user.hide_sub_style
db.session.add(user)
db.session.commit()
flash('successfully updated settings', 'success')
return redirect('/user/preferences/')
else:
flash('incorrect current password', 'danger')
return redirect('/user/preferences/')
@ubp.route('/pgp/', methods=['GET'])
#@cache.memoize(config.DEFAULT_CACHE_TIME, unless=only_cache_get)
def user_pgp():
if 'username' not in session:
flash('not logged in', 'danger')
return redirect('/login/')
user = db.session.query(Iuser).filter_by(username=session['username']).first()
pgp = db.session.query(Pgp).filter_by(username=session['username']).first()
return render_template('pgp.html', user=user, pgp=pgp)
@ubp.route('/addpgp', methods=['POST'])
def user_add_pgp():
if 'username' not in session:
flash('not logged in', 'danger')
return redirect('/login/')
user = db.session.query(Iuser).filter_by(username=session['username']).first()
if user == None:
return 'invalid user'
pgp = db.session.query(Pgp).filter_by(username=session['username']).first()
if pgp != None:
db.session.delete(pgp)
db.session.commit()
privkey = request.form.get('privkey')
pubkey = request.form.get('pubkey')
if privkey == None or privkey == '' or pubkey == None or pubkey == '':
flash('pubkey or privkey empty', 'danger')
return redirect('/user/pgp/')
new_pgp = Pgp(username=user.username, privkey=privkey, pubkey=pubkey)
db.session.add(new_pgp)
db.session.commit()
session['pgp_enabled'] = True
flash('updated pgp key', 'success')
return redirect('/u/' + user.username)
@ubp.route('/hide', methods=['POST'])
def hide_obj():
post_id = None
comment_id = None
other_user = None
d = json.loads(request.form.get('d'))
for k in d.keys():
if k == 'post_id':
post_id = d[k]
elif k == 'comment_id':
comment_id = d[k]
elif k == 'other_user':
other_user = d[k]
if 'username' in session:
new_hidden = Hidden(post_id=post_id, comment_id=comment_id, username=session['username'],
other_user=other_user)
db.session.add(new_hidden)
db.session.commit()
if 'blocked' not in session:
session['blocked'] = {'comment_id':[], 'post_id':[], 'other_user':[]}
d = {'post_id':post_id, 'comment_id':comment_id, 'other_user':other_user}
for i in d.keys():
if d[i] != None:
d[i] = int(d[i])
if d[i] not in session['blocked'][i]:
session['blocked'][i].append(d[i])
return 'ok'
@ubp.route('/show', methods=['POST'])
def show_obj():
post_id = None
comment_id = None
other_user = None
d = json.loads(request.form.get('d'))
for k in d.keys():
if k == 'post_id':
post_id = d[k]
itype = 'post_id'
iid = d[k]
elif k == 'comment_id':
comment_id = d[k]
itype = 'comment_id'
iid = d[k]
elif k == 'other_user':
other_user = d[k]
itype = 'other_user'
iid = d[k]
print(str(session['blocked'][itype]))
if int(iid) not in session['blocked'][itype]:
return 'ok'
if 'username' in session:
hid = db.session.query(Hidden).filter_by(post_id=post_id, comment_id=comment_id, other_user=other_user).delete()
db.session.commit()
print(iid)
session['blocked'][itype].pop(session['blocked'][itype].index(int(iid)))
else:
print(iid)
session['blocked'][itype].pop(session['blocked'][itype].index(int(iid)))
return 'ok'
@cache.memoize(config.DEFAULT_CACHE_TIME, unless=only_cache_get)
def get_total_blocked():
uids, pids, cids = [], [], []
for u in session['blocked']['other_user']:
u2 = db.session.query(Iuser).filter_by(id=int(u)).first()
u2.created_ago = time_ago(u2.created)
u2.type = 'user'
u2.key = 'other_user'
u2.permalink = config.URL + '/u/' + u2.username
uids.append(u2)
for p in session['blocked']['post_id']:
p2 = db.session.query(Post).filter_by(id=int(p)).first()
p2.created_ago = time_ago(p2.created)
p2.type = 'post'
p2.key = 'post_id'
pids.append(p2)
for c in session['blocked']['comment_id']:
c2 = db.session.query(Comment).filter_by(id=int(c)).first()
c2.created_ago = time_ago(c2.created)
c2.type = 'comment'
c2.key = 'comment_id'
cids.append(c2)
blocked = uids + pids + cids
blocked.sort(key=lambda x: x.created, reverse=True)
return blocked
@ubp.route('/blocked/', methods=['GET'])
def show_blocked():
return render_template('blocked.html', blocked=get_total_blocked())