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

« back to all changes in this revision

Viewing changes to ivle/database.py

Convince the main XHTML template to use versioned styles/scripts, and move the
core styles and scripts to the new framework (in ivle.webapp.core).

www/media is dead!

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', 'TestSuiteVar'
 
42
            'AlreadyEnrolledError'
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
 
 
137
133
    def _get_enrolments(self, justactive):
138
134
        return Store.of(self).find(Enrolment,
139
135
            Enrolment.user_id == self.id,
228
224
        perms = set()
229
225
        if user is not None:
230
226
            perms.add('view')
231
 
            if user.rolenm == 'admin':
232
 
                perms.add('edit')
 
227
        if user.rolenm == 'admin':
 
228
            perms.add('edit')
233
229
        return perms
234
230
 
235
231
class Semester(Storm):
264
260
                           'User.id')
265
261
    project_sets = ReferenceSet(id, 'ProjectSet.offering_id')
266
262
 
267
 
    worksheets = ReferenceSet(id, 'Worksheet.offering_id')
268
 
 
269
263
    __init__ = _kwarg_init
270
264
 
271
265
    def __repr__(self):
284
278
        e = Enrolment(user=user, offering=self, active=True)
285
279
        self.enrolments.add(e)
286
280
 
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
 
 
295
281
class Enrolment(Storm):
296
282
    __storm_table__ = "enrolment"
297
283
    __storm_primary__ = "user_id", "offering_id"
399
385
    # Note: Table "problem" is called "Exercise" in the Object layer, since
400
386
    # it's called that everywhere else.
401
387
    __storm_table__ = "problem"
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()
 
388
 
 
389
    id = Int(primary=True, name="problemid")
 
390
    name = Unicode(name="identifier")
 
391
    spec = Unicode()
410
392
 
411
393
    worksheets = ReferenceSet(id,
412
394
        'WorksheetExercise.exercise_id',
413
395
        'WorksheetExercise.worksheet_id',
414
396
        'Worksheet.id'
415
397
    )
416
 
    
417
 
    test_suites = ReferenceSet(id, 'TestSuite.exercise_id')
418
398
 
419
399
    __init__ = _kwarg_init
420
400
 
421
401
    def __repr__(self):
422
402
        return "<%s %s>" % (type(self).__name__, self.name)
423
403
 
 
404
    @classmethod
 
405
    def get_by_name(cls, store, name):
 
406
        """
 
407
        Get the Exercise from the db associated with a given store and name.
 
408
        If the exercise is not in the database, creates it and inserts it
 
409
        automatically.
 
410
        """
 
411
        ex = store.find(cls, cls.name == unicode(name)).one()
 
412
        if ex is not None:
 
413
            return ex
 
414
        ex = Exercise(name=unicode(name))
 
415
        store.add(ex)
 
416
        store.commit()
 
417
        return ex
424
418
 
425
419
class Worksheet(Storm):
426
420
    __storm_table__ = "worksheet"
428
422
    id = Int(primary=True, name="worksheetid")
429
423
    # XXX subject is not linked to a Subject object. This is a property of
430
424
    # the database, and will be refactored.
431
 
    offering_id = Int(name="offeringid")
 
425
    subject = Unicode()
432
426
    name = Unicode(name="identifier")
433
427
    assessable = Bool()
434
428
    mtime = DateTime()
435
429
 
436
 
    attempts = ReferenceSet(id, "ExerciseAttempt.worksheetid")
437
 
    offering = Reference(offering_id, 'Offering.id')
438
 
 
439
430
    exercises = ReferenceSet(id,
440
431
        'WorksheetExercise.worksheet_id',
441
432
        'WorksheetExercise.exercise_id',
445
436
    # "optional" field.
446
437
    worksheet_exercises = ReferenceSet(id,
447
438
        'WorksheetExercise.worksheet_id')
448
 
        
449
439
 
450
440
    __init__ = _kwarg_init
451
441
 
472
462
        """
473
463
        store.find(WorksheetExercise,
474
464
            WorksheetExercise.worksheet == self).remove()
475
 
            
476
 
    def get_permissions(self, user):
477
 
        return self.offering.get_permissions(user)
478
465
 
479
466
class WorksheetExercise(Storm):
480
467
    __storm_table__ = "worksheet_problem"
482
469
 
483
470
    worksheet_id = Int(name="worksheetid")
484
471
    worksheet = Reference(worksheet_id, Worksheet.id)
485
 
    exercise_id = Unicode(name="problemid")
 
472
    exercise_id = Int(name="problemid")
486
473
    exercise = Reference(exercise_id, Exercise.id)
487
474
    optional = Bool()
488
475
 
504
491
    __storm_table__ = "problem_save"
505
492
    __storm_primary__ = "exercise_id", "user_id", "date"
506
493
 
507
 
    exercise_id = Unicode(name="problemid")
 
494
    exercise_id = Int(name="problemid")
508
495
    exercise = Reference(exercise_id, Exercise.id)
509
496
    user_id = Int(name="loginid")
510
497
    user = Reference(user_id, User.id)
511
498
    date = DateTime()
512
499
    text = Unicode()
513
 
    worksheetid = Int()
514
 
    worksheet = Reference(worksheetid, Worksheet.id)
515
500
 
516
501
    __init__ = _kwarg_init
517
502
 
540
525
    text = Unicode(name="attempt")
541
526
    complete = Bool()
542
527
    active = Bool()
543
 
    
 
528
 
544
529
    def get_permissions(self, user):
545
530
        return set(['view']) if user is self.user else set()
546
 
  
547
 
class TestSuite(Storm):
548
 
    """A Testsuite acts as a container for the test cases of an exercise."""
549
 
    __storm_table__ = "test_suite"
550
 
    __storm_primary__ = "exercise_id", "suiteid"
551
 
    
552
 
    suiteid = Int()
553
 
    exercise_id = Unicode(name="problemid")
554
 
    description = Unicode()
555
 
    seq_no = Int()
556
 
    function = Unicode()
557
 
    stdin = Unicode()
558
 
    exercise = Reference(exercise_id, Exercise.id)
559
 
    test_cases = ReferenceSet(suiteid, 'TestCase.suiteid')
560
 
    variables = ReferenceSet(suiteid, 'TestSuiteVar.suiteid')
561
 
 
562
 
class TestCase(Storm):
563
 
    """A TestCase is a member of a TestSuite.
564
 
    
565
 
    It contains the data necessary to check if an exercise is correct"""
566
 
    __storm_table__ = "test_case"
567
 
    __storm_primary__ = "testid", "suiteid"
568
 
    
569
 
    testid = Int()
570
 
    suiteid = Int()
571
 
    suite = Reference(suiteid, "TestSuite.suiteid")
572
 
    passmsg = Unicode()
573
 
    failmsg = Unicode()
574
 
    test_default = Unicode()
575
 
    seq_no = Int()
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
 
    
612
 
    __init__ = _kwarg_init