36
36
import formencode.validators
38
from ivle.webapp.base.forms import BaseFormView
39
from ivle.webapp.base.plugins import ViewPlugin, MediaPlugin
38
40
from ivle.webapp.base.xhtml import XHTMLView
39
from ivle.webapp.base.plugins import ViewPlugin, MediaPlugin
40
41
from ivle.webapp import ApplicationRoot
42
43
from ivle.database import Subject, Semester, Offering, Enrolment, User,\
303
304
chained_validators = [OfferingUniquenessValidator()]
306
class BaseFormView(XHTMLView):
307
"""A base form view."""
309
def filter(self, stream, ctx):
310
return stream | HTMLFormFiller(data=ctx['data'])
312
def populate_state(self, state):
315
def get_return_url(self, obj):
316
return self.req.publisher.generate(obj)
318
def get_default_data(self, req):
319
raise NotImplementedError()
321
def save_object(self, req, data):
322
raise NotImplementedError()
326
raise NotImplementedError()
328
def populate(self, req, ctx):
329
if req.method == 'POST':
330
data = dict(req.get_fieldstorage())
332
self.populate_state(req)
333
data = self.validator.to_python(data, state=req)
335
obj = self.save_object(req, data)
338
req.throw_redirect(self.get_return_url(obj))
339
except formencode.Invalid, e:
341
errors = e.unpack_errors()
343
data = self.get_default_data(req)
351
ctx['context'] = self.context
352
ctx['data'] = data or {}
353
ctx['errors'] = errors
354
# If all of the fields validated, set the global form error.
355
if isinstance(errors, basestring):
356
ctx['error_value'] = errors
359
307
class OfferingEdit(BaseFormView):
360
308
"""A form to edit an offering's details."""
361
309
template = 'templates/offering-edit.html'