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

« back to all changes in this revision

Viewing changes to ivle/webapp/admin/subject.py

  • Committer: William Grant
  • Date: 2010-02-15 10:09:00 UTC
  • Revision ID: grantw@unimelb.edu.au-20100215100900-1gp5wy3yrduxt9bq
Add UI to clone worksheets between offerings -- replacing ivle-cloneworksheets.

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
from ivle.webapp.base.forms import BaseFormView
39
39
from ivle.webapp.base.plugins import ViewPlugin, MediaPlugin
40
40
from ivle.webapp.base.xhtml import XHTMLView
 
41
from ivle.webapp.errors import BadRequest
41
42
from ivle.webapp import ApplicationRoot
42
43
 
43
44
from ivle.database import Subject, Semester, Offering, Enrolment, User,\
281
282
        ctx['format_datetime'] = ivle.date.make_date_nice
282
283
        ctx['format_datetime_short'] = ivle.date.format_datetime_for_paragraph
283
284
        ctx['OfferingEdit'] = OfferingEdit
 
285
        ctx['OfferingCloneWorksheets'] = OfferingCloneWorksheets
284
286
        ctx['GroupsView'] = GroupsView
285
287
 
286
288
        # As we go, calculate the total score for this subject
450
452
        return new_offering
451
453
 
452
454
 
 
455
class OfferingCloneWorksheetsSchema(formencode.Schema):
 
456
    subject = formencode.All(
 
457
        SubjectValidator(), formencode.validators.UnicodeString())
 
458
    semester = formencode.All(
 
459
        SemesterValidator(), formencode.validators.UnicodeString())
 
460
 
 
461
 
 
462
class OfferingCloneWorksheets(BaseFormView):
 
463
    """A form to clone worksheets from one offering to another."""
 
464
    template = 'templates/offering-clone-worksheets.html'
 
465
    tab = 'subjects'
 
466
 
 
467
    def authorize(self, req):
 
468
        return req.user is not None and req.user.admin
 
469
 
 
470
    @property
 
471
    def validator(self):
 
472
        return OfferingCloneWorksheetsSchema()
 
473
 
 
474
    def populate(self, req, ctx):
 
475
        super(OfferingCloneWorksheets, self).populate(req, ctx)
 
476
        ctx['subjects'] = req.store.find(Subject).order_by(Subject.name)
 
477
        ctx['semesters'] = req.store.find(Semester).order_by(
 
478
            Semester.year, Semester.semester)
 
479
 
 
480
    def get_default_data(self, req):
 
481
        return {}
 
482
 
 
483
    def save_object(self, req, data):
 
484
        if self.context.worksheets.count() > 0:
 
485
            raise BadRequest(
 
486
                "Cannot clone to target with existing worksheets.")
 
487
        offering = req.store.find(
 
488
            Offering, subject=data['subject'], semester=data['semester']).one()
 
489
        if offering is None:
 
490
            raise BadRequest("No such offering.")
 
491
        if offering.worksheets.count() == 0:
 
492
            raise BadRequest("Source offering has no worksheets.")
 
493
 
 
494
        self.context.clone_worksheets(offering)
 
495
        return self.context
 
496
 
 
497
 
453
498
class UserValidator(formencode.FancyValidator):
454
499
    """A FormEncode validator that turns a username into a user.
455
500
 
631
676
             (Semester, '+edit', SemesterEdit),
632
677
             (Offering, '+index', OfferingView),
633
678
             (Offering, '+edit', OfferingEdit),
 
679
             (Offering, '+clone-worksheets', OfferingCloneWorksheets),
634
680
             (Offering, ('+enrolments', '+index'), EnrolmentsView),
635
681
             (Offering, ('+enrolments', '+new'), EnrolView),
636
682
             (Offering, ('+projects', '+index'), OfferingProjectsView),