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

« back to all changes in this revision

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

Updated service.py to correctly add a seq_no to a worksheet before
it is added to the database. This allows it to have a NOT NULL 
constraint in the schema.

Show diffs side-by-side

added added

removed removed

Lines of Context:
304
304
        
305
305
        return {"result": "ok"}
306
306
 
307
 
class OfferingRESTView(JSONRESTView):
308
 
    """View used to update Offering and create Worksheets."""
 
307
class WorksheetsRESTView(JSONRESTView):
 
308
    """View used to update and create Worksheets."""
309
309
    
310
310
    def get_permissions(self, user):
311
311
        # XXX: Do it properly.
313
313
        #      under their control
314
314
        if user is not None:
315
315
            if user.rolenm == 'admin':
316
 
                return set(['add_worksheet'])
 
316
                return set(['add_worksheet', 'set_sequence'])
317
317
            else:
318
318
                return set()
319
319
        else:
336
336
            raise NotFound()
337
337
 
338
338
    @named_operation('add_worksheet')
339
 
    def add_worksheet(self,req, identifier, name, assessable, data, format):
 
339
    def add_worksheet(self, req, identifier, name, assessable, data, format):
340
340
        """Takes worksheet data and adds it."""
341
341
        
342
342
        new_worksheet = Worksheet()
343
 
        
 
343
        new_worksheet.seq_no = self.context.worksheets.count()
 
344
        # Setting new_worksheet.offering implicitly adds new_worksheet,
 
345
        # hence worksheets.count MUST be called above it
344
346
        new_worksheet.offering = self.context
345
347
        new_worksheet.identifier = unicode(identifier)
346
348
        new_worksheet.name = unicode(name)
347
349
        new_worksheet.assessable = self.convert_bool(assessable)
348
350
        new_worksheet.data = unicode(data)
349
351
        new_worksheet.format = unicode(format)
350
 
        new_worksheet.seq_no = self.context.worksheets.count()
 
352
        
 
353
        # This call is added for clarity, as the worksheet is implicitly added.        
351
354
        req.store.add(new_worksheet)
352
355
        
353
356
        generate_exerciselist(new_worksheet, req, data)
354
357
        
355
358
        return {"result": "ok"}
356
359
 
 
360
    @named_operation('set_sequence')
 
361
    def seq_sequence(self, req, worksheet_list):
 
362
        """Takes a list of worksheet-seq_no pairs and updates their 
 
363
        corresponding Worksheet objects to match."""
 
364
        
 
365
        for worksheetid, seq_no in worksheet_list:
 
366
            worksheet = req.store.find(Worksheet,
 
367
                Worksheet.offering_id == self.context.id,
 
368
                Worksheet.identifier == worksheetid).one()
 
369
            if worksheet is None:
 
370
                raise NotFound(worksheet)
 
371
            worksheet.seq_no = seq_no
 
372
        
 
373
        return {'result': 'ok'}