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

« back to all changes in this revision

Viewing changes to ivle/database.py

  • Committer: Matt Giuca
  • Date: 2009-01-19 16:03:36 UTC
  • mto: This revision was merged to the branch mainline in revision 1090.
  • Revision ID: matt.giuca@gmail.com-20090119160336-28ob0uev0hnikec3
Added new module: ivle.worksheet. This will contain general functions for
    worksheets (particularly for interacting with the DB), which don't belong
    in either tutorial or tutorialservice.
    worksheet now has get_exercise_status, which is a Storm re-implementation
    of ivle.db.get_problem_status. Much cleaner!
tutorial, tutorialservice: Now use ivle.worksheet.get_exercise_status rather
    than ivle.db.get_problem_status.
ivle.db: Renamed get_problem_status to _get_problem_status. Still required,
    but only used internally now.

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
            'User',
38
38
            'Subject', 'Semester', 'Offering', 'Enrolment',
39
39
            'ProjectSet', 'Project', 'ProjectGroup', 'ProjectGroupMembership',
40
 
            'Exercise', 'Worksheet', 'WorksheetExercise',
41
 
            'ExerciseSave', 'ExerciseAttempt',
42
 
            'AlreadyEnrolledError'
 
40
            'Exercise', 'Worksheet',
43
41
        ]
44
42
 
45
43
def _kwarg_init(self, **kwargs):
142
140
                Desc(Subject.code)
143
141
            )
144
142
 
145
 
    def _set_password(self, password):
146
 
        if password is None:
147
 
            self.passhash = None
148
 
        else:
149
 
            self.passhash = unicode(User.hash_password(password))
150
 
    password = property(fset=_set_password)
151
 
 
152
143
    @property
153
144
    def subjects(self):
154
145
        return Store.of(self).find(Subject,
240
231
    groups_student_permissions = Unicode()
241
232
 
242
233
    enrolments = ReferenceSet(id, 'Enrolment.offering_id')
243
 
    members = ReferenceSet(id,
244
 
                           'Enrolment.offering_id',
245
 
                           'Enrolment.user_id',
246
 
                           'User.id')
247
 
    project_sets = ReferenceSet(id, 'ProjectSet.offering_id')
248
234
 
249
235
    __init__ = _kwarg_init
250
236
 
252
238
        return "<%s %r in %r>" % (type(self).__name__, self.subject,
253
239
                                  self.semester)
254
240
 
255
 
    def enrol(self, user):
256
 
        '''Enrol a user in this offering.'''
257
 
        # We'll get a horrible database constraint violation error if we try
258
 
        # to add a second enrolment.
259
 
        if Store.of(self).find(Enrolment,
260
 
                               Enrolment.user_id == user.id,
261
 
                               Enrolment.offering_id == self.id).count() == 1:
262
 
            raise AlreadyEnrolledError()
263
 
 
264
 
        e = Enrolment(user=user, offering=self, active=True)
265
 
        self.enrolments.add(e)
266
 
 
267
241
class Enrolment(Storm):
268
242
    __storm_table__ = "enrolment"
269
243
    __storm_primary__ = "user_id", "offering_id"
275
249
    notes = Unicode()
276
250
    active = Bool()
277
251
 
278
 
    @property
279
 
    def groups(self):
280
 
        return Store.of(self).find(ProjectGroup,
281
 
                ProjectSet.offering_id == self.offering.id,
282
 
                ProjectGroup.project_set_id == ProjectSet.id,
283
 
                ProjectGroupMembership.project_group_id == ProjectGroup.id,
284
 
                ProjectGroupMembership.user_id == self.user.id)
285
 
 
286
252
    __init__ = _kwarg_init
287
253
 
288
254
    def __repr__(self):
289
255
        return "<%s %r in %r>" % (type(self).__name__, self.user,
290
256
                                  self.offering)
291
257
 
292
 
class AlreadyEnrolledError(Exception):
293
 
    pass
294
 
 
295
258
# PROJECTS #
296
259
 
297
260
class ProjectSet(Storm):
302
265
    offering = Reference(offering_id, Offering.id)
303
266
    max_students_per_group = Int()
304
267
 
305
 
    projects = ReferenceSet(id, 'Project.project_set_id')
306
 
    project_groups = ReferenceSet(id, 'ProjectGroup.project_set_id')
307
 
 
308
268
    __init__ = _kwarg_init
309
269
 
310
270
    def __repr__(self):
339
299
    created_by = Reference(created_by_id, User.id)
340
300
    epoch = DateTime()
341
301
 
342
 
    members = ReferenceSet(id,
343
 
                           "ProjectGroupMembership.project_group_id",
344
 
                           "ProjectGroupMembership.user_id",
345
 
                           "User.id")
346
 
 
347
302
    __init__ = _kwarg_init
348
303
 
349
304
    def __repr__(self):
350
305
        return "<%s %s in %r>" % (type(self).__name__, self.name,
351
306
                                  self.project_set.offering)
352
307
 
 
308
    @property
 
309
    def members(self):
 
310
        return Store.of(self).find(User,
 
311
            ProjectGroupMembership.project_group_id == self.id,
 
312
            User.id == ProjectGroupMembership.user_id)
 
313
 
353
314
class ProjectGroupMembership(Storm):
354
315
    __storm_table__ = "group_member"
355
316
    __storm_primary__ = "user_id", "project_group_id"