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

« back to all changes in this revision

Viewing changes to ivle/database.py

mip() the config dir when installing.

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
It also provides miscellaneous utility functions for database interaction.
25
25
"""
26
26
 
27
 
import hashlib
 
27
import md5
28
28
import datetime
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
33
32
 
34
 
from ivle.worksheet.rst import rst
 
33
import ivle.conf
 
34
import ivle.caps
35
35
 
36
36
__all__ = ['get_store',
37
37
            'User',
38
38
            'Subject', 'Semester', 'Offering', 'Enrolment',
39
39
            'ProjectSet', 'Project', 'ProjectGroup', 'ProjectGroupMembership',
40
 
            'Assessed', 'ProjectSubmission', 'ProjectExtension',
41
40
            'Exercise', 'Worksheet', 'WorksheetExercise',
42
41
            'ExerciseSave', 'ExerciseAttempt',
43
 
            'TestCase', 'TestSuite', 'TestSuiteVar'
 
42
            'AlreadyEnrolledError', 'TestCase', 'TestSuite', 'TestSuiteVar'
44
43
        ]
45
44
 
46
45
def _kwarg_init(self, **kwargs):
50
49
                % (self.__class__.__name__, k))
51
50
        setattr(self, k, v)
52
51
 
53
 
def get_conn_string(config):
54
 
    """Create a Storm connection string to the IVLE database
55
 
 
56
 
    @param config: The IVLE configuration.
57
 
    """
58
 
 
59
 
    clusterstr = ''
60
 
    if config['database']['username']:
61
 
        clusterstr += config['database']['username']
62
 
        if config['database']['password']:
63
 
            clusterstr += ':' + config['database']['password']
64
 
        clusterstr += '@'
65
 
 
66
 
    host = config['database']['host'] or 'localhost'
67
 
    port = config['database']['port'] or 5432
68
 
 
69
 
    clusterstr += '%s:%d' % (host, port)
70
 
 
71
 
    return "postgres://%s/%s" % (clusterstr, config['database']['name'])
72
 
 
73
 
def get_store(config):
74
 
    """Create a Storm store connected to the IVLE database.
75
 
 
76
 
    @param config: The IVLE configuration.
77
 
    """
78
 
    return Store(create_database(get_conn_string(config)))
 
52
def get_conn_string():
 
53
    """
 
54
    Returns the Storm connection string, generated from the conf file.
 
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)
 
59
 
 
60
def get_store():
 
61
    """
 
62
    Open a database connection and transaction. Return a storm.store.Store
 
63
    instance connected to the configured IVLE database.
 
64
    """
 
65
    return Store(create_database(get_conn_string()))
79
66
 
80
67
# USERS #
81
68
 
89
76
    login = Unicode()
90
77
    passhash = Unicode()
91
78
    state = Unicode()
92
 
    admin = Bool()
 
79
    rolenm = Unicode()
93
80
    unixid = Int()
94
81
    nick = Unicode()
95
82
    pass_exp = DateTime()
101
88
    studentid = Unicode()
102
89
    settings = Unicode()
103
90
 
 
91
    def _get_role(self):
 
92
        if self.rolenm is None:
 
93
            return None
 
94
        return ivle.caps.Role(self.rolenm)
 
95
    def _set_role(self, value):
 
96
        if not isinstance(value, ivle.caps.Role):
 
97
            raise TypeError("role must be an ivle.caps.Role")
 
98
        self.rolenm = unicode(value)
 
99
    role = property(_get_role, _set_role)
 
100
 
104
101
    __init__ = _kwarg_init
105
102
 
106
103
    def __repr__(self):
117
114
            return None
118
115
        return self.hash_password(password) == self.passhash
119
116
 
120
 
    @property
121
 
    def display_name(self):
122
 
        return self.fullname
 
117
    def hasCap(self, capability):
 
118
        """Given a capability (which is a Role object), returns True if this
 
119
        User has that capability, False otherwise.
 
120
        """
 
121
        return self.role.hasCap(capability)
123
122
 
124
123
    @property
125
124
    def password_expired(self):
189
188
        '''A sanely ordered list of all of the user's enrolments.'''
190
189
        return self._get_enrolments(False) 
191
190
 
192
 
    def get_projects(self, offering=None, active_only=True):
193
 
        '''Return Projects that the user can submit.
194
 
 
195
 
        This will include projects for offerings in which the user is
196
 
        enrolled, as long as the project is not in a project set which has
197
 
        groups (ie. if maximum number of group members is 0).
198
 
 
199
 
        Unless active_only is False, only projects for active offerings will
200
 
        be returned.
201
 
 
202
 
        If an offering is specified, returned projects will be limited to
203
 
        those for that offering.
204
 
        '''
205
 
        return Store.of(self).find(Project,
206
 
            Project.project_set_id == ProjectSet.id,
207
 
            ProjectSet.max_students_per_group == None,
208
 
            ProjectSet.offering_id == Offering.id,
209
 
            (offering is None) or (Offering.id == offering.id),
210
 
            Semester.id == Offering.semester_id,
211
 
            (not active_only) or (Semester.state == u'current'),
212
 
            Enrolment.offering_id == Offering.id,
213
 
            Enrolment.user_id == self.id)
214
 
 
215
191
    @staticmethod
216
192
    def hash_password(password):
217
 
        return hashlib.md5(password).hexdigest()
 
193
        return md5.md5(password).hexdigest()
218
194
 
219
195
    @classmethod
220
196
    def get_by_login(cls, store, login):
225
201
        return store.find(cls, cls.login == unicode(login)).one()
226
202
 
227
203
    def get_permissions(self, user):
228
 
        if user and user.admin or user is self:
229
 
            return set(['view', 'edit', 'submit_project'])
 
204
        if user and user.rolenm == 'admin' or user is self:
 
205
            return set(['view', 'edit'])
230
206
        else:
231
207
            return set()
232
208
 
252
228
        perms = set()
253
229
        if user is not None:
254
230
            perms.add('view')
255
 
            if user.admin:
 
231
            if user.rolenm == 'admin':
256
232
                perms.add('edit')
257
233
        return perms
258
234
 
259
 
    def active_offerings(self):
260
 
        """Return a sequence of currently active offerings for this subject
261
 
        (offerings whose semester.state is "current"). There should be 0 or 1
262
 
        elements in this sequence, but it's possible there are more.
263
 
        """
264
 
        return self.offerings.find(Offering.semester_id == Semester.id,
265
 
                                   Semester.state == u'current')
266
 
 
267
 
    def offering_for_semester(self, year, semester):
268
 
        """Get the offering for the given year/semester, or None."""
269
 
        return self.offerings.find(Offering.semester_id == Semester.id,
270
 
                               Semester.year == unicode(year),
271
 
                               Semester.semester == unicode(semester)).one()
272
 
 
273
235
class Semester(Storm):
274
236
    __storm_table__ = "semester"
275
237
 
276
238
    id = Int(primary=True, name="semesterid")
277
239
    year = Unicode()
278
240
    semester = Unicode()
279
 
    state = Unicode()
 
241
    active = Bool()
280
242
 
281
243
    offerings = ReferenceSet(id, 'Offering.semester_id')
282
 
    enrolments = ReferenceSet(id,
283
 
                              'Offering.semester_id',
284
 
                              'Offering.id',
285
 
                              'Enrolment.offering_id')
286
244
 
287
245
    __init__ = _kwarg_init
288
246
 
306
264
                           'User.id')
307
265
    project_sets = ReferenceSet(id, 'ProjectSet.offering_id')
308
266
 
309
 
    worksheets = ReferenceSet(id, 
310
 
        'Worksheet.offering_id', 
311
 
        order_by="seq_no"
312
 
    )
 
267
    worksheets = ReferenceSet(id, 'Worksheet.offering_id')
313
268
 
314
269
    __init__ = _kwarg_init
315
270
 
317
272
        return "<%s %r in %r>" % (type(self).__name__, self.subject,
318
273
                                  self.semester)
319
274
 
320
 
    def enrol(self, user, role=u'student'):
 
275
    def enrol(self, user):
321
276
        '''Enrol a user in this offering.'''
322
 
        enrolment = Store.of(self).find(Enrolment,
323
 
                               Enrolment.user_id == user.id,
324
 
                               Enrolment.offering_id == self.id).one()
325
 
 
326
 
        if enrolment is None:
327
 
            enrolment = Enrolment(user=user, offering=self)
328
 
            self.enrolments.add(enrolment)
329
 
 
330
 
        enrolment.active = True
331
 
        enrolment.role = role
332
 
 
333
 
    def unenrol(self, user):
334
 
        '''Unenrol a user from this offering.'''
335
 
        enrolment = Store.of(self).find(Enrolment,
336
 
                               Enrolment.user_id == user.id,
337
 
                               Enrolment.offering_id == self.id).one()
338
 
        Store.of(enrolment).remove(enrolment)
 
277
        # We'll get a horrible database constraint violation error if we try
 
278
        # to add a second enrolment.
 
279
        if Store.of(self).find(Enrolment,
 
280
                               Enrolment.user_id == user.id,
 
281
                               Enrolment.offering_id == self.id).count() == 1:
 
282
            raise AlreadyEnrolledError()
 
283
 
 
284
        e = Enrolment(user=user, offering=self, active=True)
 
285
        self.enrolments.add(e)
339
286
 
340
287
    def get_permissions(self, user):
341
288
        perms = set()
342
289
        if user is not None:
343
 
            enrolment = self.get_enrolment(user)
344
 
            if enrolment or user.admin:
345
 
                perms.add('view')
346
 
            if (enrolment and enrolment.role in (u'tutor', u'lecturer')) \
347
 
               or user.admin:
 
290
            perms.add('view')
 
291
            if user.rolenm == 'admin':
348
292
                perms.add('edit')
349
293
        return perms
350
294
 
351
 
    def get_enrolment(self, user):
352
 
        try:
353
 
            enrolment = self.enrolments.find(user=user).one()
354
 
        except NotOneError:
355
 
            enrolment = None
356
 
 
357
 
        return enrolment
358
 
 
359
295
class Enrolment(Storm):
360
296
    __storm_table__ = "enrolment"
361
297
    __storm_primary__ = "user_id", "offering_id"
364
300
    user = Reference(user_id, User.id)
365
301
    offering_id = Int(name="offeringid")
366
302
    offering = Reference(offering_id, Offering.id)
367
 
    role = Unicode()
368
303
    notes = Unicode()
369
304
    active = Bool()
370
305
 
382
317
        return "<%s %r in %r>" % (type(self).__name__, self.user,
383
318
                                  self.offering)
384
319
 
 
320
class AlreadyEnrolledError(Exception):
 
321
    pass
 
322
 
385
323
# PROJECTS #
386
324
 
387
325
class ProjectSet(Storm):
405
343
    __storm_table__ = "project"
406
344
 
407
345
    id = Int(name="projectid", primary=True)
408
 
    name = Unicode()
409
 
    short_name = Unicode()
410
346
    synopsis = Unicode()
411
347
    url = Unicode()
412
348
    project_set_id = Int(name="projectsetid")
413
349
    project_set = Reference(project_set_id, ProjectSet.id)
414
350
    deadline = DateTime()
415
351
 
416
 
    assesseds = ReferenceSet(id, 'Assessed.project_id')
417
 
    submissions = ReferenceSet(id,
418
 
                               'Assessed.project_id',
419
 
                               'Assessed.id',
420
 
                               'ProjectSubmission.assessed_id')
421
 
 
422
352
    __init__ = _kwarg_init
423
353
 
424
354
    def __repr__(self):
425
 
        return "<%s '%s' in %r>" % (type(self).__name__, self.short_name,
 
355
        return "<%s '%s' in %r>" % (type(self).__name__, self.synopsis,
426
356
                                  self.project_set.offering)
427
357
 
428
 
    def can_submit(self, principal):
429
 
        return (self in principal.get_projects() and
430
 
                self.deadline > datetime.datetime.now())
431
 
 
432
 
    def submit(self, principal, path, revision, who):
433
 
        """Submit a Subversion path and revision to a project.
434
 
 
435
 
        'principal' is the owner of the Subversion repository, and the
436
 
        entity on behalf of whom the submission is being made. 'path' is
437
 
        a path within that repository, and 'revision' specifies which
438
 
        revision of that path. 'who' is the person making the submission.
439
 
        """
440
 
 
441
 
        if not self.can_submit(principal):
442
 
            raise Exception('cannot submit')
443
 
 
444
 
        a = Assessed.get(Store.of(self), principal, self)
445
 
        ps = ProjectSubmission()
446
 
        ps.path = path
447
 
        ps.revision = revision
448
 
        ps.date_submitted = datetime.datetime.now()
449
 
        ps.assessed = a
450
 
        ps.submitter = who
451
 
 
452
 
        return ps
453
 
 
454
 
 
455
358
class ProjectGroup(Storm):
456
359
    __storm_table__ = "project_group"
457
360
 
475
378
        return "<%s %s in %r>" % (type(self).__name__, self.name,
476
379
                                  self.project_set.offering)
477
380
 
478
 
    @property
479
 
    def display_name(self):
480
 
        return '%s (%s)' % (self.nick, self.name)
481
 
 
482
 
    def get_projects(self, offering=None, active_only=True):
483
 
        '''Return Projects that the group can submit.
484
 
 
485
 
        This will include projects in the project set which owns this group,
486
 
        unless the project set disallows groups (in which case none will be
487
 
        returned).
488
 
 
489
 
        Unless active_only is False, projects will only be returned if the
490
 
        group's offering is active.
491
 
 
492
 
        If an offering is specified, projects will only be returned if it
493
 
        matches the group's.
494
 
        '''
495
 
        return Store.of(self).find(Project,
496
 
            Project.project_set_id == ProjectSet.id,
497
 
            ProjectSet.id == self.project_set.id,
498
 
            ProjectSet.max_students_per_group != None,
499
 
            ProjectSet.offering_id == Offering.id,
500
 
            (offering is None) or (Offering.id == offering.id),
501
 
            Semester.id == Offering.semester_id,
502
 
            (not active_only) or (Semester.state == u'current'))
503
 
 
504
 
 
505
 
    def get_permissions(self, user):
506
 
        if user.admin or user in self.members:
507
 
            return set(['submit_project'])
508
 
        else:
509
 
            return set()
510
 
 
511
381
class ProjectGroupMembership(Storm):
512
382
    __storm_table__ = "group_member"
513
383
    __storm_primary__ = "user_id", "project_group_id"
523
393
        return "<%s %r in %r>" % (type(self).__name__, self.user,
524
394
                                  self.project_group)
525
395
 
526
 
class Assessed(Storm):
527
 
    __storm_table__ = "assessed"
528
 
 
529
 
    id = Int(name="assessedid", primary=True)
530
 
    user_id = Int(name="loginid")
531
 
    user = Reference(user_id, User.id)
532
 
    project_group_id = Int(name="groupid")
533
 
    project_group = Reference(project_group_id, ProjectGroup.id)
534
 
 
535
 
    project_id = Int(name="projectid")
536
 
    project = Reference(project_id, Project.id)
537
 
 
538
 
    extensions = ReferenceSet(id, 'ProjectExtension.assessed_id')
539
 
    submissions = ReferenceSet(id, 'ProjectSubmission.assessed_id')
540
 
 
541
 
    def __repr__(self):
542
 
        return "<%s %r in %r>" % (type(self).__name__,
543
 
            self.user or self.project_group, self.project)
544
 
 
545
 
    @classmethod
546
 
    def get(cls, store, principal, project):
547
 
        t = type(principal)
548
 
        if t not in (User, ProjectGroup):
549
 
            raise AssertionError('principal must be User or ProjectGroup')
550
 
 
551
 
        a = store.find(cls,
552
 
            (t is User) or (cls.project_group_id == principal.id),
553
 
            (t is ProjectGroup) or (cls.user_id == principal.id),
554
 
            Project.id == project.id).one()
555
 
 
556
 
        if a is None:
557
 
            a = cls()
558
 
            if t is User:
559
 
                a.user = principal
560
 
            else:
561
 
                a.project_group = principal
562
 
            a.project = project
563
 
            store.add(a)
564
 
 
565
 
        return a
566
 
 
567
 
 
568
 
class ProjectExtension(Storm):
569
 
    __storm_table__ = "project_extension"
570
 
 
571
 
    id = Int(name="extensionid", primary=True)
572
 
    assessed_id = Int(name="assessedid")
573
 
    assessed = Reference(assessed_id, Assessed.id)
574
 
    deadline = DateTime()
575
 
    approver_id = Int(name="approver")
576
 
    approver = Reference(approver_id, User.id)
577
 
    notes = Unicode()
578
 
 
579
 
class ProjectSubmission(Storm):
580
 
    __storm_table__ = "project_submission"
581
 
 
582
 
    id = Int(name="submissionid", primary=True)
583
 
    assessed_id = Int(name="assessedid")
584
 
    assessed = Reference(assessed_id, Assessed.id)
585
 
    path = Unicode()
586
 
    revision = Int()
587
 
    submitter_id = Int(name="submitter")
588
 
    submitter = Reference(submitter_id, User.id)
589
 
    date_submitted = DateTime()
590
 
 
591
 
 
592
396
# WORKSHEETS AND EXERCISES #
593
397
 
594
398
class Exercise(Storm):
595
 
    __storm_table__ = "exercise"
 
399
    # Note: Table "problem" is called "Exercise" in the Object layer, since
 
400
    # it's called that everywhere else.
 
401
    __storm_table__ = "problem"
 
402
#TODO: Add in a field for the user-friendly identifier
596
403
    id = Unicode(primary=True, name="identifier")
597
404
    name = Unicode()
598
405
    description = Unicode()
601
408
    include = Unicode()
602
409
    num_rows = Int()
603
410
 
604
 
    worksheet_exercises =  ReferenceSet(id,
605
 
        'WorksheetExercise.exercise_id')
606
 
 
607
411
    worksheets = ReferenceSet(id,
608
412
        'WorksheetExercise.exercise_id',
609
413
        'WorksheetExercise.worksheet_id',
610
414
        'Worksheet.id'
611
415
    )
612
416
    
613
 
    test_suites = ReferenceSet(id, 
614
 
        'TestSuite.exercise_id',
615
 
        order_by='seq_no')
 
417
    test_suites = ReferenceSet(id, 'TestSuite.exercise_id')
616
418
 
617
419
    __init__ = _kwarg_init
618
420
 
619
421
    def __repr__(self):
620
422
        return "<%s %s>" % (type(self).__name__, self.name)
621
423
 
622
 
    def get_permissions(self, user):
623
 
        perms = set()
624
 
        roles = set()
625
 
        if user is not None:
626
 
            if user.admin:
627
 
                perms.add('edit')
628
 
                perms.add('view')
629
 
            elif 'lecturer' in set((e.role for e in user.active_enrolments)):
630
 
                perms.add('edit')
631
 
                perms.add('view')
632
 
            
633
 
        return perms
634
 
    
635
 
    def get_description(self):
636
 
        return rst(self.description)
637
 
 
638
 
    def delete(self):
639
 
        """Deletes the exercise, providing it has no associated worksheets."""
640
 
        if (self.worksheet_exercises.count() > 0):
641
 
            raise IntegrityError()
642
 
        for suite in self.test_suites:
643
 
            suite.delete()
644
 
        Store.of(self).remove(self)
645
424
 
646
425
class Worksheet(Storm):
647
426
    __storm_table__ = "worksheet"
648
427
 
649
428
    id = Int(primary=True, name="worksheetid")
 
429
    # XXX subject is not linked to a Subject object. This is a property of
 
430
    # the database, and will be refactored.
650
431
    offering_id = Int(name="offeringid")
651
 
    identifier = Unicode()
652
 
    name = Unicode()
 
432
    name = Unicode(name="identifier")
653
433
    assessable = Bool()
654
 
    data = Unicode()
655
 
    seq_no = Int()
656
 
    format = Unicode()
 
434
    mtime = DateTime()
657
435
 
658
436
    attempts = ReferenceSet(id, "ExerciseAttempt.worksheetid")
659
437
    offering = Reference(offering_id, 'Offering.id')
660
438
 
661
 
    all_worksheet_exercises = ReferenceSet(id,
 
439
    exercises = ReferenceSet(id,
 
440
        'WorksheetExercise.worksheet_id',
 
441
        'WorksheetExercise.exercise_id',
 
442
        Exercise.id)
 
443
    # Use worksheet_exercises to get access to the WorksheetExercise objects
 
444
    # binding worksheets to exercises. This is required to access the
 
445
    # "optional" field.
 
446
    worksheet_exercises = ReferenceSet(id,
662
447
        'WorksheetExercise.worksheet_id')
663
 
 
664
 
    # Use worksheet_exercises to get access to the *active* WorksheetExercise
665
 
    # objects binding worksheets to exercises. This is required to access the
666
 
    # "optional" field.
667
 
 
668
 
    @property
669
 
    def worksheet_exercises(self):
670
 
        return self.all_worksheet_exercises.find(active=True)
 
448
        
671
449
 
672
450
    __init__ = _kwarg_init
673
451
 
674
452
    def __repr__(self):
675
453
        return "<%s %s>" % (type(self).__name__, self.name)
676
454
 
677
 
    def remove_all_exercises(self):
 
455
    # XXX Refactor this - make it an instance method of Subject rather than a
 
456
    # class method of Worksheet. Can't do that now because Subject isn't
 
457
    # linked referentially to the Worksheet.
 
458
    @classmethod
 
459
    def get_by_name(cls, store, subjectname, worksheetname):
 
460
        """
 
461
        Get the Worksheet from the db associated with a given store, subject
 
462
        name and worksheet name.
 
463
        """
 
464
        return store.find(cls, cls.subject == unicode(subjectname),
 
465
            cls.name == unicode(worksheetname)).one()
 
466
 
 
467
    def remove_all_exercises(self, store):
678
468
        """
679
469
        Remove all exercises from this worksheet.
680
470
        This does not delete the exercises themselves. It just removes them
681
471
        from the worksheet.
682
472
        """
683
 
        store = Store.of(self)
684
 
        for ws_ex in self.all_worksheet_exercises:
685
 
            if ws_ex.saves.count() > 0 or ws_ex.attempts.count() > 0:
686
 
                raise IntegrityError()
687
473
        store.find(WorksheetExercise,
688
474
            WorksheetExercise.worksheet == self).remove()
689
475
            
690
476
    def get_permissions(self, user):
691
477
        return self.offering.get_permissions(user)
692
 
    
693
 
    def get_xml(self):
694
 
        """Returns the xml of this worksheet, converts from rst if required."""
695
 
        if self.format == u'rst':
696
 
            ws_xml = rst(self.data)
697
 
            return ws_xml
698
 
        else:
699
 
            return self.data
700
 
    
701
 
    def delete(self):
702
 
        """Deletes the worksheet, provided it has no attempts on any exercises.
703
 
        
704
 
        Returns True if delete succeeded, or False if this worksheet has
705
 
        attempts attached."""
706
 
        for ws_ex in self.all_worksheet_exercises:
707
 
            if ws_ex.saves.count() > 0 or ws_ex.attempts.count() > 0:
708
 
                raise IntegrityError()
709
 
        
710
 
        self.remove_all_exercises()
711
 
        Store.of(self).remove(self)
712
 
        
 
478
 
713
479
class WorksheetExercise(Storm):
714
 
    __storm_table__ = "worksheet_exercise"
715
 
    
716
 
    id = Int(primary=True, name="ws_ex_id")
 
480
    __storm_table__ = "worksheet_problem"
 
481
    __storm_primary__ = "worksheet_id", "exercise_id"
717
482
 
718
483
    worksheet_id = Int(name="worksheetid")
719
484
    worksheet = Reference(worksheet_id, Worksheet.id)
720
 
    exercise_id = Unicode(name="exerciseid")
 
485
    exercise_id = Unicode(name="problemid")
721
486
    exercise = Reference(exercise_id, Exercise.id)
722
487
    optional = Bool()
723
 
    active = Bool()
724
 
    seq_no = Int()
725
 
    
726
 
    saves = ReferenceSet(id, "ExerciseSave.ws_ex_id")
727
 
    attempts = ReferenceSet(id, "ExerciseAttempt.ws_ex_id")
728
488
 
729
489
    __init__ = _kwarg_init
730
490
 
731
491
    def __repr__(self):
732
492
        return "<%s %s in %s>" % (type(self).__name__, self.exercise.name,
733
 
                                  self.worksheet.identifier)
734
 
 
735
 
    def get_permissions(self, user):
736
 
        return self.worksheet.get_permissions(user)
737
 
    
 
493
                                  self.worksheet.name)
738
494
 
739
495
class ExerciseSave(Storm):
740
496
    """
745
501
    ExerciseSave may be extended with additional semantics (such as
746
502
    ExerciseAttempt).
747
503
    """
748
 
    __storm_table__ = "exercise_save"
749
 
    __storm_primary__ = "ws_ex_id", "user_id"
750
 
 
751
 
    ws_ex_id = Int(name="ws_ex_id")
752
 
    worksheet_exercise = Reference(ws_ex_id, "WorksheetExercise.id")
753
 
 
 
504
    __storm_table__ = "problem_save"
 
505
    __storm_primary__ = "exercise_id", "user_id", "date"
 
506
 
 
507
    exercise_id = Unicode(name="problemid")
 
508
    exercise = Reference(exercise_id, Exercise.id)
754
509
    user_id = Int(name="loginid")
755
510
    user = Reference(user_id, User.id)
756
511
    date = DateTime()
757
512
    text = Unicode()
 
513
    worksheetid = Int()
 
514
    worksheet = Reference(worksheetid, Worksheet.id)
758
515
 
759
516
    __init__ = _kwarg_init
760
517
 
775
532
        they won't count (either as a penalty or success), but will still be
776
533
        stored.
777
534
    """
778
 
    __storm_table__ = "exercise_attempt"
779
 
    __storm_primary__ = "ws_ex_id", "user_id", "date"
 
535
    __storm_table__ = "problem_attempt"
 
536
    __storm_primary__ = "exercise_id", "user_id", "date"
780
537
 
781
538
    # The "text" field is the same but has a different name in the DB table
782
539
    # for some reason.
793
550
    __storm_primary__ = "exercise_id", "suiteid"
794
551
    
795
552
    suiteid = Int()
796
 
    exercise_id = Unicode(name="exerciseid")
 
553
    exercise_id = Unicode(name="problemid")
797
554
    description = Unicode()
798
555
    seq_no = Int()
799
556
    function = Unicode()
800
557
    stdin = Unicode()
801
558
    exercise = Reference(exercise_id, Exercise.id)
802
 
    test_cases = ReferenceSet(suiteid, 'TestCase.suiteid', order_by="seq_no")
803
 
    variables = ReferenceSet(suiteid, 'TestSuiteVar.suiteid', order_by='arg_no')
804
 
    
805
 
    def delete(self):
806
 
        """Delete this suite, without asking questions."""
807
 
        for vaariable in self.variables:
808
 
            variable.delete()
809
 
        for test_case in self.test_cases:
810
 
            test_case.delete()
811
 
        Store.of(self).remove(self)
 
559
    test_cases = ReferenceSet(suiteid, 'TestCase.suiteid')
 
560
    variables = ReferenceSet(suiteid, 'TestSuiteVar.suiteid')
812
561
 
813
562
class TestCase(Storm):
814
563
    """A TestCase is a member of a TestSuite.
828
577
    parts = ReferenceSet(testid, "TestCasePart.testid")
829
578
    
830
579
    __init__ = _kwarg_init
831
 
    
832
 
    def delete(self):
833
 
        for part in self.parts:
834
 
            part.delete()
835
 
        Store.of(self).remove(self)
836
580
 
837
581
class TestSuiteVar(Storm):
838
582
    """A container for the arguments of a Test Suite"""
839
 
    __storm_table__ = "suite_variable"
 
583
    __storm_table__ = "suite_variables"
840
584
    __storm_primary__ = "varid"
841
585
    
842
586
    varid = Int()
850
594
    
851
595
    __init__ = _kwarg_init
852
596
    
853
 
    def delete(self):
854
 
        Store.of(self).remove(self)
855
 
    
856
597
class TestCasePart(Storm):
857
598
    """A container for the test elements of a Test Case"""
858
 
    __storm_table__ = "test_case_part"
 
599
    __storm_table__ = "test_case_parts"
859
600
    __storm_primary__ = "partid"
860
601
    
861
602
    partid = Int()
869
610
    test = Reference(testid, "TestCase.testid")
870
611
    
871
612
    __init__ = _kwarg_init
872
 
    
873
 
    def delete(self):
874
 
        Store.of(self).remove(self)