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

« back to all changes in this revision

Viewing changes to ivle/webapp/tutorial/service.py

  • Committer: William Grant
  • Date: 2009-02-24 23:50:36 UTC
  • Revision ID: grantw@unimelb.edu.au-20090224235036-mj7x2x8daw1g31ua
Move ivle.webapp.tutorial.service.generate_exerciselist() to ivle.worksheet,
and make the arguments a bit more sane.

Show diffs side-by-side

added added

removed removed

Lines of Context:
209
209
        return {"result": "ok"}
210
210
 
211
211
 
212
 
def generate_exerciselist(worksheet, req, worksheetdata):
213
 
    """Runs through the worksheetstream, generating the appropriate
214
 
    WorksheetExercises, and de-activating the old ones."""
215
 
    exercises = []
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':
223
 
                src = ""
224
 
                optional = False
225
 
                for attr in data[1]:
226
 
                    if attr[0] == 'src':
227
 
                        src = attr[1]
228
 
                    if attr[0] == 'optional':
229
 
                        optional = attr[1] == 'true'
230
 
                if src != "":
231
 
                    exercises.append((src, optional))
232
 
    ex_num = 0
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
238
 
    
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
247
 
            ).one()
248
 
            if exercise is None:
249
 
                raise NotFound()
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
257
 
 
258
212
 
259
213
# Note that this is the view of an existing worksheet. Creation is handled
260
214
# by OfferingRESTView (as offerings have worksheets)
295
249
    @named_operation('save')
296
250
    def save(self, req, name, assessable, data, format):
297
251
        """Takes worksheet data and saves it."""
298
 
        generate_exerciselist(self.context, req, data)
299
 
        
300
252
        self.context.name = unicode(name)
301
253
        self.context.assessable = self.convert_bool(assessable)
302
254
        self.context.data = unicode(data)
303
255
        self.context.format = unicode(format)
 
256
        ivle.worksheet.update_exerciselist(self.context)
304
257
        
305
258
        return {"result": "ok"}
306
259
 
353
306
        # This call is added for clarity, as the worksheet is implicitly added.        
354
307
        req.store.add(new_worksheet)
355
308
        
356
 
        generate_exerciselist(new_worksheet, req, data)
 
309
        ivle.worksheet.update_exerciselist(new_worksheet)
357
310
        
358
311
        return {"result": "ok"}
359
312