28
28
import ivle.database
29
29
from ivle.database import Exercise, ExerciseAttempt, ExerciseSave, Worksheet, \
30
30
Offering, Subject, Semester, WorksheetExercise
31
import ivle.worksheet.utils
33
33
import ivle.webapp.tutorial.test
34
35
from ivle.webapp.base.rest import (JSONRESTView, named_operation,
35
36
require_permission)
36
37
from ivle.webapp.errors import NotFound
53
54
self.worksheet_exercise = req.store.find(WorksheetExercise,
54
WorksheetExercise.exercise_id == unicode(exercise),
55
WorksheetExercise.exercise_id == exercise,
55
56
WorksheetExercise.worksheet_id == Worksheet.id,
56
57
Worksheet.offering_id == Offering.id,
57
Worksheet.identifier == unicode(worksheet),
58
58
Offering.subject_id == Subject.id,
59
Subject.short_name == subject,
59
Subject.code == subject,
60
60
Offering.semester_id == Semester.id,
61
61
Semester.year == year,
62
62
Semester.semester == semester).one()
113
113
# Query the DB to get an updated score on whether or not this problem
114
114
# has EVER been completed (may be different from "passed", if it has
115
115
# been completed before), and the total number of attempts.
116
completed, attempts = ivle.worksheet.utils.get_exercise_status(
117
req.store, req.user, self.worksheet_exercise)
116
completed, attempts = ivle.worksheet.get_exercise_status(req.store,
117
req.user, self.worksheet_exercise)
118
118
test_results["completed"] = completed
119
119
test_results["attempts"] = attempts
136
136
except ValueError:
139
# XXX Hack around Google Code issue #87
140
# Query from the given date +1 secnod.
141
# Date is in seconds (eg. 3:47:12), while the data is in finer time
142
# (eg. 3:47:12.3625). The query "date <= 3:47:12" will fail because
143
# 3:47:12.3625 is greater. Hence we do the query from +1 second,
144
# "date <= 3:47:13", and it finds the correct submission, UNLESS there
145
# are multiple submissions inside the same second.
146
date += datetime.timedelta(seconds=1)
148
139
worksheet_exercise = req.store.find(WorksheetExercise,
149
140
WorksheetExercise.exercise_id == exercise,
150
141
WorksheetExercise.worksheet_id == Worksheet.id,
151
142
Worksheet.identifier == worksheet,
152
143
Worksheet.offering_id == Offering.id,
153
144
Offering.subject_id == Subject.id,
154
Subject.short_name == subject,
145
Subject.code == subject,
155
146
Offering.semester_id == Semester.id,
156
147
Semester.year == year,
157
148
Semester.semester == semester).one()
159
attempt = ivle.worksheet.utils.get_exercise_attempt(req.store, user,
150
attempt = ivle.worksheet.get_exercise_attempt(req.store, user,
160
151
worksheet_exercise, as_of=date,
161
152
allow_inactive=HISTORY_ALLOW_INACTIVE)
170
161
return {'code': self.context.text}
173
class WorksheetExerciseRESTView(JSONRESTView):
174
'''REST view of a worksheet exercise.'''
176
def __init__(self, req, subject, year, semester, worksheet, exercise):
177
self.context = req.store.find(WorksheetExercise,
178
WorksheetExercise.exercise_id == exercise,
179
WorksheetExercise.worksheet_id == Worksheet.id,
180
Worksheet.offering_id == Offering.id,
181
Offering.subject_id == Subject.id,
182
Subject.short_name == subject,
183
Offering.semester_id == Semester.id,
184
Semester.year == year,
185
Semester.semester == semester).one()
187
if self.context is None:
190
@named_operation('view')
164
class ExerciseRESTView(JSONRESTView):
165
'''REST view of an exercise.'''
167
def get_permissions(self, user):
168
# XXX: Do it properly.
169
# XXX: Does any user have the ability to save as themselves?
170
# XXX: Does a user EVER have permission to save as another user?
176
@named_operation('save')
191
177
def save(self, req, text):
192
178
# Find the appropriate WorksheetExercise to save to. If its not found,
193
179
# the user is submitting against a non-existant worksheet/exercise
180
worksheet_exercise = req.store.find(WorksheetExercise,
181
WorksheetExercise.exercise_id == self.exercise,
182
WorksheetExercise.worksheet_id == Worksheet.id,
183
Worksheet.offering_id == Offering.id,
184
Offering.subject_id == Subject.id,
185
Subject.code == self.subject,
186
Offering.semester_id == Semester.id,
187
Semester.year == self.year,
188
Semester.semester == self.semester).one()
190
if worksheet_exercise is None:
195
193
old_save = req.store.find(ExerciseSave,
196
ExerciseSave.ws_ex_id == self.context.id,
194
ExerciseSave.ws_ex_id == worksheet_exercise.id,
197
195
ExerciseSave.user == req.user).one()
199
197
#Overwrite the old, or create a new if there isn't one
211
209
return {"result": "ok"}
214
213
# Note that this is the view of an existing worksheet. Creation is handled
215
214
# by OfferingRESTView (as offerings have worksheets)
216
215
class WorksheetRESTView(JSONRESTView):
217
216
"""View used to update a worksheet."""
218
def get_permissions(self, user):
219
# XXX: Do it properly.
220
# XXX: Lecturers should be allowed to add worksheets Only to subjects
221
# under their control
219
230
def __init__(self, req, **kwargs):
221
232
self.worksheet = kwargs['worksheet']
227
238
Worksheet.identifier == self.worksheet,
228
239
Worksheet.offering_id == Offering.id,
229
240
Offering.subject_id == Subject.id,
230
Subject.short_name == self.subject,
241
Subject.code == self.subject,
231
242
Offering.semester_id == Semester.id,
232
243
Semester.year == self.year,
233
244
Semester.semester == self.semester).one()
235
246
if self.context is None:
238
@named_operation('edit')
249
@named_operation('save')
239
250
def save(self, req, name, assessable, data, format):
240
251
"""Takes worksheet data and saves it."""
241
252
self.context.name = unicode(name)
242
253
self.context.assessable = self.convert_bool(assessable)
243
254
self.context.data = unicode(data)
244
255
self.context.format = unicode(format)
245
ivle.worksheet.utils.update_exerciselist(self.context)
256
ivle.worksheet.update_exerciselist(self.context)
247
258
return {"result": "ok"}
249
260
class WorksheetsRESTView(JSONRESTView):
250
261
"""View used to update and create Worksheets."""
263
def get_permissions(self, user):
264
# XXX: Do it properly.
265
# XXX: Lecturers should be allowed to add worksheets Only to subjects
266
# under their control
252
275
def __init__(self, req, **kwargs):
254
277
self.subject = kwargs['subject']
258
281
self.context = req.store.find(Offering,
259
282
Offering.subject_id == Subject.id,
260
Subject.short_name == self.subject,
283
Subject.code == self.subject,
261
284
Offering.semester_id == Semester.id,
262
285
Semester.year == self.year,
263
286
Semester.semester == self.semester).one()