103
110
req.throw_error(req.HTTP_BAD_REQUEST)
105
def handle_save(req, exercise, code, fields):
112
def handle_save(req, exercisename, code, fields):
106
113
"""Handles a save action. This saves the user's code without executing it.
108
115
# Need to open JUST so we know this is a real exercise.
109
116
# (This avoids users submitting code for bogus exercises).
110
exercisefile = util.open_exercise_file(exercise)
117
exercisefile = util.open_exercise_file(exercisename)
111
118
if exercisefile is None:
112
119
req.throw_error(req.HTTP_NOT_FOUND,
113
120
"The exercise was not found.")
114
121
exercisefile.close()
123
exercise = ivle.database.Exercise.get_by_name(req.store, exercisename)
124
ivle.worksheet.save_exercise(req.store, req.user, exercise,
125
unicode(code), datetime.datetime.now())
116
128
req.write('{"result": "ok"}')
121
conn.write_problem_save(
122
login = req.user.login,
123
exercisename = exercise,
124
date = time.localtime(),
129
def handle_test(req, exercise, code, fields):
131
def handle_test(req, exercisesrc, code, fields):
130
132
"""Handles a test action."""
132
exercisefile = util.open_exercise_file(exercise)
134
exercisefile = util.open_exercise_file(exercisesrc)
133
135
if exercisefile is None:
134
136
req.throw_error(req.HTTP_NOT_FOUND,
135
137
"The exercise was not found.")
150
152
# Close the console
155
conn.insert_problem_attempt(
156
login = req.user.login,
157
exercisename = exercise,
158
date = time.localtime(),
159
complete = test_results['passed'],
162
# Query the DB to get an updated score on whether or not this problem
163
# has EVER been completed (may be different from "passed", if it has
164
# been completed before), and the total number of attempts.
165
completed, attempts = conn.get_problem_status(req.user.login,
167
test_results["completed"] = completed
168
test_results["attempts"] = attempts
170
req.write(cjson.encode(test_results))
174
def handle_getattempts(req, exercise):
155
# Get the Exercise from the database
156
exercise = ivle.database.Exercise.get_by_name(req.store, exercisesrc)
158
attempt = ivle.database.ExerciseAttempt(user=req.user,
160
date=datetime.datetime.now(),
161
complete=test_results['passed'],
162
text=unicode(code)) # XXX
164
req.store.add(attempt)
166
# Query the DB to get an updated score on whether or not this problem
167
# has EVER been completed (may be different from "passed", if it has
168
# been completed before), and the total number of attempts.
169
completed, attempts = ivle.worksheet.get_exercise_status(req.store,
171
test_results["completed"] = completed
172
test_results["attempts"] = attempts
174
req.write(cjson.encode(test_results))
176
def handle_getattempts(req, exercisename):
175
177
"""Handles a getattempts action."""
178
attempts = conn.get_problem_attempts(
179
login=req.user.login,
180
exercisename=exercise,
181
allow_inactive=HISTORY_ALLOW_INACTIVE)
182
req.write(cjson.encode(attempts))
178
exercise = ivle.database.Exercise.get_by_name(req.store, exercisename)
179
attempts = ivle.worksheet.get_exercise_attempts(req.store, req.user,
180
exercise, allow_inactive=HISTORY_ALLOW_INACTIVE)
181
# attempts is a list of ExerciseAttempt objects. Convert to dictionaries.
182
time_fmt = lambda dt: datetime.datetime.strftime(dt, TIMESTAMP_FORMAT)
183
attempts = [{'date': time_fmt(a.date), 'complete': a.complete}
185
req.write(cjson.encode(attempts))
186
def handle_getattempt(req, exercise, date):
187
"""Handles a getattempts action. Date is a struct_time."""
190
attempt = conn.get_problem_attempt(
191
login=req.user.login,
192
exercisename=exercise,
194
allow_inactive=HISTORY_ALLOW_INACTIVE)
195
# attempt may be None; will write "null"
196
req.write(cjson.encode({'code': attempt}))
187
def handle_getattempt(req, exercisename, date):
188
"""Handles a getattempts action. Date is a datetime.datetime."""
189
exercise = ivle.database.Exercise.get_by_name(req.store, exercisename)
190
attempt = ivle.worksheet.get_exercise_attempt(req.store, req.user,
191
exercise, as_of=date, allow_inactive=HISTORY_ALLOW_INACTIVE)
192
if attempt is not None:
193
attempt = attempt.text
194
# attempt may be None; will write "null"
195
req.write(cjson.encode({'code': attempt}))