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

« back to all changes in this revision

Viewing changes to ivle/database.py

  • Committer: Matt Giuca
  • Date: 2009-03-24 08:03:53 UTC
  • mto: This revision was merged to the branch mainline in revision 1322.
  • Revision ID: matt.giuca@gmail.com-20090324080353-1w4oduwp7elujgs8
Fleshed out the documentation structure a bit.
Added a high-level description of the system on the front page, and 'dev' and
'man' sections.

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
 
30
30
from storm.locals import create_database, Store, Int, Unicode, DateTime, \
31
31
                         Reference, ReferenceSet, Bool, Storm, Desc
 
32
from storm.exceptions import NotOneError, IntegrityError
32
33
 
33
34
import ivle.conf
 
35
from ivle.worksheet.rst import rst
34
36
 
35
37
__all__ = ['get_store',
36
38
            'User',
235
237
    state = Unicode()
236
238
 
237
239
    offerings = ReferenceSet(id, 'Offering.semester_id')
 
240
    enrolments = ReferenceSet(id,
 
241
                              'Offering.semester_id',
 
242
                              'Offering.id',
 
243
                              'Enrolment.offering_id')
238
244
 
239
245
    __init__ = _kwarg_init
240
246
 
260
266
 
261
267
    worksheets = ReferenceSet(id, 
262
268
        'Worksheet.offering_id', 
263
 
        order_by="Worksheet.seq_no"
 
269
        order_by="seq_no"
264
270
    )
265
271
 
266
272
    __init__ = _kwarg_init
282
288
        enrolment.active = True
283
289
        enrolment.role = role
284
290
 
 
291
    def unenrol(self, user):
 
292
        '''Unenrol a user from this offering.'''
 
293
        enrolment = Store.of(self).find(Enrolment,
 
294
                               Enrolment.user_id == user.id,
 
295
                               Enrolment.offering_id == self.id).one()
 
296
        Store.of(enrolment).remove(enrolment)
 
297
 
285
298
    def get_permissions(self, user):
286
299
        perms = set()
287
300
        if user is not None:
288
 
            perms.add('view')
289
 
            if user.admin:
 
301
            enrolment = self.get_enrolment(user)
 
302
            if enrolment or user.admin:
 
303
                perms.add('view')
 
304
            if (enrolment and enrolment.role in (u'tutor', u'lecturer')) \
 
305
               or user.admin:
290
306
                perms.add('edit')
291
307
        return perms
292
308
 
 
309
    def get_enrolment(self, user):
 
310
        try:
 
311
            enrolment = self.enrolments.find(user=user).one()
 
312
        except NotOneError:
 
313
            enrolment = None
 
314
 
 
315
        return enrolment
 
316
 
293
317
class Enrolment(Storm):
294
318
    __storm_table__ = "enrolment"
295
319
    __storm_primary__ = "user_id", "offering_id"
401
425
    include = Unicode()
402
426
    num_rows = Int()
403
427
 
 
428
    worksheet_exercises =  ReferenceSet(id,
 
429
        'WorksheetExercise.exercise_id')
 
430
 
404
431
    worksheets = ReferenceSet(id,
405
432
        'WorksheetExercise.exercise_id',
406
433
        'WorksheetExercise.worksheet_id',
407
434
        'Worksheet.id'
408
435
    )
409
436
    
410
 
    test_suites = ReferenceSet(id, 'TestSuite.exercise_id')
 
437
    test_suites = ReferenceSet(id, 
 
438
        'TestSuite.exercise_id',
 
439
        order_by='seq_no')
411
440
 
412
441
    __init__ = _kwarg_init
413
442
 
416
445
 
417
446
    def get_permissions(self, user):
418
447
        perms = set()
 
448
        roles = set()
419
449
        if user is not None:
420
450
            if user.admin:
421
451
                perms.add('edit')
422
452
                perms.add('view')
 
453
            elif 'lecturer' in set((e.role for e in user.active_enrolments)):
 
454
                perms.add('edit')
 
455
                perms.add('view')
 
456
            
423
457
        return perms
 
458
    
 
459
    def get_description(self):
 
460
        return rst(self.description)
 
461
 
 
462
    def delete(self):
 
463
        """Deletes the exercise, providing it has no associated worksheets."""
 
464
        if (self.worksheet_exercises.count() > 0):
 
465
            raise IntegrityError()
 
466
        for suite in self.test_suites:
 
467
            suite.delete()
 
468
        Store.of(self).remove(self)
424
469
 
425
470
class Worksheet(Storm):
426
471
    __storm_table__ = "worksheet"
443
488
    # Use worksheet_exercises to get access to the *active* WorksheetExercise
444
489
    # objects binding worksheets to exercises. This is required to access the
445
490
    # "optional" field.
 
491
 
446
492
    @property
447
493
    def worksheet_exercises(self):
448
494
        return self.all_worksheet_exercises.find(active=True)
464
510
        return store.find(cls, cls.subject == unicode(subjectname),
465
511
            cls.name == unicode(worksheetname)).one()
466
512
 
467
 
    def remove_all_exercises(self, store):
 
513
    def remove_all_exercises(self):
468
514
        """
469
515
        Remove all exercises from this worksheet.
470
516
        This does not delete the exercises themselves. It just removes them
471
517
        from the worksheet.
472
518
        """
 
519
        store = Store.of(self)
 
520
        for ws_ex in self.all_worksheet_exercises:
 
521
            if ws_ex.saves.count() > 0 or ws_ex.attempts.count() > 0:
 
522
                raise IntegrityError()
473
523
        store.find(WorksheetExercise,
474
524
            WorksheetExercise.worksheet == self).remove()
475
525
            
476
526
    def get_permissions(self, user):
477
527
        return self.offering.get_permissions(user)
478
 
 
 
528
    
 
529
    def get_xml(self):
 
530
        """Returns the xml of this worksheet, converts from rst if required."""
 
531
        if self.format == u'rst':
 
532
            ws_xml = rst(self.data)
 
533
            return ws_xml
 
534
        else:
 
535
            return self.data
 
536
    
 
537
    def delete(self):
 
538
        """Deletes the worksheet, provided it has no attempts on any exercises.
 
539
        
 
540
        Returns True if delete succeeded, or False if this worksheet has
 
541
        attempts attached."""
 
542
        for ws_ex in self.all_worksheet_exercises:
 
543
            if ws_ex.saves.count() > 0 or ws_ex.attempts.count() > 0:
 
544
                raise IntegrityError()
 
545
        
 
546
        self.remove_all_exercises()
 
547
        Store.of(self).remove(self)
 
548
        
479
549
class WorksheetExercise(Storm):
480
550
    __storm_table__ = "worksheet_exercise"
481
551
    
498
568
        return "<%s %s in %s>" % (type(self).__name__, self.exercise.name,
499
569
                                  self.worksheet.identifier)
500
570
 
 
571
    def get_permissions(self, user):
 
572
        return self.worksheet.get_permissions(user)
 
573
    
 
574
 
501
575
class ExerciseSave(Storm):
502
576
    """
503
577
    Represents a potential solution to an exercise that a user has submitted
561
635
    function = Unicode()
562
636
    stdin = Unicode()
563
637
    exercise = Reference(exercise_id, Exercise.id)
564
 
    test_cases = ReferenceSet(suiteid, 'TestCase.suiteid')
565
 
    variables = ReferenceSet(suiteid, 'TestSuiteVar.suiteid')
 
638
    test_cases = ReferenceSet(suiteid, 'TestCase.suiteid', order_by="seq_no")
 
639
    variables = ReferenceSet(suiteid, 'TestSuiteVar.suiteid', order_by='arg_no')
 
640
    
 
641
    def delete(self):
 
642
        """Delete this suite, without asking questions."""
 
643
        for vaariable in self.variables:
 
644
            variable.delete()
 
645
        for test_case in self.test_cases:
 
646
            test_case.delete()
 
647
        Store.of(self).remove(self)
566
648
 
567
649
class TestCase(Storm):
568
650
    """A TestCase is a member of a TestSuite.
582
664
    parts = ReferenceSet(testid, "TestCasePart.testid")
583
665
    
584
666
    __init__ = _kwarg_init
 
667
    
 
668
    def delete(self):
 
669
        for part in self.parts:
 
670
            part.delete()
 
671
        Store.of(self).remove(self)
585
672
 
586
673
class TestSuiteVar(Storm):
587
674
    """A container for the arguments of a Test Suite"""
599
686
    
600
687
    __init__ = _kwarg_init
601
688
    
 
689
    def delete(self):
 
690
        Store.of(self).remove(self)
 
691
    
602
692
class TestCasePart(Storm):
603
693
    """A container for the test elements of a Test Case"""
604
694
    __storm_table__ = "test_case_part"
615
705
    test = Reference(testid, "TestCase.testid")
616
706
    
617
707
    __init__ = _kwarg_init
 
708
    
 
709
    def delete(self):
 
710
        Store.of(self).remove(self)