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

« back to all changes in this revision

Viewing changes to ivle/database.py

ivle.chat now opens /dev/null as std{in,out,err} when daemonising.

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):
53
53
    """
54
54
    Returns the Storm connection string, generated from the conf file.
55
55
    """
56
 
    return "postgres://%s:%s@%s:%d/%s" % (ivle.conf.db_user,
57
 
        ivle.conf.db_password, ivle.conf.db_host, ivle.conf.db_port,
58
 
        ivle.conf.db_dbname)
 
56
 
 
57
    clusterstr = ''
 
58
    if ivle.conf.db_user:
 
59
        clusterstr += ivle.conf.db_user
 
60
        if ivle.conf.db_password:
 
61
            clusterstr += ':' + ivle.conf.db_password
 
62
        clusterstr += '@'
 
63
 
 
64
    host = ivle.conf.db_host or 'localhost'
 
65
    port = ivle.conf.db_port or 5432
 
66
 
 
67
    clusterstr += '%s:%d' % (host, port)
 
68
 
 
69
    return "postgres://%s/%s" % (clusterstr, ivle.conf.db_dbname)
59
70
 
60
71
def get_store():
61
72
    """
130
141
        fieldval = self.acct_exp
131
142
        return fieldval is not None and datetime.datetime.now() > fieldval
132
143
 
 
144
    @property
 
145
    def valid(self):
 
146
        return self.state == 'enabled' and not self.account_expired
 
147
 
133
148
    def _get_enrolments(self, justactive):
134
149
        return Store.of(self).find(Enrolment,
135
150
            Enrolment.user_id == self.id,
196
211
        """
197
212
        return store.find(cls, cls.login == unicode(login)).one()
198
213
 
 
214
    def get_permissions(self, user):
 
215
        if user and user.rolenm == 'admin' or user is self:
 
216
            return set(['view', 'edit'])
 
217
        else:
 
218
            return set()
 
219
 
199
220
# SUBJECTS AND ENROLMENTS #
200
221
 
201
222
class Subject(Storm):
214
235
    def __repr__(self):
215
236
        return "<%s '%s'>" % (type(self).__name__, self.short_name)
216
237
 
 
238
    def get_permissions(self, user):
 
239
        perms = set()
 
240
        if user is not None:
 
241
            perms.add('view')
 
242
            if user.rolenm == 'admin':
 
243
                perms.add('edit')
 
244
        return perms
 
245
 
217
246
class Semester(Storm):
218
247
    __storm_table__ = "semester"
219
248
 
246
275
                           'User.id')
247
276
    project_sets = ReferenceSet(id, 'ProjectSet.offering_id')
248
277
 
 
278
    worksheets = ReferenceSet(id, 'Worksheet.offering_id')
 
279
 
249
280
    __init__ = _kwarg_init
250
281
 
251
282
    def __repr__(self):
264
295
        e = Enrolment(user=user, offering=self, active=True)
265
296
        self.enrolments.add(e)
266
297
 
 
298
    def get_permissions(self, user):
 
299
        perms = set()
 
300
        if user is not None:
 
301
            perms.add('view')
 
302
            if user.rolenm == 'admin':
 
303
                perms.add('edit')
 
304
        return perms
 
305
 
267
306
class Enrolment(Storm):
268
307
    __storm_table__ = "enrolment"
269
308
    __storm_primary__ = "user_id", "offering_id"
371
410
    # Note: Table "problem" is called "Exercise" in the Object layer, since
372
411
    # it's called that everywhere else.
373
412
    __storm_table__ = "problem"
374
 
 
375
 
    id = Int(primary=True, name="problemid")
376
 
    name = Unicode(name="identifier")
377
 
    spec = Unicode()
 
413
#TODO: Add in a field for the user-friendly identifier
 
414
    id = Unicode(primary=True, name="identifier")
 
415
    name = Unicode()
 
416
    description = Unicode()
 
417
    partial = Unicode()
 
418
    solution = Unicode()
 
419
    include = Unicode()
 
420
    num_rows = Int()
378
421
 
379
422
    worksheets = ReferenceSet(id,
380
423
        'WorksheetExercise.exercise_id',
381
424
        'WorksheetExercise.worksheet_id',
382
425
        'Worksheet.id'
383
426
    )
 
427
    
 
428
    test_suites = ReferenceSet(id, 'TestSuite.exercise_id')
384
429
 
385
430
    __init__ = _kwarg_init
386
431
 
387
432
    def __repr__(self):
388
433
        return "<%s %s>" % (type(self).__name__, self.name)
389
434
 
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
404
435
 
405
436
class Worksheet(Storm):
406
437
    __storm_table__ = "worksheet"
408
439
    id = Int(primary=True, name="worksheetid")
409
440
    # XXX subject is not linked to a Subject object. This is a property of
410
441
    # the database, and will be refactored.
411
 
    subject = Unicode()
 
442
    offering_id = Int(name="offeringid")
412
443
    name = Unicode(name="identifier")
413
444
    assessable = Bool()
414
445
    mtime = DateTime()
415
446
 
 
447
    attempts = ReferenceSet(id, "ExerciseAttempt.worksheetid")
 
448
    offering = Reference(offering_id, 'Offering.id')
 
449
 
416
450
    exercises = ReferenceSet(id,
417
451
        'WorksheetExercise.worksheet_id',
418
452
        'WorksheetExercise.exercise_id',
422
456
    # "optional" field.
423
457
    worksheet_exercises = ReferenceSet(id,
424
458
        'WorksheetExercise.worksheet_id')
 
459
        
425
460
 
426
461
    __init__ = _kwarg_init
427
462
 
448
483
        """
449
484
        store.find(WorksheetExercise,
450
485
            WorksheetExercise.worksheet == self).remove()
 
486
            
 
487
    def get_permissions(self, user):
 
488
        return self.offering.get_permissions(user)
451
489
 
452
490
class WorksheetExercise(Storm):
453
491
    __storm_table__ = "worksheet_problem"
455
493
 
456
494
    worksheet_id = Int(name="worksheetid")
457
495
    worksheet = Reference(worksheet_id, Worksheet.id)
458
 
    exercise_id = Int(name="problemid")
 
496
    exercise_id = Unicode(name="problemid")
459
497
    exercise = Reference(exercise_id, Exercise.id)
460
498
    optional = Bool()
461
499
 
477
515
    __storm_table__ = "problem_save"
478
516
    __storm_primary__ = "exercise_id", "user_id", "date"
479
517
 
480
 
    exercise_id = Int(name="problemid")
 
518
    exercise_id = Unicode(name="problemid")
481
519
    exercise = Reference(exercise_id, Exercise.id)
482
520
    user_id = Int(name="loginid")
483
521
    user = Reference(user_id, User.id)
484
522
    date = DateTime()
485
523
    text = Unicode()
 
524
    worksheetid = Int()
 
525
    worksheet = Reference(worksheetid, Worksheet.id)
486
526
 
487
527
    __init__ = _kwarg_init
488
528
 
511
551
    text = Unicode(name="attempt")
512
552
    complete = Bool()
513
553
    active = Bool()
 
554
    
 
555
    def get_permissions(self, user):
 
556
        return set(['view']) if user is self.user else set()
 
557
  
 
558
class TestSuite(Storm):
 
559
    """A Testsuite acts as a container for the test cases of an exercise."""
 
560
    __storm_table__ = "test_suite"
 
561
    __storm_primary__ = "exercise_id", "suiteid"
 
562
    
 
563
    suiteid = Int()
 
564
    exercise_id = Unicode(name="problemid")
 
565
    description = Unicode()
 
566
    seq_no = Int()
 
567
    function = Unicode()
 
568
    stdin = Unicode()
 
569
    exercise = Reference(exercise_id, Exercise.id)
 
570
    test_cases = ReferenceSet(suiteid, 'TestCase.suiteid')
 
571
    variables = ReferenceSet(suiteid, 'TestSuiteVar.suiteid')
 
572
 
 
573
class TestCase(Storm):
 
574
    """A TestCase is a member of a TestSuite.
 
575
    
 
576
    It contains the data necessary to check if an exercise is correct"""
 
577
    __storm_table__ = "test_case"
 
578
    __storm_primary__ = "testid", "suiteid"
 
579
    
 
580
    testid = Int()
 
581
    suiteid = Int()
 
582
    suite = Reference(suiteid, "TestSuite.suiteid")
 
583
    passmsg = Unicode()
 
584
    failmsg = Unicode()
 
585
    test_default = Unicode()
 
586
    seq_no = Int()
 
587
    
 
588
    parts = ReferenceSet(testid, "TestCasePart.testid")
 
589
    
 
590
    __init__ = _kwarg_init
 
591
 
 
592
class TestSuiteVar(Storm):
 
593
    """A container for the arguments of a Test Suite"""
 
594
    __storm_table__ = "suite_variables"
 
595
    __storm_primary__ = "varid"
 
596
    
 
597
    varid = Int()
 
598
    suiteid = Int()
 
599
    var_name = Unicode()
 
600
    var_value = Unicode()
 
601
    var_type = Unicode()
 
602
    arg_no = Int()
 
603
    
 
604
    suite = Reference(suiteid, "TestSuite.suiteid")
 
605
    
 
606
    __init__ = _kwarg_init
 
607
    
 
608
class TestCasePart(Storm):
 
609
    """A container for the test elements of a Test Case"""
 
610
    __storm_table__ = "test_case_parts"
 
611
    __storm_primary__ = "partid"
 
612
    
 
613
    partid = Int()
 
614
    testid = Int()
 
615
    
 
616
    part_type = Unicode()
 
617
    test_type = Unicode()
 
618
    data = Unicode()
 
619
    filename = Unicode()
 
620
    
 
621
    test = Reference(testid, "TestCase.testid")
 
622
    
 
623
    __init__ = _kwarg_init