64
64
exercise = exercise.value
68
handle_save(req, exercise, code, fields)
68
70
handle_test(req, exercise, code, fields)
70
72
handle_run(req, exercise, code, fields)
72
74
req.throw_error(req.HTTP_BAD_REQUEST)
74
def handle_test(req, exercise, code, fields):
75
"""Handles a test action."""
76
def open_exercise_file(exercisename):
77
"""Given an exercise name, opens the corresponding XML file for reading.
78
Returns None if the exercise file was not found.
77
80
# First normalise the path
78
exercise = os.path.normpath(exercise)
81
exercisename = os.path.normpath(exercisename)
79
82
# Now if it begins with ".." or separator, then it's illegal
80
if exercise.startswith("..") or exercise.startswith(os.sep):
83
if exercisename.startswith("..") or exercisename.startswith(os.sep):
81
84
exercisefile = None
83
exercisefile = os.path.join(conf.exercises_base, exercise)
86
exercisefile = os.path.join(conf.exercises_base, exercisename)
86
exercisefile = open(exercisefile)
89
return open(exercisefile)
87
90
except (TypeError, IOError): # TypeError if exercisefile == None
88
req.throw_error(req.HTTP_NOT_FOUND)
93
def handle_save(req, exercise, code, fields):
94
"""Handles a save action. This saves the user's code without executing it.
96
# Need to open JUST so we know this is a real exercise.
97
# (This avoids users submitting code for bogus exercises).
98
exercisefile = open_exercise_file(exercise)
99
if exercisefile is None:
100
req.throw_error(req.HTTP_NOT_FOUND,
101
"The exercise was not found.")
104
req.write('{"result": "ok"}')
108
conn.write_problem_save(
109
login = req.user.login,
110
exercisename = exercise,
111
date = time.localtime(),
114
def handle_test(req, exercise, code, fields):
115
"""Handles a test action."""
117
exercisefile = open_exercise_file(exercise)
118
if exercisefile is None:
119
req.throw_error(req.HTTP_NOT_FOUND,
120
"The exercise was not found.")
90
122
# Parse the file into a exercise object using the test suite
91
123
exercise_obj = test.parse_exercise_file(exercisefile)