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

« back to all changes in this revision

Viewing changes to ivle/database.py

Began implementing new dispatch framework (with Will Grant and Nick Chadwick).
Added package: ivle.webapp. This contains 'base', with some base class
    implementations for the new Plugins and Views, and 'admin', with 'user'
    (part of 'userservice') re-implemented for the new framework in a RESTful
    way.
dispatch: Added code to partly handle the new plugin system, then fall back to
    the old one.

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,
200
196
        """
201
197
        return store.find(cls, cls.login == unicode(login)).one()
202
198
 
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
 
 
209
199
# SUBJECTS AND ENROLMENTS #
210
200
 
211
201
class Subject(Storm):
224
214
    def __repr__(self):
225
215
        return "<%s '%s'>" % (type(self).__name__, self.short_name)
226
216
 
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
 
 
235
217
class Semester(Storm):
236
218
    __storm_table__ = "semester"
237
219
 
264
246
                           'User.id')
265
247
    project_sets = ReferenceSet(id, 'ProjectSet.offering_id')
266
248
 
267
 
    worksheets = ReferenceSet(id, 'Worksheet.offering_id')
268
 
 
269
249
    __init__ = _kwarg_init
270
250
 
271
251
    def __repr__(self):
284
264
        e = Enrolment(user=user, offering=self, active=True)
285
265
        self.enrolments.add(e)
286
266
 
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
267
class Enrolment(Storm):
296
268
    __storm_table__ = "enrolment"
297
269
    __storm_primary__ = "user_id", "offering_id"
399
371
    # Note: Table "problem" is called "Exercise" in the Object layer, since
400
372
    # it's called that everywhere else.
401
373
    __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()
 
374
 
 
375
    id = Int(primary=True, name="problemid")
 
376
    name = Unicode(name="identifier")
 
377
    spec = Unicode()
410
378
 
411
379
    worksheets = ReferenceSet(id,
412
380
        'WorksheetExercise.exercise_id',
413
381
        'WorksheetExercise.worksheet_id',
414
382
        'Worksheet.id'
415
383
    )
416
 
    
417
 
    test_suites = ReferenceSet(id, 'TestSuite.exercise_id')
418
384
 
419
385
    __init__ = _kwarg_init
420
386
 
421
387
    def __repr__(self):
422
388
        return "<%s %s>" % (type(self).__name__, self.name)
423
389
 
 
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
404
 
425
405
class Worksheet(Storm):
426
406
    __storm_table__ = "worksheet"
428
408
    id = Int(primary=True, name="worksheetid")
429
409
    # XXX subject is not linked to a Subject object. This is a property of
430
410
    # the database, and will be refactored.
431
 
    offering_id = Int(name="offeringid")
 
411
    subject = Unicode()
432
412
    name = Unicode(name="identifier")
433
413
    assessable = Bool()
434
414
    mtime = DateTime()
435
415
 
436
 
    attempts = ReferenceSet(id, "ExerciseAttempt.worksheetid")
437
 
    offering = Reference(offering_id, 'Offering.id')
438
 
 
439
416
    exercises = ReferenceSet(id,
440
417
        'WorksheetExercise.worksheet_id',
441
418
        'WorksheetExercise.exercise_id',
445
422
    # "optional" field.
446
423
    worksheet_exercises = ReferenceSet(id,
447
424
        'WorksheetExercise.worksheet_id')
448
 
        
449
425
 
450
426
    __init__ = _kwarg_init
451
427
 
472
448
        """
473
449
        store.find(WorksheetExercise,
474
450
            WorksheetExercise.worksheet == self).remove()
475
 
            
476
 
    def get_permissions(self, user):
477
 
        return self.offering.get_permissions(user)
478
451
 
479
452
class WorksheetExercise(Storm):
480
453
    __storm_table__ = "worksheet_problem"
482
455
 
483
456
    worksheet_id = Int(name="worksheetid")
484
457
    worksheet = Reference(worksheet_id, Worksheet.id)
485
 
    exercise_id = Unicode(name="problemid")
 
458
    exercise_id = Int(name="problemid")
486
459
    exercise = Reference(exercise_id, Exercise.id)
487
460
    optional = Bool()
488
461
 
504
477
    __storm_table__ = "problem_save"
505
478
    __storm_primary__ = "exercise_id", "user_id", "date"
506
479
 
507
 
    exercise_id = Unicode(name="problemid")
 
480
    exercise_id = Int(name="problemid")
508
481
    exercise = Reference(exercise_id, Exercise.id)
509
482
    user_id = Int(name="loginid")
510
483
    user = Reference(user_id, User.id)
511
484
    date = DateTime()
512
485
    text = Unicode()
513
 
    worksheetid = Int()
514
 
    worksheet = Reference(worksheetid, Worksheet.id)
515
486
 
516
487
    __init__ = _kwarg_init
517
488
 
540
511
    text = Unicode(name="attempt")
541
512
    complete = Bool()
542
513
    active = Bool()
543
 
    
544
 
    def get_permissions(self, user):
545
 
        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