-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblueprint.py
181 lines (159 loc) · 7.37 KB
/
blueprint.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
from flask import Blueprint, redirect, render_template, request, url_for
from CTFd.models import Challenges, Solves, db
from CTFd.utils.config.pages import build_markdown
from CTFd.utils.decorators import admins_only
from CTFd.utils.decorators.visibility import check_challenge_visibility
from CTFd.utils.helpers import markup
from CTFd.utils.logging import log
from CTFd.utils.user import is_admin, get_current_user
from .models import Solutions
plugin_bp = Blueprint('solutions', __name__, template_folder='templates', static_folder='static', static_url_path='/static/solutions')
def load_bp():
@plugin_bp.route('/admin/solutions')
@admins_only
def solutions_list():
q = request.args.get("q")
field = request.args.get("field")
filters = []
if q:
# The field exists as an exposed column
if Challenges.__mapper__.has_property(field):
filters.append(getattr(Challenges, field).like("%{}%".format(q)))
query = Challenges.query.filter(*filters).order_by(Challenges.id.asc())
challenges = query.all()
total = query.count()
for challenge in challenges:
solution = Solutions.query.filter_by(id=challenge.id).first()
if solution:
challenge.solution_state = solution.state
else:
challenge.solution_state = "hidden"
return render_template(
"solution_list.html",
challenges=challenges,
total=total,
q=q,
field=field,
)
@plugin_bp.route('/admin/solutions/<int:challenge_id>')
@admins_only
def solutions_detail(challenge_id):
challenge = Challenges.query.filter_by(id=challenge_id).first_or_404()
solution = Solutions.query.filter_by(id=challenge_id).first()
if not solution:
# There is no solution text for this change yet. Create it
solution = Solutions(id=challenge_id, solution="", state = "hidden")
db.session.add(solution)
db.session.commit()
db.session.flush()
return render_template(
"solution.html",
challenge=challenge,
challenge_html=markup(build_markdown(challenge.description)),
solution=solution,
)
# Overload of /api/v1/challenges/<int:challenge_id>, treat solution
# specific deletion and hand off the rest to the existing function
@plugin_bp.route("/api/v1/solutions/<int:challenge_id>", methods = ['GET', 'DELETE', 'PATCH', 'POST'])
@check_challenge_visibility
def solutions_api(challenge_id):
if request.method == 'GET':
if is_admin():
data = Solutions.query.filter(Solutions.id == challenge_id, Solutions.state != "hidden").first_or_404()
if data:
solution_html = markup(build_markdown(data.solution))
return {"success": True, "data": {"id": data.id,
"solution": data.solution,
"solution_html": solution_html,
"state": data.state}}
else:
return {"success": False}
else:
user = get_current_user()
if user:
solved = Solves.query.filter_by(challenge_id=challenge_id, user_id=user.id).first()
if solved:
visibility = "solved"
else:
visibility = "visible"
else:
visibility = "visible"
data = Solutions.query.filter_by(id=challenge_id).first()
if data and (data.state == "visible" or data.state == visibility):
solution_html = markup(build_markdown(data.solution))
return {"success": True, "data": {"id": data.id,
"solution": data.solution,
"solution_html": solution_html,
"state": data.state}}
else:
return {"success": False}
if not is_admin():
return {"success": False, "errors": "Permission denied"}, 400
if request.method == 'DELETE':
data = Solutions.query.filter_by(id=challenge_id).first_or_404()
if data:
db.session.delete(data)
db.session.commit()
db.session.flush()
return {"success": True}
else:
return {"success": False}, 404
elif request.method == 'PATCH':
data = Solutions.query.filter_by(id=challenge_id).first()
req = request.get_json()
if not data:
# There is no solution text for this change yet. Create it
data = Solutions(id=challenge_id, solution="", state = "hidden")
db.session.add(data)
db.session.commit()
db.session.flush()
if "solution" in req:
data.solution = req["solution"]
if "solution_state" in req:
if req["solution_state"] == "visible":
data.state = "visible"
elif req["solution_state"] == "solved":
data.state = "solved"
elif req["solution_state"] == "admin":
data.state = "admin"
else:
data.state = "hidden"
else:
data.data = "hidden"
solution_html = markup(build_markdown(data.solution))
db.session.commit()
db.session.flush()
return {"success": True, "data": {"id": data.id,
"solution": data.solution,
"solution_html": solution_html,
"state": data.state}}
elif request.method == 'POST':
data = Solutions.query.filter_by(id=challenge_id).first()
req = request.get_json()
if data:
# Create element, but allow overwriting of existing elements
db.session.delete(data)
solution = Solutions(id=challenge_id, solution="", visibility = False)
if "solution" in req:
data.solution = req["solution"]
if "solution_state" in req:
if req["solution_state"] == "visible":
data.state = "visible"
elif req["solution_state"] == "solved":
data.state = "solved"
elif req["solution_state"] == "admin":
data.state = "admin"
else:
data.state = "hidden"
else:
data.data = "hidden"
solution_html = markup(build_markdown(solution.solution))
db.session.commit()
db.session.flush()
return {"success": True, "data": {"id": data.id,
"solution": data.solution,
"solution_html": solution_html,
"state": data.state}}
else:
return {"success": False, "errors": "Permission denied"}, 400
return plugin_bp