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
from ivle.webapp.tutorial.rst import rst as rstfunc
35
34
from ivle.webapp.base.rest import (JSONRESTView, named_operation,
36
35
require_permission)
37
36
from ivle.webapp.errors import NotFound
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
147
146
Semester.year == year,
148
147
Semester.semester == semester).one()
150
attempt = ivle.worksheet.get_exercise_attempt(req.store, user,
149
attempt = ivle.worksheet.utils.get_exercise_attempt(req.store, user,
151
150
worksheet_exercise, as_of=date,
152
151
allow_inactive=HISTORY_ALLOW_INACTIVE)
209
208
return {"result": "ok"}
212
def generate_exerciselist(worksheet, req, worksheetdata):
213
"""Runs through the worksheetstream, generating the appropriate
214
WorksheetExercises, and de-activating the old ones."""
216
# Turns the worksheet into an xml stream, and then finds all the
217
# exercise nodes in the stream.
218
worksheetdata = genshi.XML(worksheetdata)
219
for kind, data, pos in worksheetdata:
220
if kind is genshi.core.START:
221
# Data is a tuple of tag name and a list of name->value tuples
222
if data[0] == 'exercise':
228
if attr[0] == 'optional':
229
optional = attr[1] == 'true'
231
exercises.append((src, optional))
233
# Set all current worksheet_exercises to be inactive
234
db_worksheet_exercises = req.store.find(WorksheetExercise,
235
WorksheetExercise.worksheet_id == worksheet.id)
236
for worksheet_exercise in db_worksheet_exercises:
237
worksheet_exercise.active = False
239
for exerciseid, optional in exercises:
240
worksheet_exercise = req.store.find(WorksheetExercise,
241
WorksheetExercise.worksheet_id == worksheet.id,
242
Exercise.id == WorksheetExercise.exercise_id,
243
Exercise.id == exerciseid).one()
244
if worksheet_exercise is None:
245
exercise = req.store.find(Exercise,
246
Exercise.id == exerciseid
250
worksheet_exercise = WorksheetExercise()
251
worksheet_exercise.worksheet_id = worksheet.id
252
worksheet_exercise.exercise_id = exercise.id
253
req.store.add(worksheet_exercise)
254
worksheet_exercise.active = True
255
worksheet_exercise.seq_no = ex_num
256
worksheet_exercise.optional = optional
259
212
# Note that this is the view of an existing worksheet. Creation is handled
260
213
# by OfferingRESTView (as offerings have worksheets)
295
248
@named_operation('save')
296
249
def save(self, req, name, assessable, data, format):
297
250
"""Takes worksheet data and saves it."""
298
generate_exerciselist(self.context, req, data)
300
251
self.context.name = unicode(name)
301
252
self.context.assessable = self.convert_bool(assessable)
302
253
self.context.data = unicode(data)
303
254
self.context.format = unicode(format)
255
ivle.worksheet.utils.update_exerciselist(self.context)
305
257
return {"result": "ok"}
353
305
# This call is added for clarity, as the worksheet is implicitly added.
354
306
req.store.add(new_worksheet)
356
if new_worksheet.format == u'rst':
357
ws_data = '<worksheet>' + rstfunc(unicode(data)) + '</worksheet>'
358
generate_exerciselist(new_worksheet, req, ws_data)
360
generate_exerciselist(new_worksheet, req, data)
308
ivle.worksheet.utils.update_exerciselist(new_worksheet)
362
310
return {"result": "ok"}
364
312
@named_operation('edit')