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

« back to all changes in this revision

Viewing changes to ivle/database.py

Quick port of fileservice to the new framework. It's still very much old-style,
though.

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
430
424
 
431
425
class Worksheet(Storm):
432
426
    __storm_table__ = "worksheet"
434
428
    id = Int(primary=True, name="worksheetid")
435
429
    # XXX subject is not linked to a Subject object. This is a property of
436
430
    # the database, and will be refactored.
437
 
    subject = Unicode()
438
431
    offering_id = Int(name="offeringid")
439
432
    name = Unicode(name="identifier")
440
433
    assessable = Bool()
441
434
    mtime = DateTime()
442
435
 
 
436
    attempts = ReferenceSet(id, "ExerciseAttempt.worksheetid")
443
437
    offering = Reference(offering_id, 'Offering.id')
444
438
 
445
439
    exercises = ReferenceSet(id,
478
472
        """
479
473
        store.find(WorksheetExercise,
480
474
            WorksheetExercise.worksheet == self).remove()
 
475
            
 
476
    def get_permissions(self, user):
 
477
        return self.offering.get_permissions(user)
481
478
 
482
479
class WorksheetExercise(Storm):
483
480
    __storm_table__ = "worksheet_problem"
554
551
    
555
552
    suiteid = Int()
556
553
    exercise_id = Unicode(name="problemid")
 
554
    description = Unicode()
 
555
    seq_no = Int()
 
556
    function = Unicode()
 
557
    stdin = Unicode()
557
558
    exercise = Reference(exercise_id, Exercise.id)
558
559
    test_cases = ReferenceSet(suiteid, 'TestCase.suiteid')
559
 
    description = Unicode()
560
 
    seq_no = Int()
 
560
    variables = ReferenceSet(suiteid, 'TestSuiteVar.suiteid')
561
561
 
562
562
class TestCase(Storm):
563
563
    """A TestCase is a member of a TestSuite.
568
568
    
569
569
    testid = Int()
570
570
    suiteid = Int()
571
 
    suite = Reference(suiteid, TestSuite.suiteid)
 
571
    suite = Reference(suiteid, "TestSuite.suiteid")
572
572
    passmsg = Unicode()
573
573
    failmsg = Unicode()
574
 
    init = Unicode()
575
 
    code_type = Unicode()
576
 
    code = Unicode()
577
 
    testtype = Unicode()
 
574
    test_default = Unicode()
578
575
    seq_no = Int()
579
576
    
 
577
    parts = ReferenceSet(testid, "TestCasePart.testid")
 
578
    
 
579
    __init__ = _kwarg_init
 
580
 
 
581
class TestSuiteVar(Storm):
 
582
    """A container for the arguments of a Test Suite"""
 
583
    __storm_table__ = "suite_variables"
 
584
    __storm_primary__ = "varid"
 
585
    
 
586
    varid = Int()
 
587
    suiteid = Int()
 
588
    var_name = Unicode()
 
589
    var_value = Unicode()
 
590
    var_type = Unicode()
 
591
    arg_no = Int()
 
592
    
 
593
    suite = Reference(suiteid, "TestSuite.suiteid")
 
594
    
 
595
    __init__ = _kwarg_init
 
596
    
 
597
class TestCasePart(Storm):
 
598
    """A container for the test elements of a Test Case"""
 
599
    __storm_table__ = "test_case_parts"
 
600
    __storm_primary__ = "partid"
 
601
    
 
602
    partid = Int()
 
603
    testid = Int()
 
604
    
 
605
    part_type = Unicode()
 
606
    test_type = Unicode()
 
607
    data = Unicode()
 
608
    filename = Unicode()
 
609
    
 
610
    test = Reference(testid, "TestCase.testid")
 
611
    
580
612
    __init__ = _kwarg_init