~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to ivle/worksheet/utils.py

MergedĀ fromĀ trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
This module provides functions for tutorial and worksheet computations.
24
24
"""
25
25
 
26
 
from storm.locals import And, Asc, Desc
 
26
from storm.locals import And, Asc, Desc, Store
 
27
import genshi
 
28
 
27
29
import ivle.database
28
 
from ivle.database import ExerciseAttempt, ExerciseSave, Worksheet
 
30
from ivle.database import ExerciseAttempt, ExerciseSave, Worksheet, \
 
31
                          WorksheetExercise, Exercise
29
32
 
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
191
194
 
192
195
    return mand_done, mand_total, opt_done, opt_total
 
196
 
 
197
def update_exerciselist(worksheet):
 
198
    """Runs through the worksheetstream, generating the appropriate
 
199
    WorksheetExercises, and de-activating the old ones."""
 
200
    exercises = []
 
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':
 
208
                src = ""
 
209
                optional = False
 
210
                for attr in data[1]:
 
211
                    if attr[0] == 'src':
 
212
                        src = attr[1]
 
213
                    if attr[0] == 'optional':
 
214
                        optional = attr[1] == 'true'
 
215
                if src != "":
 
216
                    exercises.append((src, optional))
 
217
    ex_num = 0
 
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
 
223
    
 
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
 
232
            ).one()
 
233
            if exercise is None:
 
234
                raise NotFound()
 
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
 
242