23
23
This module provides functions for tutorial and worksheet computations.
26
from storm.locals import And, Asc, Desc
26
from storm.locals import And, Asc, Desc, Store
27
29
import ivle.database
28
from ivle.database import ExerciseAttempt, ExerciseSave, Worksheet
30
from ivle.database import ExerciseAttempt, ExerciseSave, Worksheet, \
31
WorksheetExercise, Exercise
30
33
__all__ = ['get_exercise_status', 'get_exercise_stored_text',
31
34
'get_exercise_attempts', 'get_exercise_attempt',
190
193
if done: mand_done += 1
192
195
return mand_done, mand_total, opt_done, opt_total
197
def update_exerciselist(worksheet):
198
"""Runs through the worksheetstream, generating the appropriate
199
WorksheetExercises, and de-activating the old ones."""
201
# Turns the worksheet into an xml stream, and then finds all the
202
# exercise nodes in the stream.
203
worksheetdata = genshi.XML(worksheet.get_xml())
204
for kind, data, pos in worksheetdata:
205
if kind is genshi.core.START:
206
# Data is a tuple of tag name and a list of name->value tuples
207
if data[0] == 'exercise':
213
if attr[0] == 'optional':
214
optional = attr[1] == 'true'
216
exercises.append((src, optional))
218
# Set all current worksheet_exercises to be inactive
219
db_worksheet_exercises = Store.of(worksheet).find(WorksheetExercise,
220
WorksheetExercise.worksheet_id == worksheet.id)
221
for worksheet_exercise in db_worksheet_exercises:
222
worksheet_exercise.active = False
224
for exerciseid, optional in exercises:
225
worksheet_exercise = Store.of(worksheet).find(WorksheetExercise,
226
WorksheetExercise.worksheet_id == worksheet.id,
227
Exercise.id == WorksheetExercise.exercise_id,
228
Exercise.id == exerciseid).one()
229
if worksheet_exercise is None:
230
exercise = Store.of(worksheet).find(Exercise,
231
Exercise.id == exerciseid
235
worksheet_exercise = WorksheetExercise()
236
worksheet_exercise.worksheet_id = worksheet.id
237
worksheet_exercise.exercise_id = exercise.id
238
Store.of(worksheet).add(worksheet_exercise)
239
worksheet_exercise.active = True
240
worksheet_exercise.seq_no = ex_num
241
worksheet_exercise.optional = optional