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

« back to all changes in this revision

Viewing changes to ivle/database.py

  • Committer: William Grant
  • Date: 2009-03-31 00:54:25 UTC
  • mto: (1165.3.1 submissions)
  • mto: This revision was merged to the branch mainline in revision 1174.
  • Revision ID: grantw@unimelb.edu.au-20090331005425-z875m95vkov53e83
Detect the permitted offering for the given submission URL and restrict to that.

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'
184
185
        '''A sanely ordered list of all of the user's enrolments.'''
185
186
        return self._get_enrolments(False) 
186
187
 
 
188
    def get_projects(self, offering=None, active_only=True):
 
189
        '''Return Projects that the user can submit.
 
190
 
 
191
        This will include projects for offerings in which the user is
 
192
        enrolled, as long as the project is not in a project set which has
 
193
        groups (ie. if maximum number of group members is 0).
 
194
 
 
195
        Unless active_only is False, only projects for active offerings will
 
196
        be returned.
 
197
 
 
198
        If an offering is specified, returned projects will be limited to
 
199
        those for that offering.
 
200
        '''
 
201
        return Store.of(self).find(Project,
 
202
            Project.project_set_id == ProjectSet.id,
 
203
            ProjectSet.max_students_per_group == 0,
 
204
            ProjectSet.offering_id == Offering.id,
 
205
            (offering is None) or (Offering.id == offering.id),
 
206
            Semester.id == Offering.semester_id,
 
207
            (not active_only) or (Semester.state == u'current'),
 
208
            Enrolment.offering_id == Offering.id,
 
209
            Enrolment.user_id == self.id)
 
210
 
187
211
    @staticmethod
188
212
    def hash_password(password):
189
213
        return md5.md5(password).hexdigest()
198
222
 
199
223
    def get_permissions(self, user):
200
224
        if user and user.admin or user is self:
201
 
            return set(['view', 'edit'])
 
225
            return set(['view', 'edit', 'submit_project'])
202
226
        else:
203
227
            return set()
204
228
 
363
387
    __storm_table__ = "project"
364
388
 
365
389
    id = Int(name="projectid", primary=True)
 
390
    name = Unicode()
 
391
    short_name = Unicode()
366
392
    synopsis = Unicode()
367
393
    url = Unicode()
368
394
    project_set_id = Int(name="projectsetid")
369
395
    project_set = Reference(project_set_id, ProjectSet.id)
370
396
    deadline = DateTime()
371
397
 
 
398
    assesseds = ReferenceSet(id, 'Assessed.project_id')
 
399
    submissions = ReferenceSet(id,
 
400
                               'Assessed.project_id',
 
401
                               'Assessed.id',
 
402
                               'ProjectSubmission.assessed_id')
 
403
 
372
404
    __init__ = _kwarg_init
373
405
 
374
406
    def __repr__(self):
375
 
        return "<%s '%s' in %r>" % (type(self).__name__, self.synopsis,
 
407
        return "<%s '%s' in %r>" % (type(self).__name__, self.short_name,
376
408
                                  self.project_set.offering)
377
409
 
 
410
    def can_submit(self, principal):
 
411
        return (self in principal.get_projects() and
 
412
                self.deadline > datetime.datetime.now())
 
413
 
 
414
    def submit(self, principal, path, revision):
 
415
        if not self.can_submit(principal):
 
416
            raise Exception('cannot submit')
 
417
 
 
418
        a = Assessed.get(Store.of(self), principal, self)
 
419
        ps = ProjectSubmission()
 
420
        ps.path = path
 
421
        ps.revision = revision
 
422
        ps.date_submitted = datetime.datetime.now()
 
423
        ps.assessed = a
 
424
 
 
425
        return ps
 
426
 
 
427
 
378
428
class ProjectGroup(Storm):
379
429
    __storm_table__ = "project_group"
380
430
 
398
448
        return "<%s %s in %r>" % (type(self).__name__, self.name,
399
449
                                  self.project_set.offering)
400
450
 
 
451
    def get_projects(self, offering=None, active_only=True):
 
452
        '''Return Projects that the group can submit.
 
453
 
 
454
        This will include projects in the project set which owns this group,
 
455
        unless the project set disallows groups (in which case none will be
 
456
        returned).
 
457
 
 
458
        Unless active_only is False, projects will only be returned if the
 
459
        group's offering is active.
 
460
 
 
461
        If an offering is specified, projects will only be returned if it
 
462
        matches the group's.
 
463
        '''
 
464
        return Store.of(self).find(Project,
 
465
            Project.project_set_id == ProjectSet.id,
 
466
            ProjectSet.id == self.project_set.id,
 
467
            ProjectSet.max_students_per_group > 0,
 
468
            ProjectSet.offering_id == Offering.id,
 
469
            (offering is None) or (Offering.id == offering.id),
 
470
            Semester.id == Offering.semester_id,
 
471
            (not active_only) or (Semester.state == u'current'))
 
472
 
 
473
 
 
474
    def get_permissions(self, user):
 
475
        if user.admin or user in self.members:
 
476
            return set(['submit_project'])
 
477
        else:
 
478
            return set()
 
479
 
401
480
class ProjectGroupMembership(Storm):
402
481
    __storm_table__ = "group_member"
403
482
    __storm_primary__ = "user_id", "project_group_id"
413
492
        return "<%s %r in %r>" % (type(self).__name__, self.user,
414
493
                                  self.project_group)
415
494
 
 
495
class Assessed(Storm):
 
496
    __storm_table__ = "assessed"
 
497
 
 
498
    id = Int(name="assessedid", primary=True)
 
499
    user_id = Int(name="loginid")
 
500
    user = Reference(user_id, User.id)
 
501
    project_group_id = Int(name="groupid")
 
502
    project_group = Reference(project_group_id, ProjectGroup.id)
 
503
 
 
504
    project_id = Int(name="projectid")
 
505
    project = Reference(project_id, Project.id)
 
506
 
 
507
    extensions = ReferenceSet(id, 'ProjectExtension.assessed_id')
 
508
    submissions = ReferenceSet(id, 'ProjectSubmission.assessed_id')
 
509
 
 
510
    def __repr__(self):
 
511
        return "<%s %r in %r>" % (type(self).__name__,
 
512
            self.user or self.project_group, self.project)
 
513
 
 
514
    @classmethod
 
515
    def get(cls, store, principal, project):
 
516
        t = type(principal)
 
517
        if t not in (User, ProjectGroup):
 
518
            raise AssertionError('principal must be User or ProjectGroup')
 
519
 
 
520
        a = store.find(cls,
 
521
            (t is User) or (cls.project_group_id == principal.id),
 
522
            (t is ProjectGroup) or (cls.user_id == principal.id),
 
523
            Project.id == project.id).one()
 
524
 
 
525
        if a is None:
 
526
            a = cls()
 
527
            if t is User:
 
528
                a.user = principal
 
529
            else:
 
530
                a.project_group = principal
 
531
            a.project = project
 
532
            store.add(a)
 
533
 
 
534
        return a
 
535
 
 
536
 
 
537
class ProjectExtension(Storm):
 
538
    __storm_table__ = "project_extension"
 
539
 
 
540
    id = Int(name="extensionid", primary=True)
 
541
    assessed_id = Int(name="assessedid")
 
542
    assessed = Reference(assessed_id, Assessed.id)
 
543
    deadline = DateTime()
 
544
    approver_id = Int(name="approver")
 
545
    approver = Reference(approver_id, User.id)
 
546
    notes = Unicode()
 
547
 
 
548
class ProjectSubmission(Storm):
 
549
    __storm_table__ = "project_submission"
 
550
 
 
551
    id = Int(name="submissionid", primary=True)
 
552
    assessed_id = Int(name="assessedid")
 
553
    assessed = Reference(assessed_id, Assessed.id)
 
554
    path = Unicode()
 
555
    revision = Int()
 
556
    date_submitted = DateTime()
 
557
 
 
558
 
416
559
# WORKSHEETS AND EXERCISES #
417
560
 
418
561
class Exercise(Storm):