Skip to content

Commit f4763b0

Browse files
prandlaveluca93
authored andcommitted
Refactor common logic of Question handlers, improve comments in macro_question
1 parent 65513ea commit f4763b0

File tree

2 files changed

+28
-46
lines changed

2 files changed

+28
-46
lines changed

cms/server/admin/handlers/contestquestion.py

Lines changed: 25 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -64,25 +64,39 @@ def get(self, contest_id):
6464
self.render("questions.html", **self.r_params)
6565

6666

67-
class QuestionReplyHandler(BaseHandler):
68-
"""Called when the manager replies to a question made by a user.
6967

70-
"""
68+
class QuestionActionHandler(BaseHandler):
69+
"""Base class for handlers for actions on questions."""
70+
71+
def do(self, question: Question):
72+
"""Called on POST requests. Perform the appropriate action on the
73+
question."""
74+
pass
7175

7276
@require_permission(BaseHandler.PERMISSION_MESSAGING)
7377
def post(self, contest_id, question_id):
74-
userid = self.get_argument("user_id", None)
75-
if userid is not None:
76-
ref = self.url("contest", contest_id, "user", userid, "edit")
78+
user_id = self.get_argument("user_id", None)
79+
if user_id is not None:
80+
ref = self.url("contest", contest_id, "user", user_id, "edit")
7781
else:
7882
ref = self.url("contest", contest_id, "questions")
83+
7984
question = self.safe_get_item(Question, question_id)
8085
self.contest = self.safe_get_item(Contest, contest_id)
8186

8287
# Protect against URLs providing incompatible parameters.
8388
if self.contest is not question.participation.contest:
8489
raise tornado_web.HTTPError(404)
8590

91+
self.do(question)
92+
self.redirect(ref)
93+
94+
class QuestionReplyHandler(QuestionActionHandler):
95+
"""Called when the manager replies to a question made by a user.
96+
97+
"""
98+
99+
def do(self, question):
86100
reply_subject_code: str = self.get_argument(
87101
"reply_question_quick_answer", "")
88102
question.reply_text = self.get_argument("reply_question_text", "")
@@ -104,30 +118,14 @@ def post(self, contest_id, question_id):
104118
"question with id %s.",
105119
question.participation.user.username,
106120
question.participation.contest.name,
107-
question_id)
121+
question.id)
108122

109-
self.redirect(ref)
110-
111-
112-
class QuestionIgnoreHandler(BaseHandler):
123+
class QuestionIgnoreHandler(QuestionActionHandler):
113124
"""Called when the manager chooses to ignore or stop ignoring a
114125
question.
115126
116127
"""
117-
@require_permission(BaseHandler.PERMISSION_MESSAGING)
118-
def post(self, contest_id, question_id):
119-
userid = self.get_argument("user_id", None)
120-
if userid is not None:
121-
ref = self.url("contest", contest_id, "user", userid, "edit")
122-
else:
123-
ref = self.url("contest", contest_id, "questions")
124-
question = self.safe_get_item(Question, question_id)
125-
self.contest = self.safe_get_item(Contest, contest_id)
126-
127-
# Protect against URLs providing incompatible parameters.
128-
if self.contest is not question.participation.contest:
129-
raise tornado_web.HTTPError(404)
130-
128+
def do(self, question):
131129
should_ignore = self.get_argument("ignore", "no") == "yes"
132130

133131
# Commit the change.
@@ -141,26 +139,11 @@ def post(self, contest_id, question_id):
141139
question.participation.contest.name,
142140
"ignored" if should_ignore else "unignored")
143141

144-
self.redirect(ref)
145-
146142

147-
class QuestionClaimHandler(BaseHandler):
143+
class QuestionClaimHandler(QuestionActionHandler):
148144
"""Called when the manager chooses to claim or unclaim a question."""
149145

150-
@require_permission(BaseHandler.PERMISSION_MESSAGING)
151-
def post(self, contest_id, question_id):
152-
userid = self.get_argument("user_id", None)
153-
if userid is not None:
154-
ref = self.url("contest", contest_id, "user", userid, "edit")
155-
else:
156-
ref = self.url("contest", contest_id, "questions")
157-
question = self.safe_get_item(Question, question_id)
158-
self.contest = self.safe_get_item(Contest, contest_id)
159-
160-
# Protect against URLs providing incompatible parameters.
161-
if self.contest is not question.participation.contest:
162-
raise tornado_web.HTTPError(404)
163-
146+
def do(self, question):
164147
# Can claim/unclaim only a question not ignored or answered.
165148
if question.ignored or question.reply_timestamp is not None:
166149
raise tornado_web.HTTPError(405)
@@ -180,5 +163,3 @@ def post(self, contest_id, question_id):
180163
question.participation.contest.name,
181164
"claimed" if should_claim else "unclaimed",
182165
self.current_user.name)
183-
184-
self.redirect(ref)

cms/server/admin/templates/macro/question.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
Render one question by a participant.
88

99
msg (Question): the question object.
10-
user_id (int|None): if we are rendering a single user's questions, this is the
11-
user's id. if we are rendering all questions of the contest, then None.
10+
user_id (int|None): if we are rendering one of the questions on a single user's
11+
page, this is the user's id. if we are rendering one of the questions on
12+
the contest questions page, then None.
1213
#}
1314
<div class="notification communication {{ ("answered" if msg.reply_timestamp is not none else ("ignored" if msg.ignored else "")) }}">
1415
<div class="notification_msg">

0 commit comments

Comments
 (0)