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

« back to all changes in this revision

Viewing changes to ivle/database.py

  • Committer: me at id
  • Date: 2009-01-15 06:11:32 UTC
  • mto: This revision was merged to the branch mainline in revision 1090.
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:branches%2Fstorm:1164
ivle.db: Remove get_enrolment and get_subjects_status. They're unused.

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
import ivle.conf
34
34
import ivle.caps
35
35
 
36
 
__all__ = ['get_store',
37
 
            'User',
38
 
            'Subject', 'Semester', 'Offering', 'Enrolment',
39
 
            'ProjectSet', 'Project', 'ProjectGroup', 'ProjectGroupMembership',
40
 
            'Exercise', 'Worksheet', 'WorksheetExercise',
41
 
            'ExerciseSave', 'ExerciseAttempt',
42
 
            'AlreadyEnrolledError'
43
 
        ]
44
 
 
45
36
def _kwarg_init(self, **kwargs):
46
37
    for k,v in kwargs.items():
47
 
        if k.startswith('_') or not hasattr(self.__class__, k):
 
38
        if k.startswith('_') or not hasattr(self, k):
48
39
            raise TypeError("%s got an unexpected keyword argument '%s'"
49
 
                % (self.__class__.__name__, k))
 
40
                % self.__class__.__name__, k)
50
41
        setattr(self, k, v)
51
42
 
52
43
def get_conn_string():
64
55
    """
65
56
    return Store(create_database(get_conn_string()))
66
57
 
67
 
# USERS #
68
 
 
69
58
class User(Storm):
70
59
    """
71
60
    Represents an IVLE user.
142
131
                Desc(Subject.code)
143
132
            )
144
133
 
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
134
    @property
153
135
    def subjects(self):
154
136
        return Store.of(self).find(Subject,
157
139
            Offering.id == Enrolment.offering_id,
158
140
            Subject.id == Offering.subject_id).config(distinct=True)
159
141
 
160
 
    # TODO: Invitations should be listed too?
161
 
    def get_groups(self, offering=None):
162
 
        preds = [
163
 
            ProjectGroupMembership.user_id == self.id,
164
 
            ProjectGroup.id == ProjectGroupMembership.project_group_id,
165
 
        ]
166
 
        if offering:
167
 
            preds.extend([
168
 
                ProjectSet.offering_id == offering.id,
169
 
                ProjectGroup.project_set_id == ProjectSet.id,
170
 
            ])
171
 
        return Store.of(self).find(ProjectGroup, *preds)
172
 
 
173
 
    @property
174
 
    def groups(self):
175
 
        return self.get_groups()
176
 
 
177
142
    @property
178
143
    def active_enrolments(self):
179
144
        '''A sanely ordered list of the user's active enrolments.'''
196
161
        """
197
162
        return store.find(cls, cls.login == unicode(login)).one()
198
163
 
199
 
# SUBJECTS AND ENROLMENTS #
200
 
 
201
164
class Subject(Storm):
202
165
    __storm_table__ = "subject"
203
166
 
247
210
        return "<%s %r in %r>" % (type(self).__name__, self.subject,
248
211
                                  self.semester)
249
212
 
250
 
    def enrol(self, user):
251
 
        '''Enrol a user in this offering.'''
252
 
        # We'll get a horrible database constraint violation error if we try
253
 
        # to add a second enrolment.
254
 
        if Store.of(self).find(Enrolment,
255
 
                               Enrolment.user_id == user.id,
256
 
                               Enrolment.offering_id == self.id).count() == 1:
257
 
            raise AlreadyEnrolledError()
258
 
 
259
 
        e = Enrolment(user=user, offering=self, active=True)
260
 
        self.enrolments.add(e)
261
 
 
262
213
class Enrolment(Storm):
263
214
    __storm_table__ = "enrolment"
264
215
    __storm_primary__ = "user_id", "offering_id"
275
226
    def __repr__(self):
276
227
        return "<%s %r in %r>" % (type(self).__name__, self.user,
277
228
                                  self.offering)
278
 
 
279
 
class AlreadyEnrolledError(Exception):
280
 
    pass
281
 
 
282
 
# PROJECTS #
283
 
 
284
 
class ProjectSet(Storm):
285
 
    __storm_table__ = "project_set"
286
 
 
287
 
    id = Int(name="projectsetid", primary=True)
288
 
    offering_id = Int(name="offeringid")
289
 
    offering = Reference(offering_id, Offering.id)
290
 
    max_students_per_group = Int()
291
 
 
292
 
    __init__ = _kwarg_init
293
 
 
294
 
    def __repr__(self):
295
 
        return "<%s %d in %r>" % (type(self).__name__, self.id,
296
 
                                  self.offering)
297
 
 
298
 
class Project(Storm):
299
 
    __storm_table__ = "project"
300
 
 
301
 
    id = Int(name="projectid", primary=True)
302
 
    synopsis = Unicode()
303
 
    url = Unicode()
304
 
    project_set_id = Int(name="projectsetid")
305
 
    project_set = Reference(project_set_id, ProjectSet.id)
306
 
    deadline = DateTime()
307
 
 
308
 
    __init__ = _kwarg_init
309
 
 
310
 
    def __repr__(self):
311
 
        return "<%s '%s' in %r>" % (type(self).__name__, self.synopsis,
312
 
                                  self.project_set.offering)
313
 
 
314
 
class ProjectGroup(Storm):
315
 
    __storm_table__ = "project_group"
316
 
 
317
 
    id = Int(name="groupid", primary=True)
318
 
    name = Unicode(name="groupnm")
319
 
    project_set_id = Int(name="projectsetid")
320
 
    project_set = Reference(project_set_id, ProjectSet.id)
321
 
    nick = Unicode()
322
 
    created_by_id = Int(name="createdby")
323
 
    created_by = Reference(created_by_id, User.id)
324
 
    epoch = DateTime()
325
 
 
326
 
    __init__ = _kwarg_init
327
 
 
328
 
    def __repr__(self):
329
 
        return "<%s %s in %r>" % (type(self).__name__, self.name,
330
 
                                  self.project_set.offering)
331
 
 
332
 
    @property
333
 
    def members(self):
334
 
        return Store.of(self).find(User,
335
 
            ProjectGroupMembership.project_group_id == self.id,
336
 
            User.id == ProjectGroupMembership.user_id)
337
 
 
338
 
class ProjectGroupMembership(Storm):
339
 
    __storm_table__ = "group_member"
340
 
    __storm_primary__ = "user_id", "project_group_id"
341
 
 
342
 
    user_id = Int(name="loginid")
343
 
    user = Reference(user_id, User.id)
344
 
    project_group_id = Int(name="groupid")
345
 
    project_group = Reference(project_group_id, ProjectGroup.id)
346
 
 
347
 
    __init__ = _kwarg_init
348
 
 
349
 
    def __repr__(self):
350
 
        return "<%s %r in %r>" % (type(self).__name__, self.user,
351
 
                                  self.project_group)
352
 
 
353
 
# WORKSHEETS AND EXERCISES #
354
 
 
355
 
class Exercise(Storm):
356
 
    # Note: Table "problem" is called "Exercise" in the Object layer, since
357
 
    # it's called that everywhere else.
358
 
    __storm_table__ = "problem"
359
 
 
360
 
    id = Int(primary=True, name="problemid")
361
 
    name = Unicode(name="identifier")
362
 
    spec = Unicode()
363
 
 
364
 
    worksheets = ReferenceSet(id,
365
 
        'WorksheetExercise.exercise_id',
366
 
        'WorksheetExercise.worksheet_id',
367
 
        'Worksheet.id'
368
 
    )
369
 
 
370
 
    __init__ = _kwarg_init
371
 
 
372
 
    def __repr__(self):
373
 
        return "<%s %s>" % (type(self).__name__, self.name)
374
 
 
375
 
    @classmethod
376
 
    def get_by_name(cls, store, name):
377
 
        """
378
 
        Get the Exercise from the db associated with a given store and name.
379
 
        If the exercise is not in the database, creates it and inserts it
380
 
        automatically.
381
 
        """
382
 
        ex = store.find(cls, cls.name == unicode(name)).one()
383
 
        if ex is not None:
384
 
            return ex
385
 
        ex = Exercise(name=unicode(name))
386
 
        store.add(ex)
387
 
        store.commit()
388
 
        return ex
389
 
 
390
 
class Worksheet(Storm):
391
 
    __storm_table__ = "worksheet"
392
 
 
393
 
    id = Int(primary=True, name="worksheetid")
394
 
    # XXX subject is not linked to a Subject object. This is a property of
395
 
    # the database, and will be refactored.
396
 
    subject = Unicode()
397
 
    name = Unicode(name="identifier")
398
 
    assessable = Bool()
399
 
    mtime = DateTime()
400
 
 
401
 
    exercises = ReferenceSet(id,
402
 
        'WorksheetExercise.worksheet_id',
403
 
        'WorksheetExercise.exercise_id',
404
 
        Exercise.id)
405
 
    # Use worksheet_exercises to get access to the WorksheetExercise objects
406
 
    # binding worksheets to exercises. This is required to access the
407
 
    # "optional" field.
408
 
    worksheet_exercises = ReferenceSet(id,
409
 
        'WorksheetExercise.worksheet_id')
410
 
 
411
 
    __init__ = _kwarg_init
412
 
 
413
 
    def __repr__(self):
414
 
        return "<%s %s>" % (type(self).__name__, self.name)
415
 
 
416
 
    # XXX Refactor this - make it an instance method of Subject rather than a
417
 
    # class method of Worksheet. Can't do that now because Subject isn't
418
 
    # linked referentially to the Worksheet.
419
 
    @classmethod
420
 
    def get_by_name(cls, store, subjectname, worksheetname):
421
 
        """
422
 
        Get the Worksheet from the db associated with a given store, subject
423
 
        name and worksheet name.
424
 
        """
425
 
        return store.find(cls, cls.subject == unicode(subjectname),
426
 
            cls.name == unicode(worksheetname)).one()
427
 
 
428
 
    def remove_all_exercises(self, store):
429
 
        """
430
 
        Remove all exercises from this worksheet.
431
 
        This does not delete the exercises themselves. It just removes them
432
 
        from the worksheet.
433
 
        """
434
 
        store.find(WorksheetExercise,
435
 
            WorksheetExercise.worksheet == self).remove()
436
 
 
437
 
class WorksheetExercise(Storm):
438
 
    __storm_table__ = "worksheet_problem"
439
 
    __storm_primary__ = "worksheet_id", "exercise_id"
440
 
 
441
 
    worksheet_id = Int(name="worksheetid")
442
 
    worksheet = Reference(worksheet_id, Worksheet.id)
443
 
    exercise_id = Int(name="problemid")
444
 
    exercise = Reference(exercise_id, Exercise.id)
445
 
    optional = Bool()
446
 
 
447
 
    __init__ = _kwarg_init
448
 
 
449
 
    def __repr__(self):
450
 
        return "<%s %s in %s>" % (type(self).__name__, self.exercise.name,
451
 
                                  self.worksheet.name)
452
 
 
453
 
class ExerciseSave(Storm):
454
 
    """
455
 
    Represents a potential solution to an exercise that a user has submitted
456
 
    to the server for storage.
457
 
    A basic ExerciseSave is just the current saved text for this exercise for
458
 
    this user (doesn't count towards their attempts).
459
 
    ExerciseSave may be extended with additional semantics (such as
460
 
    ExerciseAttempt).
461
 
    """
462
 
    __storm_table__ = "problem_save"
463
 
    __storm_primary__ = "exercise_id", "user_id", "date"
464
 
 
465
 
    exercise_id = Int(name="problemid")
466
 
    exercise = Reference(exercise_id, Exercise.id)
467
 
    user_id = Int(name="loginid")
468
 
    user = Reference(user_id, User.id)
469
 
    date = DateTime()
470
 
    text = Unicode()
471
 
 
472
 
    __init__ = _kwarg_init
473
 
 
474
 
    def __repr__(self):
475
 
        return "<%s %s by %s at %s>" % (type(self).__name__,
476
 
            self.exercise.name, self.user.login, self.date.strftime("%c"))
477
 
 
478
 
class ExerciseAttempt(ExerciseSave):
479
 
    """
480
 
    An ExerciseAttempt is a special case of an ExerciseSave. Like an
481
 
    ExerciseSave, it constitutes exercise solution data that the user has
482
 
    submitted to the server for storage.
483
 
    In addition, it contains additional information about the submission.
484
 
    complete - True if this submission was successful, rendering this exercise
485
 
        complete for this user.
486
 
    active - True if this submission is "active" (usually true). Submissions
487
 
        may be de-activated by privileged users for special reasons, and then
488
 
        they won't count (either as a penalty or success), but will still be
489
 
        stored.
490
 
    """
491
 
    __storm_table__ = "problem_attempt"
492
 
    __storm_primary__ = "exercise_id", "user_id", "date"
493
 
 
494
 
    # The "text" field is the same but has a different name in the DB table
495
 
    # for some reason.
496
 
    text = Unicode(name="attempt")
497
 
    complete = Bool()
498
 
    active = Bool()