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

« back to all changes in this revision

Viewing changes to ivle/database.py

Minor updates to the sql for the userdb

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'
 
42
            'AlreadyEnrolledError', 'TestCase', 'TestSuite', 'TestSuiteVar'
43
43
        ]
44
44
 
45
45
def _kwarg_init(self, **kwargs):
130
130
        fieldval = self.acct_exp
131
131
        return fieldval is not None and datetime.datetime.now() > fieldval
132
132
 
 
133
    @property
 
134
    def valid(self):
 
135
        return self.state == 'enabled' and not self.account_expired
 
136
 
133
137
    def _get_enrolments(self, justactive):
134
138
        return Store.of(self).find(Enrolment,
135
139
            Enrolment.user_id == self.id,
196
200
        """
197
201
        return store.find(cls, cls.login == unicode(login)).one()
198
202
 
 
203
    def get_permissions(self, user):
 
204
        if user and user.rolenm == 'admin' or user is self:
 
205
            return set(['view', 'edit'])
 
206
        else:
 
207
            return set()
 
208
 
199
209
# SUBJECTS AND ENROLMENTS #
200
210
 
201
211
class Subject(Storm):
214
224
    def __repr__(self):
215
225
        return "<%s '%s'>" % (type(self).__name__, self.short_name)
216
226
 
 
227
    def get_permissions(self, user):
 
228
        perms = set()
 
229
        if user is not None:
 
230
            perms.add('view')
 
231
            if user.rolenm == 'admin':
 
232
                perms.add('edit')
 
233
        return perms
 
234
 
217
235
class Semester(Storm):
218
236
    __storm_table__ = "semester"
219
237
 
246
264
                           'User.id')
247
265
    project_sets = ReferenceSet(id, 'ProjectSet.offering_id')
248
266
 
 
267
    worksheets = ReferenceSet(id, 'Worksheet.offering_id')
 
268
 
249
269
    __init__ = _kwarg_init
250
270
 
251
271
    def __repr__(self):
264
284
        e = Enrolment(user=user, offering=self, active=True)
265
285
        self.enrolments.add(e)
266
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
 
267
295
class Enrolment(Storm):
268
296
    __storm_table__ = "enrolment"
269
297
    __storm_primary__ = "user_id", "offering_id"
371
399
    # Note: Table "problem" is called "Exercise" in the Object layer, since
372
400
    # it's called that everywhere else.
373
401
    __storm_table__ = "problem"
374
 
 
375
 
    id = Int(primary=True, name="problemid")
376
 
    name = Unicode(name="identifier")
377
 
    spec = Unicode()
 
402
#TODO: Add in a field for the user-friendly identifier
 
403
    id = Unicode(primary=True, name="identifier")
 
404
    name = Unicode()
 
405
    description = Unicode()
 
406
    partial = Unicode()
 
407
    solution = Unicode()
 
408
    include = Unicode()
 
409
    num_rows = Int()
378
410
 
379
411
    worksheets = ReferenceSet(id,
380
412
        'WorksheetExercise.exercise_id',
381
413
        'WorksheetExercise.worksheet_id',
382
414
        'Worksheet.id'
383
415
    )
 
416
    
 
417
    test_suites = ReferenceSet(id, 'TestSuite.exercise_id')
384
418
 
385
419
    __init__ = _kwarg_init
386
420
 
387
421
    def __repr__(self):
388
422
        return "<%s %s>" % (type(self).__name__, self.name)
389
423
 
390
 
    @classmethod
391
 
    def get_by_name(cls, store, name):
392
 
        """
393
 
        Get the Exercise from the db associated with a given store and name.
394
 
        If the exercise is not in the database, creates it and inserts it
395
 
        automatically.
396
 
        """
397
 
        ex = store.find(cls, cls.name == unicode(name)).one()
398
 
        if ex is not None:
399
 
            return ex
400
 
        ex = Exercise(name=unicode(name))
401
 
        store.add(ex)
402
 
        store.commit()
403
 
        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
404
438
 
405
439
class Worksheet(Storm):
406
440
    __storm_table__ = "worksheet"
409
443
    # XXX subject is not linked to a Subject object. This is a property of
410
444
    # the database, and will be refactored.
411
445
    subject = Unicode()
 
446
    offering_id = Int(name="offeringid")
412
447
    name = Unicode(name="identifier")
413
448
    assessable = Bool()
414
449
    mtime = DateTime()
415
450
 
 
451
    attempts = ReferenceSet(id, "ExerciseAttempt.worksheetid")
 
452
    offering = Reference(offering_id, 'Offering.id')
 
453
 
416
454
    exercises = ReferenceSet(id,
417
455
        'WorksheetExercise.worksheet_id',
418
456
        'WorksheetExercise.exercise_id',
422
460
    # "optional" field.
423
461
    worksheet_exercises = ReferenceSet(id,
424
462
        'WorksheetExercise.worksheet_id')
 
463
        
425
464
 
426
465
    __init__ = _kwarg_init
427
466
 
448
487
        """
449
488
        store.find(WorksheetExercise,
450
489
            WorksheetExercise.worksheet == self).remove()
 
490
            
 
491
    def get_permissions(self, user):
 
492
        return self.offering.get_permissions(user)
451
493
 
452
494
class WorksheetExercise(Storm):
453
495
    __storm_table__ = "worksheet_problem"
455
497
 
456
498
    worksheet_id = Int(name="worksheetid")
457
499
    worksheet = Reference(worksheet_id, Worksheet.id)
458
 
    exercise_id = Int(name="problemid")
 
500
    exercise_id = Unicode(name="problemid")
459
501
    exercise = Reference(exercise_id, Exercise.id)
460
502
    optional = Bool()
461
503
 
477
519
    __storm_table__ = "problem_save"
478
520
    __storm_primary__ = "exercise_id", "user_id", "date"
479
521
 
480
 
    exercise_id = Int(name="problemid")
 
522
    exercise_id = Unicode(name="problemid")
481
523
    exercise = Reference(exercise_id, Exercise.id)
482
524
    user_id = Int(name="loginid")
483
525
    user = Reference(user_id, User.id)
484
526
    date = DateTime()
485
527
    text = Unicode()
 
528
    worksheetid = Int()
 
529
    worksheet = Reference(worksheetid, Worksheet.id)
486
530
 
487
531
    __init__ = _kwarg_init
488
532
 
511
555
    text = Unicode(name="attempt")
512
556
    complete = Bool()
513
557
    active = Bool()
 
558
    
 
559
    def get_permissions(self, user):
 
560
        return set(['view']) if user is self.user else set()
 
561
  
 
562
class TestSuite(Storm):
 
563
    """A Testsuite acts as a container for the test cases of an exercise."""
 
564
    __storm_table__ = "test_suite"
 
565
    __storm_primary__ = "exercise_id", "suiteid"
 
566
    
 
567
    suiteid = Int()
 
568
    exercise_id = Unicode(name="problemid")
 
569
    description = Unicode()
 
570
    seq_no = Int()
 
571
    function = Unicode()
 
572
    stdin = Unicode()
 
573
    exercise = Reference(exercise_id, Exercise.id)
 
574
    test_cases = ReferenceSet(suiteid, 'TestCase.suiteid')
 
575
    variables = ReferenceSet(suiteid, 'TestSuiteVar.suiteid')
 
576
 
 
577
class TestCase(Storm):
 
578
    """A TestCase is a member of a TestSuite.
 
579
    
 
580
    It contains the data necessary to check if an exercise is correct"""
 
581
    __storm_table__ = "test_case"
 
582
    __storm_primary__ = "testid", "suiteid"
 
583
    
 
584
    testid = Int()
 
585
    suiteid = Int()
 
586
    suite = Reference(suiteid, "TestSuite.suiteid")
 
587
    passmsg = Unicode()
 
588
    failmsg = Unicode()
 
589
    test_default = Unicode()
 
590
    seq_no = Int()
 
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
    
 
627
    __init__ = _kwarg_init