209
209
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
213
259
# Note that this is the view of an existing worksheet. Creation is handled
214
260
# by OfferingRESTView (as offerings have worksheets)
249
295
@named_operation('save')
250
296
def save(self, req, name, assessable, data, format):
251
297
"""Takes worksheet data and saves it."""
298
generate_exerciselist(self.context, req, data)
252
300
self.context.name = unicode(name)
253
301
self.context.assessable = self.convert_bool(assessable)
254
302
self.context.data = unicode(data)
255
303
self.context.format = unicode(format)
256
ivle.worksheet.update_exerciselist(self.context)
258
305
return {"result": "ok"}
306
353
# This call is added for clarity, as the worksheet is implicitly added.
307
354
req.store.add(new_worksheet)
309
ivle.worksheet.update_exerciselist(new_worksheet)
356
generate_exerciselist(new_worksheet, req, data)
311
358
return {"result": "ok"}