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
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
286
288
# As we go, calculate the total score for this subject
450
452
return new_offering
455
class OfferingCloneWorksheetsSchema(formencode.Schema):
456
subject = formencode.All(
457
SubjectValidator(), formencode.validators.UnicodeString())
458
semester = formencode.All(
459
SemesterValidator(), formencode.validators.UnicodeString())
462
class OfferingCloneWorksheets(BaseFormView):
463
"""A form to clone worksheets from one offering to another."""
464
template = 'templates/offering-clone-worksheets.html'
467
def authorize(self, req):
468
return req.user is not None and req.user.admin
472
return OfferingCloneWorksheetsSchema()
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)
480
def get_default_data(self, req):
483
def save_object(self, req, data):
484
if self.context.worksheets.count() > 0:
486
"Cannot clone to target with existing worksheets.")
487
offering = req.store.find(
488
Offering, subject=data['subject'], semester=data['semester']).one()
490
raise BadRequest("No such offering.")
491
if offering.worksheets.count() == 0:
492
raise BadRequest("Source offering has no worksheets.")
494
self.context.clone_worksheets(offering)
453
498
class UserValidator(formencode.FancyValidator):
454
499
"""A FormEncode validator that turns a username into a user.
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),