27
26
import ivle.console
28
27
import ivle.database
29
28
from ivle.database import Exercise, ExerciseAttempt, ExerciseSave, Worksheet, \
30
29
Offering, Subject, Semester, WorksheetExercise
30
import ivle.worksheet.utils
33
32
import ivle.webapp.tutorial.test
35
33
from ivle.webapp.base.rest import (JSONRESTView, named_operation,
36
34
require_permission)
37
35
from ivle.webapp.errors import NotFound
54
52
self.worksheet_exercise = req.store.find(WorksheetExercise,
55
WorksheetExercise.exercise_id == exercise,
53
WorksheetExercise.exercise_id == unicode(exercise),
56
54
WorksheetExercise.worksheet_id == Worksheet.id,
57
55
Worksheet.offering_id == Offering.id,
56
Worksheet.identifier == unicode(worksheet),
58
57
Offering.subject_id == Subject.id,
59
Subject.code == subject,
58
Subject.short_name == subject,
60
59
Offering.semester_id == Semester.id,
61
60
Semester.year == year,
62
61
Semester.semester == semester).one()
113
112
# Query the DB to get an updated score on whether or not this problem
114
113
# has EVER been completed (may be different from "passed", if it has
115
114
# been completed before), and the total number of attempts.
116
completed, attempts = ivle.worksheet.get_exercise_status(req.store,
117
req.user, self.worksheet_exercise)
115
completed, attempts = ivle.worksheet.utils.get_exercise_status(
116
req.store, req.user, self.worksheet_exercise)
118
117
test_results["completed"] = completed
119
118
test_results["attempts"] = attempts
135
# date = datetime.datetime.strptime(date, TIMESTAMP_FORMAT)
134
date = datetime.datetime.strptime(date, TIMESTAMP_FORMAT)
138
# XXX Hack around Google Code issue #87
139
# Query from the given date +1 secnod.
140
# Date is in seconds (eg. 3:47:12), while the data is in finer time
141
# (eg. 3:47:12.3625). The query "date <= 3:47:12" will fail because
142
# 3:47:12.3625 is greater. Hence we do the query from +1 second,
143
# "date <= 3:47:13", and it finds the correct submission, UNLESS there
144
# are multiple submissions inside the same second.
145
date += datetime.timedelta(seconds=1)
139
147
worksheet_exercise = req.store.find(WorksheetExercise,
140
148
WorksheetExercise.exercise_id == exercise,
142
150
Worksheet.identifier == worksheet,
143
151
Worksheet.offering_id == Offering.id,
144
152
Offering.subject_id == Subject.id,
145
Subject.code == subject,
153
Subject.short_name == subject,
146
154
Offering.semester_id == Semester.id,
147
155
Semester.year == year,
148
156
Semester.semester == semester).one()
150
attempt = req.store.find(ExerciseAttempt,
151
ExerciseAttempt.user_id == user.id,
152
ExerciseAttempt.ws_ex_id == worksheet_exercise.id,
153
ExerciseAttempt.date == date
158
attempt = ivle.worksheet.utils.get_exercise_attempt(req.store, user,
159
worksheet_exercise, as_of=date,
160
allow_inactive=HISTORY_ALLOW_INACTIVE)
156
162
if attempt is None:
163
169
return {'code': self.context.text}
166
class ExerciseRESTView(JSONRESTView):
167
'''REST view of an exercise.'''
169
def get_permissions(self, user):
170
# XXX: Do it properly.
171
# XXX: Does any user have the ability to save as themselves?
172
# XXX: Does a user EVER have permission to save as another user?
178
@named_operation('save')
172
class WorksheetExerciseRESTView(JSONRESTView):
173
'''REST view of a worksheet exercise.'''
175
def __init__(self, req, subject, year, semester, worksheet, exercise):
176
self.context = req.store.find(WorksheetExercise,
177
WorksheetExercise.exercise_id == exercise,
178
WorksheetExercise.worksheet_id == Worksheet.id,
179
Worksheet.offering_id == Offering.id,
180
Offering.subject_id == Subject.id,
181
Subject.short_name == subject,
182
Offering.semester_id == Semester.id,
183
Semester.year == year,
184
Semester.semester == semester).one()
186
if self.context is None:
189
@named_operation('view')
179
190
def save(self, req, text):
180
191
# Find the appropriate WorksheetExercise to save to. If its not found,
181
192
# the user is submitting against a non-existant worksheet/exercise
182
worksheet_exercise = req.store.find(WorksheetExercise,
183
WorksheetExercise.exercise_id == self.exercise,
184
WorksheetExercise.worksheet_id == Worksheet.id,
185
Worksheet.offering_id == Offering.id,
186
Offering.subject_id == Subject.id,
187
Subject.code == self.subject,
188
Offering.semester_id == Semester.id,
189
Semester.year == self.year,
190
Semester.semester == self.semester).one()
192
if worksheet_exercise is None:
195
194
old_save = req.store.find(ExerciseSave,
196
ExerciseSave.ws_ex_id == worksheet_exercise.id,
195
ExerciseSave.ws_ex_id == self.context.id,
197
196
ExerciseSave.user == req.user).one()
199
198
#Overwrite the old, or create a new if there isn't one
211
210
return {"result": "ok"}
214
def generate_exerciselist(worksheet, req, worksheetdata):
215
"""Runs through the worksheetstream, generating the appropriate
216
WorksheetExercises, and de-activating the old ones."""
218
# Turns the worksheet into an xml stream, and then finds all the
219
# exercise nodes in the stream.
220
worksheetdata = genshi.XML(worksheetdata)
221
for kind, data, pos in worksheetdata:
222
if kind is genshi.core.START:
223
# Data is a tuple of tag name and a list of name->value tuples
224
if data[0] == 'exercise':
230
if attr[0] == 'optional':
231
optional = attr[1] == 'true'
233
exercises.append((src, optional))
235
# Set all current worksheet_exercises to be inactive
236
db_worksheet_exercises = req.store.find(WorksheetExercise,
237
WorksheetExercise.worksheet_id == worksheet.id)
238
for worksheet_exercise in db_worksheet_exercises:
239
worksheet_exercise.active = False
241
for exerciseid, optional in exercises:
242
worksheet_exercise = req.store.find(WorksheetExercise,
243
WorksheetExercise.worksheet_id == worksheet.id,
244
Exercise.id == WorksheetExercise.exercise_id,
245
Exercise.id == exerciseid).one()
246
if worksheet_exercise is None:
247
exercise = req.store.find(Exercise,
248
Exercise.id == exerciseid
252
worksheet_exercise = WorksheetExercise()
253
worksheet_exercise.worksheet_id = worksheet.id
254
worksheet_exercise.exercise_id = exercise.id
255
req.store.add(worksheet_exercise)
256
worksheet_exercise.active = True
257
worksheet_exercise.seq_no = ex_num
258
worksheet_exercise.optional = optional
261
213
# Note that this is the view of an existing worksheet. Creation is handled
262
214
# by OfferingRESTView (as offerings have worksheets)
263
215
class WorksheetRESTView(JSONRESTView):
264
216
"""View used to update a worksheet."""
266
def get_permissions(self, user):
267
# XXX: Do it properly.
268
# XXX: Lecturers should be allowed to add worksheets Only to subjects
269
# under their control
271
if user.rolenm == 'admin':
278
218
def __init__(self, req, **kwargs):
280
220
self.worksheet = kwargs['worksheet']
286
226
Worksheet.identifier == self.worksheet,
287
227
Worksheet.offering_id == Offering.id,
288
228
Offering.subject_id == Subject.id,
289
Subject.code == self.subject,
229
Subject.short_name == self.subject,
290
230
Offering.semester_id == Semester.id,
291
231
Semester.year == self.year,
292
232
Semester.semester == self.semester).one()
294
234
if self.context is None:
297
@named_operation('save')
237
@named_operation('edit')
298
238
def save(self, req, name, assessable, data, format):
299
239
"""Takes worksheet data and saves it."""
300
self.generate_exerciselist(self.context, req, data)
302
240
self.context.name = unicode(name)
303
241
self.context.assessable = self.convert_bool(assessable)
304
242
self.context.data = unicode(data)
243
self.context.format = unicode(format)
244
ivle.worksheet.utils.update_exerciselist(self.context)
306
246
return {"result": "ok"}
308
248
class WorksheetsRESTView(JSONRESTView):
309
249
"""View used to update and create Worksheets."""
311
def get_permissions(self, user):
312
# XXX: Do it properly.
313
# XXX: Lecturers should be allowed to add worksheets Only to subjects
314
# under their control
316
if user.rolenm == 'admin':
317
return set(['add_worksheet', 'set_sequence'])
323
251
def __init__(self, req, **kwargs):
325
253
self.subject = kwargs['subject']
329
257
self.context = req.store.find(Offering,
330
258
Offering.subject_id == Subject.id,
331
Subject.code == self.subject,
259
Subject.short_name == self.subject,
332
260
Offering.semester_id == Semester.id,
333
261
Semester.year == self.year,
334
262
Semester.semester == self.semester).one()
354
282
# This call is added for clarity, as the worksheet is implicitly added.
355
283
req.store.add(new_worksheet)
357
generate_exerciselist(new_worksheet, req, data)
285
ivle.worksheet.utils.update_exerciselist(new_worksheet)
359
287
return {"result": "ok"}
361
@named_operation('set_sequence')
362
def seq_sequence(self, req, worksheet_list):
363
"""Takes a list of worksheet-seq_no pairs and updates their
364
corresponding Worksheet objects to match."""
366
for worksheetid, seq_no in worksheet_list:
367
worksheet = req.store.find(Worksheet,
368
Worksheet.offering_id == self.context.id,
369
Worksheet.identifier == worksheetid).one()
370
if worksheet is None:
371
raise NotFound(worksheet)
372
worksheet.seq_no = seq_no
289
@named_operation('edit')
290
def move_up(self, req, worksheetid):
291
"""Takes a list of worksheet-seq_no pairs and updates their
292
corresponding Worksheet objects to match."""
294
worksheet_below = req.store.find(Worksheet,
295
Worksheet.offering_id == self.context.id,
296
Worksheet.identifier == unicode(worksheetid)).one()
297
if worksheet_below is None:
298
raise NotFound('worksheet_below')
299
worksheet_above = req.store.find(Worksheet,
300
Worksheet.offering_id == self.context.id,
301
Worksheet.seq_no == (worksheet_below.seq_no - 1)).one()
302
if worksheet_above is None:
303
raise NotFound('worksheet_above')
305
worksheet_below.seq_no = worksheet_below.seq_no - 1
306
worksheet_above.seq_no = worksheet_above.seq_no + 1
308
return {'result': 'ok'}
310
@named_operation('edit')
311
def move_down(self, req, worksheetid):
312
"""Takes a list of worksheet-seq_no pairs and updates their
313
corresponding Worksheet objects to match."""
315
worksheet_above = req.store.find(Worksheet,
316
Worksheet.offering_id == self.context.id,
317
Worksheet.identifier == unicode(worksheetid)).one()
318
if worksheet_above is None:
319
raise NotFound('worksheet_below')
320
worksheet_below = req.store.find(Worksheet,
321
Worksheet.offering_id == self.context.id,
322
Worksheet.seq_no == (worksheet_above.seq_no + 1)).one()
323
if worksheet_below is None:
324
raise NotFound('worksheet_above')
326
worksheet_below.seq_no = worksheet_below.seq_no - 1
327
worksheet_above.seq_no = worksheet_above.seq_no + 1
374
329
return {'result': 'ok'}