Skip to content

Commit ccc61cb

Browse files
committed
CWS: API requests are now marked as such
1 parent 0770697 commit ccc61cb

2 files changed

Lines changed: 21 additions & 4 deletions

File tree

cms/server/contest/handlers/api.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,17 @@
3939
logger = logging.getLogger(__name__)
4040

4141

42-
class ApiLoginHandler(ContestHandler):
42+
class ApiContestHandler(ContestHandler):
43+
"""An extension of ContestHandler marking the request as a part of the API.
44+
45+
"""
46+
47+
def __init__(self, *args, **kwargs):
48+
super().__init__(*args, **kwargs)
49+
self.api_request = True
50+
51+
52+
class ApiLoginHandler(ApiContestHandler):
4353
"""Login handler.
4454
4555
"""
@@ -87,7 +97,7 @@ def check_xsrf_cookie(self):
8797
pass
8898

8999

90-
class ApiTaskListHandler(ContestHandler):
100+
class ApiTaskListHandler(ApiContestHandler):
91101
"""Handler to list all tasks and their statements.
92102
93103
"""
@@ -107,7 +117,7 @@ def get(self):
107117
self.json({"tasks": tasks})
108118

109119

110-
class ApiSubmitHandler(ContestHandler):
120+
class ApiSubmitHandler(ApiContestHandler):
111121
"""Handles the received submissions.
112122
113123
"""
@@ -142,7 +152,7 @@ def post(self, task_name: str):
142152
self.json({'id': str(submission.opaque_id)})
143153

144154

145-
class ApiSubmissionListHandler(ContestHandler):
155+
class ApiSubmissionListHandler(ApiContestHandler):
146156
"""Retrieves the list of submissions on a task.
147157
148158
"""

cms/server/contest/handlers/base.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class BaseHandler(CommonRequestHandler):
7070
"""
7171
current_user: Participation | None
7272
service: "ContestWebServer"
73+
api_request: bool
7374

7475
def __init__(self, *args, **kwargs):
7576
super().__init__(*args, **kwargs)
@@ -85,6 +86,8 @@ def __init__(self, *args, **kwargs):
8586
self.translation = DEFAULT_TRANSLATION
8687
self._ = self.translation.gettext
8788
self.n_ = self.translation.ngettext
89+
# Is this a request on an API endpoint?
90+
self.api_request = False
8891

8992
def render(self, template_name, **params):
9093
t = self.service.jinja2_environment.get_template(template_name)
@@ -176,6 +179,10 @@ def is_multi_contest(self):
176179
"""Return whether CWS serves all contests."""
177180
return self.service.contest_id is None
178181

182+
def is_api(self):
183+
"""Return whether it's an API request."""
184+
return self.api_request
185+
179186

180187
class ContestListHandler(BaseHandler):
181188
def get(self):

0 commit comments

Comments
 (0)