forked from cc-d/ieddit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.py
59 lines (42 loc) · 1.35 KB
/
admin.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
from ieddit import *
import json
from functools import wraps
abp = Blueprint('admin', 'admin', url_prefix='/admin')
def admin_only(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if 'admin' not in session:
return '403 ad sess'
if 'username' not in session:
return '403 sess'
admins = db.session.query(Iuser).filter_by(admin=True).all()
anames = [a.username for a in admins]
if session['username'] not in anames:
return '403 names'
return f(*args, **kwargs)
return decorated_function
@abp.route('/', methods=['GET'])
@admin_only
def admincp():
return render_template('admin.html')
@abp.route('/ban_and_delete', methods=['POST'])
@admin_only
def ban_and_delete():
username = request.form.get('username')
username = normalize_username(username)
posts = db.session.query(Post).filter_by(author=username).all()
posts = [p for p in posts]
comments = db.session.query(Comment).filter_by(author=username).all()
comments = [c for c in comments]
user = db.session.query(Iuser).filter_by(username=username).first()
for p in posts:
p.deleted = True
db.session.add(p)
for c in comments:
c.deleted = True
db.session.add(c)
user.banned = True
db.session.add(user)
db.session.commit()
flash('banned and deleted %s' % user.username, 'success')
return redirect('/admin/')