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

« back to all changes in this revision

Viewing changes to ivle/database.py

Updated the exercises to be loaded from the database, not a local file.

This updated fixes some things broken in my previous commit, and now
it should leave the worksheets in a viewable (not working) state.

I have also updated ivle-addexercise and the database schema to reflect
the schema of an exercise.

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
            'ProjectSet', 'Project', 'ProjectGroup', 'ProjectGroupMembership',
40
40
            'Exercise', 'Worksheet', 'WorksheetExercise',
41
41
            'ExerciseSave', 'ExerciseAttempt',
42
 
            'AlreadyEnrolledError', 'TestCase', 'TestSuite'
 
42
            'AlreadyEnrolledError', 'TestCase', 'TestSuite', 'TestSuiteVar'
43
43
        ]
44
44
 
45
45
def _kwarg_init(self, **kwargs):
228
228
        perms = set()
229
229
        if user is not None:
230
230
            perms.add('view')
231
 
        if user.rolenm == 'admin':
232
 
            perms.add('edit')
 
231
            if user.rolenm == 'admin':
 
232
                perms.add('edit')
233
233
        return perms
234
234
 
235
235
class Semester(Storm):
284
284
        e = Enrolment(user=user, offering=self, active=True)
285
285
        self.enrolments.add(e)
286
286
 
 
287
    def get_permissions(self, user):
 
288
        perms = set()
 
289
        if user is not None:
 
290
            perms.add('view')
 
291
            if user.rolenm == 'admin':
 
292
                perms.add('edit')
 
293
        return perms
 
294
 
287
295
class Enrolment(Storm):
288
296
    __storm_table__ = "enrolment"
289
297
    __storm_primary__ = "user_id", "offering_id"
413
421
    def __repr__(self):
414
422
        return "<%s %s>" % (type(self).__name__, self.name)
415
423
 
416
 
    @classmethod
417
 
    def get_by_name(cls, store, name):
418
 
        """
419
 
        Get the Exercise from the db associated with a given store and name.
420
 
        If the exercise is not in the database, creates it and inserts it
421
 
        automatically.
422
 
        """
423
 
        ex = store.find(cls, cls.name == unicode(name)).one()
424
 
        if ex is not None:
425
 
            return ex
426
 
        ex = Exercise(name=unicode(name))
427
 
        store.add(ex)
428
 
        store.commit()
429
 
        return ex
 
424
#    @classmethod
 
425
#    def get_by_name(cls, store, name):
 
426
#        """
 
427
#        Get the Exercise from the db associated with a given store and name.
 
428
#        If the exercise is not in the database, creates it and inserts it
 
429
#        automatically.
 
430
#        """
 
431
#        ex = store.find(cls, cls.name == unicode(name)).one()
 
432
#        if ex is not None:
 
433
#            return ex
 
434
#        ex = Exercise(name=unicode(name))
 
435
#        store.add(ex)
 
436
#        store.commit()
 
437
#        return ex
430
438
 
431
439
class Worksheet(Storm):
432
440
    __storm_table__ = "worksheet"
440
448
    assessable = Bool()
441
449
    mtime = DateTime()
442
450
 
 
451
    attempts = ReferenceSet(id, "ExerciseAttempt.worksheetid")
443
452
    offering = Reference(offering_id, 'Offering.id')
444
453
 
445
454
    exercises = ReferenceSet(id,
478
487
        """
479
488
        store.find(WorksheetExercise,
480
489
            WorksheetExercise.worksheet == self).remove()
 
490
            
 
491
    def get_permissions(self, user):
 
492
        return self.offering.get_permissions(user)
481
493
 
482
494
class WorksheetExercise(Storm):
483
495
    __storm_table__ = "worksheet_problem"
554
566
    
555
567
    suiteid = Int()
556
568
    exercise_id = Unicode(name="problemid")
 
569
    description = Unicode()
 
570
    seq_no = Int()
 
571
    function = Unicode()
 
572
    stdin = Unicode()
557
573
    exercise = Reference(exercise_id, Exercise.id)
558
574
    test_cases = ReferenceSet(suiteid, 'TestCase.suiteid')
559
 
    description = Unicode()
560
 
    seq_no = Int()
 
575
    variables = ReferenceSet(suiteid, 'TestSuiteVar.suiteid')
561
576
 
562
577
class TestCase(Storm):
563
578
    """A TestCase is a member of a TestSuite.
568
583
    
569
584
    testid = Int()
570
585
    suiteid = Int()
571
 
    suite = Reference(suiteid, TestSuite.suiteid)
 
586
    suite = Reference(suiteid, "TestSuite.suiteid")
572
587
    passmsg = Unicode()
573
588
    failmsg = Unicode()
574
 
    init = Unicode()
575
 
    code_type = Unicode()
576
 
    code = Unicode()
577
 
    testtype = Unicode()
 
589
    test_default = Unicode()
578
590
    seq_no = Int()
579
591
    
 
592
    parts = ReferenceSet(testid, "TestCasePart.testid")
 
593
    
 
594
    __init__ = _kwarg_init
 
595
 
 
596
class TestSuiteVar(Storm):
 
597
    """A container for the arguments of a Test Suite"""
 
598
    __storm_table__ = "suite_variables"
 
599
    __storm_primary__ = "varid"
 
600
    
 
601
    varid = Int()
 
602
    suiteid = Int()
 
603
    var_name = Unicode()
 
604
    var_value = Unicode()
 
605
    var_type = Unicode()
 
606
    arg_no = Int()
 
607
    
 
608
    suite = Reference(suiteid, "TestSuite.suiteid")
 
609
    
 
610
    __init__ = _kwarg_init
 
611
    
 
612
class TestCasePart(Storm):
 
613
    """A container for the test elements of a Test Case"""
 
614
    __storm_table__ = "test_case_parts"
 
615
    __storm_primary__ = "partid"
 
616
    
 
617
    partid = Int()
 
618
    testid = Int()
 
619
    
 
620
    part_type = Unicode()
 
621
    test_type = Unicode()
 
622
    data = Unicode()
 
623
    filename = Unicode()
 
624
    
 
625
    test = Reference(testid, "TestCase.testid")
 
626
    
580
627
    __init__ = _kwarg_init