43
44
class AttemptsRESTView(JSONRESTView):
44
45
'''REST view of a user's attempts at an exercise.'''
45
def __init__(self, req, subject, worksheet, exercise, username):
46
# TODO: Find exercise within worksheet.
46
def __init__(self, req, subject, year, semester, worksheet,
47
48
self.user = ivle.database.User.get_by_login(req.store, username)
48
49
if self.user is None:
50
51
self.exercise = exercise
51
53
self.context = self.user # XXX: Not quite right.
53
55
@require_permission('edit')
54
56
def GET(self, req):
55
57
"""Handles a GET Attempts action."""
56
exercise = ivle.database.Exercise.get_by_name(req.store,
58
exercise = req.store.find(Exercise, Exercise.id == self.exercise).one()
59
attempts = ivle.worksheet.get_exercise_attempts(req.store, self.user,
60
exercise, allow_inactive=HISTORY_ALLOW_INACTIVE)
60
attempts = req.store.find(ExerciseAttempt,
61
ExerciseAttempt.exercise_id == exercise.id,
62
ExerciseAttempt.user_id == self.user.id)
61
63
# attempts is a list of ExerciseAttempt objects. Convert to dictionaries
62
64
time_fmt = lambda dt: datetime.datetime.strftime(dt, TIMESTAMP_FORMAT)
63
65
attempts = [{'date': time_fmt(a.date), 'complete': a.complete}
69
71
@require_permission('edit')
70
72
def PUT(self, req, data):
71
73
""" Tests the given submission """
72
exercisefile = ivle.util.open_exercise_file(self.exercise)
73
if exercisefile is None:
74
exercise = req.store.find(Exercise, Exercise.id == self.exercise).one()
76
78
# Start a console to run the tests on
90
91
# Close the console
93
# Get the Exercise from the database
94
exercise = ivle.database.Exercise.get_by_name(req.store, self.exercise)
96
94
attempt = ivle.database.ExerciseAttempt(user=req.user,
98
96
date=datetime.datetime.now(),
155
153
def save(self, req, text):
156
154
# Need to open JUST so we know this is a real exercise.
157
155
# (This avoids users submitting code for bogus exercises).
158
exercisefile = ivle.util.open_exercise_file(self.exercise)
159
if exercisefile is None:
163
exercise = ivle.database.Exercise.get_by_name(req.store, self.exercise)
156
exercise = req.store.find(Exercise,
157
Exercise.id == self.exercise).one()
164
158
ivle.worksheet.save_exercise(req.store, req.user, exercise,
165
159
unicode(text), datetime.datetime.now())
166
160
return {"result": "ok"}