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

« back to all changes in this revision

Viewing changes to ivle/database.py

  • Committer: William Grant
  • Date: 2009-04-07 03:48:23 UTC
  • mfrom: (1165.1.46 submissions)
  • Revision ID: grantw@unimelb.edu.au-20090407034823-snd6wa5p6otzq073
Allow students to submit projects from personal or group repositories.

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
            'User',
39
39
            'Subject', 'Semester', 'Offering', 'Enrolment',
40
40
            'ProjectSet', 'Project', 'ProjectGroup', 'ProjectGroupMembership',
 
41
            'Assessed', 'ProjectSubmission', 'ProjectExtension',
41
42
            'Exercise', 'Worksheet', 'WorksheetExercise',
42
43
            'ExerciseSave', 'ExerciseAttempt',
43
44
            'TestCase', 'TestSuite', 'TestSuiteVar'
117
118
        return self.hash_password(password) == self.passhash
118
119
 
119
120
    @property
 
121
    def display_name(self):
 
122
        return self.fullname
 
123
 
 
124
    @property
120
125
    def password_expired(self):
121
126
        fieldval = self.pass_exp
122
127
        return fieldval is not None and datetime.datetime.now() > fieldval
184
189
        '''A sanely ordered list of all of the user's enrolments.'''
185
190
        return self._get_enrolments(False) 
186
191
 
 
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
 
187
215
    @staticmethod
188
216
    def hash_password(password):
189
217
        return md5.md5(password).hexdigest()
198
226
 
199
227
    def get_permissions(self, user):
200
228
        if user and user.admin or user is self:
201
 
            return set(['view', 'edit'])
 
229
            return set(['view', 'edit', 'submit_project'])
202
230
        else:
203
231
            return set()
204
232
 
363
391
    __storm_table__ = "project"
364
392
 
365
393
    id = Int(name="projectid", primary=True)
 
394
    name = Unicode()
 
395
    short_name = Unicode()
366
396
    synopsis = Unicode()
367
397
    url = Unicode()
368
398
    project_set_id = Int(name="projectsetid")
369
399
    project_set = Reference(project_set_id, ProjectSet.id)
370
400
    deadline = DateTime()
371
401
 
 
402
    assesseds = ReferenceSet(id, 'Assessed.project_id')
 
403
    submissions = ReferenceSet(id,
 
404
                               'Assessed.project_id',
 
405
                               'Assessed.id',
 
406
                               'ProjectSubmission.assessed_id')
 
407
 
372
408
    __init__ = _kwarg_init
373
409
 
374
410
    def __repr__(self):
375
 
        return "<%s '%s' in %r>" % (type(self).__name__, self.synopsis,
 
411
        return "<%s '%s' in %r>" % (type(self).__name__, self.short_name,
376
412
                                  self.project_set.offering)
377
413
 
 
414
    def can_submit(self, principal):
 
415
        return (self in principal.get_projects() and
 
416
                self.deadline > datetime.datetime.now())
 
417
 
 
418
    def submit(self, principal, path, revision, who):
 
419
        """Submit a Subversion path and revision to a project.
 
420
 
 
421
        'principal' is the owner of the Subversion repository, and the
 
422
        entity on behalf of whom the submission is being made. 'path' is
 
423
        a path within that repository, and 'revision' specifies which
 
424
        revision of that path. 'who' is the person making the submission.
 
425
        """
 
426
 
 
427
        if not self.can_submit(principal):
 
428
            raise Exception('cannot submit')
 
429
 
 
430
        a = Assessed.get(Store.of(self), principal, self)
 
431
        ps = ProjectSubmission()
 
432
        ps.path = path
 
433
        ps.revision = revision
 
434
        ps.date_submitted = datetime.datetime.now()
 
435
        ps.assessed = a
 
436
        ps.submitter = who
 
437
 
 
438
        return ps
 
439
 
 
440
 
378
441
class ProjectGroup(Storm):
379
442
    __storm_table__ = "project_group"
380
443
 
398
461
        return "<%s %s in %r>" % (type(self).__name__, self.name,
399
462
                                  self.project_set.offering)
400
463
 
 
464
    @property
 
465
    def display_name(self):
 
466
        return '%s (%s)' % (self.nick, self.name)
 
467
 
 
468
    def get_projects(self, offering=None, active_only=True):
 
469
        '''Return Projects that the group can submit.
 
470
 
 
471
        This will include projects in the project set which owns this group,
 
472
        unless the project set disallows groups (in which case none will be
 
473
        returned).
 
474
 
 
475
        Unless active_only is False, projects will only be returned if the
 
476
        group's offering is active.
 
477
 
 
478
        If an offering is specified, projects will only be returned if it
 
479
        matches the group's.
 
480
        '''
 
481
        return Store.of(self).find(Project,
 
482
            Project.project_set_id == ProjectSet.id,
 
483
            ProjectSet.id == self.project_set.id,
 
484
            ProjectSet.max_students_per_group != None,
 
485
            ProjectSet.offering_id == Offering.id,
 
486
            (offering is None) or (Offering.id == offering.id),
 
487
            Semester.id == Offering.semester_id,
 
488
            (not active_only) or (Semester.state == u'current'))
 
489
 
 
490
 
 
491
    def get_permissions(self, user):
 
492
        if user.admin or user in self.members:
 
493
            return set(['submit_project'])
 
494
        else:
 
495
            return set()
 
496
 
401
497
class ProjectGroupMembership(Storm):
402
498
    __storm_table__ = "group_member"
403
499
    __storm_primary__ = "user_id", "project_group_id"
413
509
        return "<%s %r in %r>" % (type(self).__name__, self.user,
414
510
                                  self.project_group)
415
511
 
 
512
class Assessed(Storm):
 
513
    __storm_table__ = "assessed"
 
514
 
 
515
    id = Int(name="assessedid", primary=True)
 
516
    user_id = Int(name="loginid")
 
517
    user = Reference(user_id, User.id)
 
518
    project_group_id = Int(name="groupid")
 
519
    project_group = Reference(project_group_id, ProjectGroup.id)
 
520
 
 
521
    project_id = Int(name="projectid")
 
522
    project = Reference(project_id, Project.id)
 
523
 
 
524
    extensions = ReferenceSet(id, 'ProjectExtension.assessed_id')
 
525
    submissions = ReferenceSet(id, 'ProjectSubmission.assessed_id')
 
526
 
 
527
    def __repr__(self):
 
528
        return "<%s %r in %r>" % (type(self).__name__,
 
529
            self.user or self.project_group, self.project)
 
530
 
 
531
    @classmethod
 
532
    def get(cls, store, principal, project):
 
533
        t = type(principal)
 
534
        if t not in (User, ProjectGroup):
 
535
            raise AssertionError('principal must be User or ProjectGroup')
 
536
 
 
537
        a = store.find(cls,
 
538
            (t is User) or (cls.project_group_id == principal.id),
 
539
            (t is ProjectGroup) or (cls.user_id == principal.id),
 
540
            Project.id == project.id).one()
 
541
 
 
542
        if a is None:
 
543
            a = cls()
 
544
            if t is User:
 
545
                a.user = principal
 
546
            else:
 
547
                a.project_group = principal
 
548
            a.project = project
 
549
            store.add(a)
 
550
 
 
551
        return a
 
552
 
 
553
 
 
554
class ProjectExtension(Storm):
 
555
    __storm_table__ = "project_extension"
 
556
 
 
557
    id = Int(name="extensionid", primary=True)
 
558
    assessed_id = Int(name="assessedid")
 
559
    assessed = Reference(assessed_id, Assessed.id)
 
560
    deadline = DateTime()
 
561
    approver_id = Int(name="approver")
 
562
    approver = Reference(approver_id, User.id)
 
563
    notes = Unicode()
 
564
 
 
565
class ProjectSubmission(Storm):
 
566
    __storm_table__ = "project_submission"
 
567
 
 
568
    id = Int(name="submissionid", primary=True)
 
569
    assessed_id = Int(name="assessedid")
 
570
    assessed = Reference(assessed_id, Assessed.id)
 
571
    path = Unicode()
 
572
    revision = Int()
 
573
    submitter_id = Int(name="submitter")
 
574
    submitter = Reference(submitter_id, User.id)
 
575
    date_submitted = DateTime()
 
576
 
 
577
 
416
578
# WORKSHEETS AND EXERCISES #
417
579
 
418
580
class Exercise(Storm):