-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.py
211 lines (189 loc) · 7.52 KB
/
api.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
import datetime
from flask import (
current_app, Blueprint, request,
Response, stream_with_context,
abort)
from flask_restx import Resource, Namespace
from CTFd.models import db, Solves, Fails
from CTFd.schemas.files import FileSchema
from CTFd.utils import config, get_app_config, user
from CTFd.utils import user as current_user
from CTFd.utils.decorators import (
authed_only, ratelimit, admins_only,
during_ctf_time_only, require_verified_emails
)
from CTFd.utils.decorators.visibility import check_challenge_visibility
from CTFd.utils.uploads import get_uploader
from CTFd.utils.user import authed, get_current_team, get_current_user, is_admin
from .challenge import DynICPCChallenge
# from .api import submission_list, query_details
from .models import JudgeCaseFiles, DynICPCModel, PSubmission
from .schemas import PSubmissionSchema
events = Blueprint("icpc_events", __name__)
submissions_namespace = Namespace("results", description='querying judge results')
cases_namespace = Namespace('cases', description='uploading and downloading judge cases')
challenge_namespace = Namespace('challenge', description='submission endpoint')
@events.route("/events")
@authed_only
@ratelimit(method="GET", limit=150, interval=60)
def subscribe():
@stream_with_context
def gen():
current = user.get_current_user()
for event in current_app.events_manager.subscribe(str(current.id)):
yield str(event)
enabled = get_app_config("SERVER_SENT_EVENTS")
if enabled is False:
return "", 204
return Response(gen(), mimetype="text/event-stream")
@submissions_namespace.route("/", defaults={"filter_result": None})
@submissions_namespace.route('/<filter_result>')
class Submission(Resource):
@authed_only
def get(self, filter_result):
page = abs(int(request.args.get("page", 1, type=int)))
per_page = abs(int(request.args.get("slice", 10, type=int)))
submissions = PSubmission.query
if not is_admin():
submissions = submissions.filter(PSubmission.author != 1)
# don't show admin submissions
if filter_result:
submissions = submissions.filter_by(
result=filter_result
)
submissions = submissions \
.order_by(PSubmission.date.desc()) \
.slice(page * per_page - per_page, page * per_page) \
.all()
schema = PSubmissionSchema(view='admin' if is_admin() else 'user')
return schema.dump(submissions, many=True)
@cases_namespace.route('/<int:challenge_id>')
@cases_namespace.param('challenge_id', 'challenge ID')
class ProgrammingCases(Resource):
@admins_only
def get(self, challenge_id):
files = JudgeCaseFiles.query.filter_by(challenge_id=challenge_id).all()
schema = FileSchema(many=True)
response = schema.dump(files)
if response.errors:
return {"success": False, "errors": response.errors}, 400
return {"success": True, "data": response.data}
@admins_only
def post(self, challenge_id):
files = request.files.getlist("file")
objs = []
for f in files:
if not f.filename or len(f.filename) == 0:
continue
uploader = get_uploader()
location = uploader.upload(file_obj=f, filename=f.filename)
file_row = JudgeCaseFiles(
challenge_id=challenge_id, location=location
)
db.session.add(file_row)
objs.append(file_row)
db.session.commit()
schema = FileSchema(many=True)
response = schema.dump(objs)
if response.errors:
return {"success": False, "errors": response.errorss}, 400
return {"success": True, "data": response.data}
@admins_only
def delete(self, challenge_id):
uploader = get_uploader()
files = JudgeCaseFiles.query.filter_by(challenge_id=challenge_id).all()
for f in files:
uploader.delete(f.location)
JudgeCaseFiles.query.filter_by(challenge_id=challenge_id).delete()
db.session.commit()
db.session.close()
@challenge_namespace.route('/attempt')
class Challenge(Resource):
@check_challenge_visibility
@during_ctf_time_only
@require_verified_emails
def post(self):
if authed() is False:
return {"success": True, "data": {"status": "authentication_required"}}, 403
if request.content_type != "application/json":
request_data = request.form
else:
request_data = request.get_json()
challenge_id = request_data.get("challenge_id")
challenge = DynICPCModel.query.filter_by(id=challenge_id).first_or_404()
user = get_current_user()
team = get_current_team()
if current_user.is_admin():
preview = request.args.get("preview", False)
if preview:
status, message = DynICPCChallenge.real_attempt(user, team, challenge, request)
return {
"success": True,
"data": {
"status": "already_solved" if status else "incorrect",
"message": message,
},
}
if (config.is_teams_mode() and team is None) or (challenge.state == "locked"):
abort(403)
if challenge.state == "hidden":
abort(404)
if challenge.requirements:
requirements = challenge.requirements.get("prerequisites", [])
solve_ids = (
Solves.query.with_entities(Solves.challenge_id)
.filter_by(account_id=user.account_id)
.order_by(Solves.challenge_id.asc())
.all()
)
solve_ids = set([solve_id for solve_id, in solve_ids])
prereqs = set(requirements)
if solve_ids < prereqs:
abort(403)
ten_sec_ago = datetime.datetime.utcnow() + datetime.timedelta(seconds=-10)
if PSubmission.query.filter(PSubmission.date >= ten_sec_ago).first():
return (
{
"success": True,
"data": {
"status": "ratelimited",
"message": "You're submitting too fast. Slow down.",
},
},
429,
)
solves = Solves.query.filter_by(
account_id=user.account_id, challenge_id=challenge_id
).first()
fails = Fails.query.filter_by(
account_id=user.account_id, challenge_id=challenge_id
).count()
if not solves:
max_tries = challenge.max_attempts
if max_tries and fails >= max_tries > 0:
return (
{
"success": True,
"data": {
"status": "incorrect",
"message": "You have 0 tries remaining",
},
},
403,
)
else:
return {
"success": True,
"data": {
"status": "already_solved",
"message": "You already solved this",
},
}
status, message = DynICPCChallenge.real_attempt(user, team, challenge, request)
return {
"success": True,
"data": {
"status": "already_solved" if status else "incorrect",
"message": message,
},
}