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

« back to all changes in this revision

Viewing changes to ivle/database.py

  • Committer: William Grant
  • Date: 2009-02-25 02:59:48 UTC
  • Revision ID: grantw@unimelb.edu.au-20090225025948-7vrb3ehin4lxi2tj
ivle-enrol now allows updating of existing enrolments. It also sets the role.

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
            'ProjectSet', 'Project', 'ProjectGroup', 'ProjectGroupMembership',
39
39
            'Exercise', 'Worksheet', 'WorksheetExercise',
40
40
            'ExerciseSave', 'ExerciseAttempt',
41
 
            'AlreadyEnrolledError', 'TestCase', 'TestSuite', 'TestSuiteVar'
 
41
            'TestCase', 'TestSuite', 'TestSuiteVar'
42
42
        ]
43
43
 
44
44
def _kwarg_init(self, **kwargs):
269
269
        return "<%s %r in %r>" % (type(self).__name__, self.subject,
270
270
                                  self.semester)
271
271
 
272
 
    def enrol(self, user):
 
272
    def enrol(self, user, role=u'student'):
273
273
        '''Enrol a user in this offering.'''
274
 
        # We'll get a horrible database constraint violation error if we try
275
 
        # to add a second enrolment.
276
 
        if Store.of(self).find(Enrolment,
 
274
        enrolment = Store.of(self).find(Enrolment,
277
275
                               Enrolment.user_id == user.id,
278
 
                               Enrolment.offering_id == self.id).count() == 1:
279
 
            raise AlreadyEnrolledError()
280
 
 
281
 
        e = Enrolment(user=user, offering=self, active=True)
282
 
        self.enrolments.add(e)
 
276
                               Enrolment.offering_id == self.id).one()
 
277
 
 
278
        if enrolment is None:
 
279
            enrolment = Enrolment(user=user, offering=self)
 
280
            self.enrolments.add(enrolment)
 
281
 
 
282
        enrolment.active = True
 
283
        enrolment.role = role
283
284
 
284
285
    def get_permissions(self, user):
285
286
        perms = set()
315
316
        return "<%s %r in %r>" % (type(self).__name__, self.user,
316
317
                                  self.offering)
317
318
 
318
 
class AlreadyEnrolledError(Exception):
319
 
    pass
320
 
 
321
319
# PROJECTS #
322
320
 
323
321
class ProjectSet(Storm):