20
20
'''AJAX backend for the tutorial application.'''
25
from storm.locals import Store
27
28
import ivle.database
28
29
from ivle.database import Exercise, ExerciseAttempt, ExerciseSave, Worksheet, \
29
Offering, Subject, Semester, User, WorksheetExercise
30
Offering, Subject, Semester, WorksheetExercise
30
31
import ivle.worksheet.utils
33
import ivle.webapp.tutorial.test
31
34
from ivle.webapp.base.rest import (JSONRESTView, named_operation,
32
35
require_permission)
33
36
from ivle.webapp.errors import NotFound
38
# If True, getattempts or getattempt will allow browsing of inactive/disabled
39
# attempts. If False, will not allow this.
40
HISTORY_ALLOW_INACTIVE = False
36
42
TIMESTAMP_FORMAT = '%Y-%m-%d %H:%M:%S'
39
44
class AttemptsRESTView(JSONRESTView):
40
45
'''REST view of a user's attempts at an exercise.'''
47
def __init__(self, req, subject, year, semester, worksheet,
49
self.user = ivle.database.User.get_by_login(req.store, username)
53
self.worksheet_exercise = req.store.find(WorksheetExercise,
54
WorksheetExercise.exercise_id == exercise,
55
WorksheetExercise.worksheet_id == Worksheet.id,
56
Worksheet.offering_id == Offering.id,
57
Offering.subject_id == Subject.id,
58
Subject.code == subject,
59
Offering.semester_id == Semester.id,
60
Semester.year == year,
61
Semester.semester == semester).one()
63
self.context = self.user # XXX: Not quite right.
42
65
@require_permission('edit')
43
66
def GET(self, req):
44
67
"""Handles a GET Attempts action."""
45
68
attempts = req.store.find(ExerciseAttempt,
46
ExerciseAttempt.ws_ex_id == self.context.worksheet_exercise.id,
47
ExerciseAttempt.user_id == self.context.user.id)
69
ExerciseAttempt.ws_ex_id == self.worksheet_exercise.id,
70
ExerciseAttempt.user_id == self.user.id)
48
71
# attempts is a list of ExerciseAttempt objects. Convert to dictionaries
49
72
time_fmt = lambda dt: datetime.datetime.strftime(dt, TIMESTAMP_FORMAT)
50
73
attempts = [{'date': time_fmt(a.date), 'complete': a.complete}
56
79
@require_permission('edit')
57
80
def PUT(self, req, data):
58
81
""" Tests the given submission """
59
test_results = ivle.worksheet.utils.test_exercise_submission(
60
req.config, req.user, self.context.worksheet_exercise.exercise,
82
exercise = req.store.find(Exercise,
83
Exercise.id == self.worksheet_exercise.exercise_id).one()
87
# Start a console to run the tests on
88
jail_path = os.path.join(ivle.conf.jail_base, req.user.login)
89
working_dir = os.path.join("/home", req.user.login)
90
cons = ivle.console.Console(req.user.unixid, jail_path, working_dir)
92
# Parse the file into a exercise object using the test suite
93
exercise_obj = ivle.webapp.tutorial.test.parse_exercise_file(
96
# Run the test cases. Get the result back as a JSONable object.
98
test_results = exercise_obj.run_tests(data['code'])
63
103
attempt = ivle.database.ExerciseAttempt(user=req.user,
64
worksheet_exercise = self.context.worksheet_exercise,
104
worksheet_exercise = self.worksheet_exercise,
65
105
date = datetime.datetime.now(),
66
106
complete = test_results['passed'],
67
107
text = unicode(data['code'])
83
123
class AttemptRESTView(JSONRESTView):
84
124
'''REST view of an exercise attempt.'''
126
def __init__(self, req, subject, year, semester, worksheet, exercise,
128
# TODO: Find exercise within worksheet.
129
user = ivle.database.User.get_by_login(req.store, username)
134
date = datetime.datetime.strptime(date, TIMESTAMP_FORMAT)
138
worksheet_exercise = req.store.find(WorksheetExercise,
139
WorksheetExercise.exercise_id == exercise,
140
WorksheetExercise.worksheet_id == Worksheet.id,
141
Worksheet.identifier == worksheet,
142
Worksheet.offering_id == Offering.id,
143
Offering.subject_id == Subject.id,
144
Subject.code == subject,
145
Offering.semester_id == Semester.id,
146
Semester.year == year,
147
Semester.semester == semester).one()
149
attempt = ivle.worksheet.utils.get_exercise_attempt(req.store, user,
150
worksheet_exercise, as_of=date,
151
allow_inactive=HISTORY_ALLOW_INACTIVE)
156
self.context = attempt
86
158
@require_permission('view')
87
159
def GET(self, req):
88
160
return {'code': self.context.text}
91
163
class WorksheetExerciseRESTView(JSONRESTView):
92
'''REST view of a worksheet exercise.'''
94
@named_operation('view')
164
'''REST view of an exercise.'''
166
def get_permissions(self, user):
167
# XXX: Do it properly.
168
# XXX: Does any user have the ability to save as themselves?
169
# XXX: Does a user EVER have permission to save as another user?
175
@named_operation('save')
95
176
def save(self, req, text):
96
177
# Find the appropriate WorksheetExercise to save to. If its not found,
97
178
# the user is submitting against a non-existant worksheet/exercise
179
worksheet_exercise = req.store.find(WorksheetExercise,
180
WorksheetExercise.exercise_id == self.exercise,
181
WorksheetExercise.worksheet_id == Worksheet.id,
182
Worksheet.offering_id == Offering.id,
183
Offering.subject_id == Subject.id,
184
Subject.code == self.subject,
185
Offering.semester_id == Semester.id,
186
Semester.year == self.year,
187
Semester.semester == self.semester).one()
189
if worksheet_exercise is None:
99
192
old_save = req.store.find(ExerciseSave,
100
ExerciseSave.ws_ex_id == self.context.id,
193
ExerciseSave.ws_ex_id == worksheet_exercise.id,
101
194
ExerciseSave.user == req.user).one()
103
196
#Overwrite the old, or create a new if there isn't one
115
208
return {"result": "ok"}
212
# Note that this is the view of an existing worksheet. Creation is handled
213
# by OfferingRESTView (as offerings have worksheets)
214
class WorksheetRESTView(JSONRESTView):
215
"""View used to update a worksheet."""
217
def get_permissions(self, user):
218
# XXX: Do it properly.
219
# XXX: Lecturers should be allowed to add worksheets Only to subjects
220
# under their control
229
def __init__(self, req, **kwargs):
231
self.worksheet = kwargs['worksheet']
232
self.subject = kwargs['subject']
233
self.year = kwargs['year']
234
self.semester = kwargs['semester']
236
self.context = req.store.find(Worksheet,
237
Worksheet.identifier == self.worksheet,
238
Worksheet.offering_id == Offering.id,
239
Offering.subject_id == Subject.id,
240
Subject.code == self.subject,
241
Offering.semester_id == Semester.id,
242
Semester.year == self.year,
243
Semester.semester == self.semester).one()
245
if self.context is None:
248
@named_operation('save')
249
def save(self, req, name, assessable, data, format):
250
"""Takes worksheet data and saves it."""
251
self.context.name = unicode(name)
252
self.context.assessable = self.convert_bool(assessable)
253
self.context.data = unicode(data)
254
self.context.format = unicode(format)
255
ivle.worksheet.utils.update_exerciselist(self.context)
257
return {"result": "ok"}
118
259
class WorksheetsRESTView(JSONRESTView):
119
260
"""View used to update and create Worksheets."""
121
@named_operation('edit_worksheets')
262
def get_permissions(self, user):
263
# XXX: Do it properly.
264
# XXX: Lecturers should be allowed to add worksheets Only to subjects
265
# under their control
274
def __init__(self, req, **kwargs):
276
self.subject = kwargs['subject']
277
self.year = kwargs['year']
278
self.semester = kwargs['semester']
280
self.context = req.store.find(Offering,
281
Offering.subject_id == Subject.id,
282
Subject.code == self.subject,
283
Offering.semester_id == Semester.id,
284
Semester.year == self.year,
285
Semester.semester == self.semester).one()
287
if self.context is None:
290
@named_operation('edit')
291
def add_worksheet(self, req, identifier, name, assessable, data, format):
292
"""Takes worksheet data and adds it."""
294
new_worksheet = Worksheet()
295
new_worksheet.seq_no = self.context.worksheets.count()
296
# Setting new_worksheet.offering implicitly adds new_worksheet,
297
# hence worksheets.count MUST be called above it
298
new_worksheet.offering = self.context
299
new_worksheet.identifier = unicode(identifier)
300
new_worksheet.name = unicode(name)
301
new_worksheet.assessable = self.convert_bool(assessable)
302
new_worksheet.data = unicode(data)
303
new_worksheet.format = unicode(format)
305
# This call is added for clarity, as the worksheet is implicitly added.
306
req.store.add(new_worksheet)
308
ivle.worksheet.utils.update_exerciselist(new_worksheet)
310
return {"result": "ok"}
312
@named_operation('edit')
122
313
def move_up(self, req, worksheetid):
123
314
"""Takes a list of worksheet-seq_no pairs and updates their
124
315
corresponding Worksheet objects to match."""