-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadminviews.py
More file actions
143 lines (126 loc) · 5.25 KB
/
adminviews.py
File metadata and controls
143 lines (126 loc) · 5.25 KB
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
from views import *
from utils import debug
@app.route('/siteadmin/challenges')
def challenge_suggestions():
from app import get_admin_auth
if not get_admin_auth():
flash('please sign in here and then return to siteadmin')
return redirect('/admin')
return render_template('siteadmin/challenges/challenge.html',json=Suggestion.query.all())
@app.route('/siteadmin/challenges/accept', methods=['GET','POST'])
def admin_accept():
from app import get_admin_auth
if not get_admin_auth():
flash('please sign in here and then return to siteadmin')
return redirect('/admin')
if request.method == 'POST':
suggestion_to_accept = str(request.form.get('suggestion'))
debug('accepting ',suggestion_to_accept)
suggestion = Suggestion.query.filter_by(name=suggestion_to_accept).first()
assert suggestion
s_type = suggestion.type.lower()
assert s_type in dir(ChallengeTypes), "The suggested type is not a valid type"
add_challenge({'type':s_type,'name':suggestion_to_accept})
# once it has been added, delete it from suggestions
db.session.delete(suggestion)
db.session.commit()
return redirect('/siteadmin/challenges')
return render_template('siteadmin/challenges/accept.html',json=Suggestion.query.all())
@app.route('/siteadmin/challenges/delete', methods=['GET','POST'])
def admin_delete():
from app import get_admin_auth
if not get_admin_auth():
flash('please sign in here and then return to siteadmin')
return redirect('/admin')
if request.method == 'POST':
suggestion_to_delete = request.form.get('suggestion')
debug('deleting the suggestion',suggestion_to_delete)
suggestion = Suggestion.query.filter_by(name=suggestion_to_delete).first()
debug(f'deleting {repr(suggestion)} from db')
db.session.delete(suggestion)
db.session.commit()
return redirect('/siteadmin/challenges')
return render_template('siteadmin/challenges/delete.html',json=Suggestion.query.all())
@app.route('/siteadmin/challenges/delete-ch', methods=['GET','POST'])
def admin_delete_ch():
from constants import load_challenge_dict
challenge_dict = load_challenge_dict()
from app import get_admin_auth
if not get_admin_auth():
flash('please sign in here and then return to siteadmin')
return redirect('/admin')
if request.method == 'POST':
ch_to_delete = request.form.get('challenge')
debug('deleting the challenge',ch_to_delete)
debug('old challenge_dict\n\n',challenge_dict)
for lst in challenge_dict.values():
for item in lst:
debug(item)
if item == ch_to_delete:
debug('yay')
lst.remove(item)
with open('database/challenges.json','w') as file:
file.write(json.dumps(challenge_dict))
debug('new challenge_dict:\n\n')
debug(challenge_dict)
delete_all_of_ch(ch_to_delete)
return redirect('/siteadmin/challenges')
return render_template('siteadmin/challenges/delete-ch.html',
json=Suggestion.query.all(),
challenge_dict=challenge_dict)
@app.route('/siteadmin/securityq')
def security_questions():
from app import get_admin_auth
if not get_admin_auth():
flash('please sign in here and then return to siteadmin')
return redirect('/admin')
from constants import load_security_questions
SECURITY_QUESTIONS = load_security_questions()
return render_template('siteadmin/questions/securityq.html',SECURITY_QUESTIONS=SECURITY_QUESTIONS)
@app.route('/siteadmin/securityq/add',methods=['GET','POST'])
def security_question_add():
from app import get_admin_auth
if not get_admin_auth():
flash('please sign in here and then return to siteadmin')
return redirect('/admin')
from constants import load_security_questions
SECURITY_QUESTIONS = load_security_questions()
if request.method == 'POST':
q = request.form.get('question')
if limit_input_size(name=q, max_size=100, item="security question"):
return redirect('/siteadmin/securityq/add')
add_security_question(q)
return redirect('/siteadmin/securityq')
return render_template('siteadmin/questions/add.html',SECURITY_QUESTIONS=SECURITY_QUESTIONS, add=True)
@app.route('/siteadmin/securityq/remove',methods=['GET','POST'])
def security_question_remove():
from app import get_admin_auth
if not get_admin_auth():
flash('please sign in here and then return to siteadmin')
return redirect('/admin')
from constants import load_security_questions
SECURITY_QUESTIONS = load_security_questions()
if request.method == 'POST':
q = request.form.get('question')
status = remove_security_question(q)
if status == False:
flash('You cannot delete that security question because it is already in use by somebody')
return redirect('/siteadmin/securityq')
return render_template('siteadmin/questions/remove.html',SECURITY_QUESTIONS=SECURITY_QUESTIONS, remove=True)
@app.route('/siteadmin/img')
def imgview():
from app import get_admin_auth
if not get_admin_auth():
flash('please sign in here and then return to /siteadmin/img')
return redirect('/admin')
users = User.query.all()
return render_template('siteadmin/img.html',users=users)
@app.route('/siteadmin/<username>/')
def userview(username):
from app import get_admin_auth
if not get_admin_auth():
flash('please sign in here and then return to that page')
return redirect('/admin')
user = User.query.filter_by(username=username).first()
ch = json_to_objects(user.challenges)
return render_template('siteadmin/user.html',user=user,ch=ch)