forked from cc-d/ieddit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.py
435 lines (365 loc) · 12.8 KB
/
mod.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
from ieddit import *
import json
bp = Blueprint('mod', 'mod', url_prefix='/mod')
def mod_action(username, action, url, sub):
new_action = Mod_action(username=username, action=action, url=url, sub=sub)
db.session.add(new_action)
db.session.commit()
@bp.route('/delete/post', methods=['POST'])
def mod_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 'admin' in session:
is_mod = True
else:
is_mod = db.session.query(db.session.query(Moderator).filter(Moderator.username.like(session['username']),
Moderator.sub.like(post.sub)).exists()).scalar()
if is_mod:
#db.session.delete(post)
msg = 'deleted'
if post.deleted == True:
post.deleted = False
msg = 'undeleted'
else:
post.deleted = True
mod_action(session['username'], msg, post.get_permalink(), post.sub)
db.session.commit()
flash('post %s' % msg, category='success')
return redirect(sub_url)
else:
return '403'
@bp.route('/delete/comment', methods=['POST'])
def mod_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 'admin' in session:
is_mod = True
else:
is_mod = db.session.query(db.session.query(Moderator).filter(Moderator.username.like(session['username']),
Moderator.sub.like(post.sub)).exists()).scalar()
if is_mod:
comment.deleted = True
mod_action(session['username'], 'delete', comment.get_permalink(), comment.sub_name)
db.session.commit()
flash('comment deleted', category='success')
return redirect(post.get_permalink())
else:
return '403'
@bp.route('/sticky/post', methods=['POST'])
def mod_sticky_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()
if 'admin' in session:
is_mod = True
else:
is_mod = db.session.query(db.session.query(Moderator).filter(Moderator.username.like(session['username']),
Moderator.sub.like(post.sub)).exists()).scalar()
if is_mod:
old_sticky = db.session.query(Post).filter_by(sub=post.sub, stickied=True).all()
for s in old_sticky:
s.stickied = False
db.session.commit()
mod_action(session['username'], 'unstickied', post.get_permalink(), post.sub)
post.stickied = True
db.session.commit()
mod_action(session['username'], 'stickied', post.get_permalink(), post.sub)
flash('stickied post', category='success')
return redirect(config.URL + '/i/' + post.sub)
else:
return '403'
@bp.route('/unsticky/post', methods=['POST'])
def mod_unsticky_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()
if 'admin' in session:
is_mod = True
else:
is_mod = db.session.query(db.session.query(Moderator).filter(Moderator.username.like(session['username']),
Moderator.sub.like(post.sub)).exists()).scalar()
if is_mod:
mod_action(session['username'], 'unstickied', post.get_permalink(), post.sub)
post.stickied = False
db.session.commit()
flash('unstickied post', category='success')
return redirect(config.URL + '/i/' + post.sub)
else:
return '403'
@bp.route('/lock/post', methods=['POST'])
def mod_lock_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()
if post.locked == False:
action = 'lock'
else:
action = 'unlock'
if 'admin' in session:
is_mod = True
else:
is_mod = db.session.query(db.session.query(Moderator).filter(Moderator.username.like(session['username']),
Moderator.sub.like(post.sub)).exists()).scalar()
if is_mod:
if action == 'lock':
mod_action(session['username'], 'locked', post.get_permalink(), post.sub)
post.locked = True
flash('locked post', category='success')
elif action == 'unlock':
mod_action(session['username'], 'unlocked', post.get_permalink(), post.sub)
post.locked = False
flash('unlocked post', category='success')
db.session.commit()
return redirect(post.get_permalink())
else:
return '403'
@bp.route('/ban', methods=['POST'])
def mod_ban_user():
if 'username' not in session:
flash('not logged in', 'error')
return redirect(url_for('login'))
dropdown = request.form.get('post_id')
if dropdown != None:
if dropdown.find('|') != -1:
params = dropdown.split('|')
iid = params[1]
itype = params[2]
else:
iid = request.form.get('iid')
itype = request.form.get('itype')
else:
iid = request.form.get('iid')
itype = request.form.get('itype')
is_mod = True
if is_mod:
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()
if obj.anonymous:
anon = True
else:
anon = False
if hasattr(obj, 'sub_name'):
is_mod = db.session.query(db.session.query(Moderator).filter(Moderator.username.like(session['username']),
Moderator.sub.like(obj.sub_name)).exists()).scalar()
if is_mod == False:
if 'admin' in session:
is_mod = True
if is_mod != True:
return '403'
new_ban = Ban(sub=obj.sub_name, username=obj.author, anonymous=anon)
sub = obj.sub_name
db.session.add(new_ban)
else:
is_mod = db.session.query(db.session.query(Moderator).filter(Moderator.username.like(session['username']),
Moderator.sub.like(obj.sub)).exists()).scalar()
if is_mod == False:
if 'admin' in session:
is_mod = True
if is_mod != True:
return '403'
new_ban = Ban(sub=obj.sub, username=obj.author, anonymous=anon)
sub = obj.sub
db.session.add(new_ban)
mod_action(session['username'], 'ban', obj.get_permalink(), sub)
db.session.commit()
flash('banned ' + obj.author + ' from ' + sub, category='success')
return redirect(config.URL + '/i/' + sub)
else:
return '403'
@bp.route('/unban', methods=['POST'])
def mod_unban_user():
if 'username' not in session:
flash('not logged in', 'error')
return redirect(url_for('login'))
username = request.form.get('username')
username = normalize_username(username)
ban_id = request.form.get('ban_id')
sub = request.form.get('sub')
if 'admin' in session:
is_mod = True
else:
is_mod = db.session.query(db.session.query(Moderator).filter(Moderator.username.like(session['username']),
Moderator.sub.like(sub)).exists()).scalar()
if is_mod:
uban = db.session.query(Ban).filter_by(id=ban_id, sub=sub)
uban.delete()
db.session.commit()
mod_action(session['username'], 'unban', username, sub)
flash('unbanned ' + username, 'success')
return redirect('/i/' + sub + '/mods/banned/')
@bp.route('/add', methods=['POST'])
def mod_addmod():
if 'username' not in session:
flash('not logged in', 'error')
return redirect(url_for('login'))
username = request.form.get('username')
sub = request.form.get('sub')
username = db.session.query(Iuser).filter(func.lower(Iuser.username) == func.lower(username)).first()
username = username.username
if db.session.query(Moderator).filter(func.lower(Moderator.username) == func.lower(username), Moderator.sub == sub).first() != None:
flash('Mod already added', 'error')
return redirect('/i/%s/mods/' % sub)
if 'admin' in session:
is_mod = True
else:
is_mod = db.session.query(db.session.query(Moderator).filter(Moderator.username.like(session['username']),
Moderator.sub.like(sub)).exists()).scalar()
if is_mod:
mods = db.session.query(Moderator).filter_by(sub=sub).all()
rank = 0
if mods != None:
for x in mods:
if x.rank > rank:
rank = x.rank
new_mod = Moderator(sub=sub, username=username, rank=rank)
db.session.add(new_mod)
db.session.commit()
flash('new moderator ' + username, 'success')
return redirect('/i/' + sub + '/mods/')
else:
return '403'
@bp.route('/remove', methods=['POST'])
def mod_removemod():
if 'username' not in session:
flash('not logged in', 'error')
return redirect(url_for('login'))
username = normalize_username(request.form.get('username'))
sub = request.form.get('sub')
if not username:
flash ('invalid username', 'error')
return redirect('/i/' + sub + '/mods/')
if 'admin' in session:
is_mod = True
else:
is_mod = db.session.query(db.session.query(Moderator).filter(Moderator.username.like(session['username']),
Moderator.sub.like(sub)).exists()).scalar()
if is_mod:
sub = normalize_sub(sub)
delmod = db.session.query(Moderator).filter_by(username=username, sub=sub).first()
you = db.session.query(Moderator).filter_by(username=session['username'], sub=sub).first()
if delmod.rank < you.rank:
flash('cannot delete mod of higher rank')
return redirect('/i/' + sub + '/mods/')
db.session.query(Moderator).filter_by(username=delmod.username, sub=sub).delete()
db. session.commit()
flash('deleted mod')
return redirect('/i/' + sub + '/mods/')
@bp.route('/edit/description', methods=['POST'])
def mod_editrules():
sub = request.form.get('sub')
rtext = request.form.get('rtext')
if 'username' not in session:
flash('not logged in', 'error')
return redirect(url_for('login'))
if len(rtext) < 1 or len(rtext) > 20000:
return 'invalid rules text lngth'
if 'admin' in session:
is_mod = True
else:
is_mod = db.session.query(db.session.query(Moderator).filter(Moderator.username.like(session['username']),
Moderator.sub.like(sub)).exists()).scalar()
if is_mod:
desc = db.session.query(Sub).filter_by(name=sub).first()
desc.rules = rtext
db.session.add(desc)
db.session.commit()
flash('successfully updated description')
return(redirect('/i/' + sub + '/info/'))
else:
return '403'
@bp.route('/nsfw', methods=['POST'])
def mod_marknsfw():
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 'admin' in session:
is_mod = True
else:
is_mod = db.session.query(db.session.query(Moderator).filter(Moderator.username.like(session['username']),
Moderator.sub.like(post.sub)).exists()).scalar()
if is_mod:
post.nsfw = True
flash('marked as nsfw')
db.session.commit()
return redirect(post.get_permalink())
return '403'
@bp.route('/title', methods=['POST'])
def mod_title():
sub = request.form.get('sub')
title = request.form.get('title')
if 'username' not in session:
flash('not logged in', 'error')
return redirect(url_for('login'))
if len(title) > 1000:
return 'invalid title lngth'
elif len(title) == 0:
title = None
if 'admin' in session:
is_mod = True
else:
is_mod = db.session.query(db.session.query(Moderator).filter(Moderator.username.like(session['username']),
Moderator.sub.like(sub)).exists()).scalar()
if is_mod:
desc = db.session.query(Sub).filter_by(name=sub).first()
desc.title = title
db.session.add(desc)
db.session.commit()
flash('successfully updated title')
return(redirect('/i/' + sub + '/info/'))
else:
return '403'
@bp.route('/settings', methods=['POST'])
def mod_settings():
sub = request.form.get('sub')
subr = normalize_sub(sub)
marknsfw = request.form.get('marknsfw')
newcss = request.form.get('newcss')
if 'username' not in session:
flash('not logged in', 'error')
return redirect(url_for('login'))
if 'admin' in session:
is_mod = True
else:
is_mod = db.session.query(db.session.query(Moderator).filter(Moderator.username.like(session['username']),
Moderator.sub.like(sub)).exists()).scalar()
if is_mod:
sub = db.session.query(Sub).filter_by(name=sub).first()
if marknsfw:
if request.form.get('alsoposts'):
for p in db.session.query(Post).filter_by(sub=subr):
p.nsfw = True
db.session.commit()
sub.nsfw = True
else:
if request.form.get('alsoposts'):
for p in db.session.query(Post).filter_by(sub=subr):
p.nsfw = False
db.session.commit()
sub.nsfw = False
if newcss != None:
if len(newcss) < 20000:
sub.css = newcss
db.session.add(sub)
db.session.commit()
flash('successfully updated settings', 'success')
return(redirect('/i/' + sub.name + '/info/'))
else:
return '403'