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

« back to all changes in this revision

Viewing changes to ivle/database.py

Modified worksheets edit view, so now there are links to edit, add,
and re-arranging worksheets.

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, 
 
279
        'Worksheet.offering_id', 
 
280
        order_by="Worksheet.seq_no"
 
281
    )
 
282
 
249
283
    __init__ = _kwarg_init
250
284
 
251
285
    def __repr__(self):
264
298
        e = Enrolment(user=user, offering=self, active=True)
265
299
        self.enrolments.add(e)
266
300
 
 
301
    def get_permissions(self, user):
 
302
        perms = set()
 
303
        if user is not None:
 
304
            perms.add('view')
 
305
            if user.rolenm == 'admin':
 
306
                perms.add('edit')
 
307
        return perms
 
308
 
267
309
class Enrolment(Storm):
268
310
    __storm_table__ = "enrolment"
269
311
    __storm_primary__ = "user_id", "offering_id"
368
410
# WORKSHEETS AND EXERCISES #
369
411
 
370
412
class Exercise(Storm):
371
 
    # Note: Table "problem" is called "Exercise" in the Object layer, since
372
 
    # it's called that everywhere else.
373
 
    __storm_table__ = "problem"
374
 
 
375
 
    id = Int(primary=True, name="problemid")
376
 
    name = Unicode(name="identifier")
377
 
    spec = Unicode()
 
413
    __storm_table__ = "exercise"
 
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"
407
438
 
408
439
    id = Int(primary=True, name="worksheetid")
409
 
    # XXX subject is not linked to a Subject object. This is a property of
410
 
    # the database, and will be refactored.
411
 
    subject = Unicode()
412
 
    name = Unicode(name="identifier")
 
440
    offering_id = Int(name="offeringid")
 
441
    identifier = Unicode()
 
442
    name = Unicode()
413
443
    assessable = Bool()
414
 
    mtime = DateTime()
415
 
 
416
 
    exercises = ReferenceSet(id,
417
 
        'WorksheetExercise.worksheet_id',
418
 
        'WorksheetExercise.exercise_id',
419
 
        Exercise.id)
 
444
    data = Unicode()
 
445
    seq_no = Int()
 
446
    format = Unicode()
 
447
 
 
448
    attempts = ReferenceSet(id, "ExerciseAttempt.worksheetid")
 
449
    offering = Reference(offering_id, 'Offering.id')
 
450
 
420
451
    # Use worksheet_exercises to get access to the WorksheetExercise objects
421
452
    # binding worksheets to exercises. This is required to access the
422
453
    # "optional" field.
423
454
    worksheet_exercises = ReferenceSet(id,
424
455
        'WorksheetExercise.worksheet_id')
 
456
        
425
457
 
426
458
    __init__ = _kwarg_init
427
459
 
448
480
        """
449
481
        store.find(WorksheetExercise,
450
482
            WorksheetExercise.worksheet == self).remove()
 
483
            
 
484
    def get_permissions(self, user):
 
485
        return self.offering.get_permissions(user)
451
486
 
452
487
class WorksheetExercise(Storm):
453
 
    __storm_table__ = "worksheet_problem"
454
 
    __storm_primary__ = "worksheet_id", "exercise_id"
 
488
    __storm_table__ = "worksheet_exercise"
 
489
    
 
490
    id = Int(primary=True, name="ws_ex_id")
455
491
 
456
492
    worksheet_id = Int(name="worksheetid")
457
493
    worksheet = Reference(worksheet_id, Worksheet.id)
458
 
    exercise_id = Int(name="problemid")
 
494
    exercise_id = Unicode(name="exerciseid")
459
495
    exercise = Reference(exercise_id, Exercise.id)
460
496
    optional = Bool()
 
497
    active = Bool()
 
498
    seq_no = Int()
 
499
    
 
500
    saves = ReferenceSet(id, "ExerciseSave.ws_ex_id")
 
501
    attempts = ReferenceSet(id, "ExerciseAttempt.ws_ex_id")
461
502
 
462
503
    __init__ = _kwarg_init
463
504
 
464
505
    def __repr__(self):
465
506
        return "<%s %s in %s>" % (type(self).__name__, self.exercise.name,
466
 
                                  self.worksheet.name)
 
507
                                  self.worksheet.identifier)
467
508
 
468
509
class ExerciseSave(Storm):
469
510
    """
474
515
    ExerciseSave may be extended with additional semantics (such as
475
516
    ExerciseAttempt).
476
517
    """
477
 
    __storm_table__ = "problem_save"
478
 
    __storm_primary__ = "exercise_id", "user_id", "date"
479
 
 
480
 
    exercise_id = Int(name="problemid")
481
 
    exercise = Reference(exercise_id, Exercise.id)
 
518
    __storm_table__ = "exercise_save"
 
519
    __storm_primary__ = "ws_ex_id", "user_id"
 
520
 
 
521
    ws_ex_id = Int(name="ws_ex_id")
 
522
    worksheet_exercise = Reference(ws_ex_id, "WorksheetExercise.id")
 
523
 
482
524
    user_id = Int(name="loginid")
483
525
    user = Reference(user_id, User.id)
484
526
    date = DateTime()
503
545
        they won't count (either as a penalty or success), but will still be
504
546
        stored.
505
547
    """
506
 
    __storm_table__ = "problem_attempt"
507
 
    __storm_primary__ = "exercise_id", "user_id", "date"
 
548
    __storm_table__ = "exercise_attempt"
 
549
    __storm_primary__ = "ws_ex_id", "user_id", "date"
508
550
 
509
551
    # The "text" field is the same but has a different name in the DB table
510
552
    # for some reason.
511
553
    text = Unicode(name="attempt")
512
554
    complete = Bool()
513
555
    active = Bool()
 
556
    
 
557
    def get_permissions(self, user):
 
558
        return set(['view']) if user is self.user else set()
 
559
  
 
560
class TestSuite(Storm):
 
561
    """A Testsuite acts as a container for the test cases of an exercise."""
 
562
    __storm_table__ = "test_suite"
 
563
    __storm_primary__ = "exercise_id", "suiteid"
 
564
    
 
565
    suiteid = Int()
 
566
    exercise_id = Unicode(name="exerciseid")
 
567
    description = Unicode()
 
568
    seq_no = Int()
 
569
    function = Unicode()
 
570
    stdin = Unicode()
 
571
    exercise = Reference(exercise_id, Exercise.id)
 
572
    test_cases = ReferenceSet(suiteid, 'TestCase.suiteid')
 
573
    variables = ReferenceSet(suiteid, 'TestSuiteVar.suiteid')
 
574
 
 
575
class TestCase(Storm):
 
576
    """A TestCase is a member of a TestSuite.
 
577
    
 
578
    It contains the data necessary to check if an exercise is correct"""
 
579
    __storm_table__ = "test_case"
 
580
    __storm_primary__ = "testid", "suiteid"
 
581
    
 
582
    testid = Int()
 
583
    suiteid = Int()
 
584
    suite = Reference(suiteid, "TestSuite.suiteid")
 
585
    passmsg = Unicode()
 
586
    failmsg = Unicode()
 
587
    test_default = Unicode()
 
588
    seq_no = Int()
 
589
    
 
590
    parts = ReferenceSet(testid, "TestCasePart.testid")
 
591
    
 
592
    __init__ = _kwarg_init
 
593
 
 
594
class TestSuiteVar(Storm):
 
595
    """A container for the arguments of a Test Suite"""
 
596
    __storm_table__ = "suite_variable"
 
597
    __storm_primary__ = "varid"
 
598
    
 
599
    varid = Int()
 
600
    suiteid = Int()
 
601
    var_name = Unicode()
 
602
    var_value = Unicode()
 
603
    var_type = Unicode()
 
604
    arg_no = Int()
 
605
    
 
606
    suite = Reference(suiteid, "TestSuite.suiteid")
 
607
    
 
608
    __init__ = _kwarg_init
 
609
    
 
610
class TestCasePart(Storm):
 
611
    """A container for the test elements of a Test Case"""
 
612
    __storm_table__ = "test_case_part"
 
613
    __storm_primary__ = "partid"
 
614
    
 
615
    partid = Int()
 
616
    testid = Int()
 
617
    
 
618
    part_type = Unicode()
 
619
    test_type = Unicode()
 
620
    data = Unicode()
 
621
    filename = Unicode()
 
622
    
 
623
    test = Reference(testid, "TestCase.testid")
 
624
    
 
625
    __init__ = _kwarg_init