~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-12 03:16:41 UTC
  • Revision ID: grantw@unimelb.edu.au-20100212031641-8gajjv95z7illqa9
Move BaseFormView into ivle.webapp.base.forms.

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
import formencode
36
36
import formencode.validators
37
37
 
 
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
41
42
 
42
43
from ivle.database import Subject, Semester, Offering, Enrolment, User,\
303
304
    chained_validators = [OfferingUniquenessValidator()]
304
305
 
305
306
 
306
 
class BaseFormView(XHTMLView):
307
 
    """A base form view."""
308
 
 
309
 
    def filter(self, stream, ctx):
310
 
        return stream | HTMLFormFiller(data=ctx['data'])
311
 
 
312
 
    def populate_state(self, state):
313
 
        pass
314
 
 
315
 
    def get_return_url(self, obj):
316
 
        return self.req.publisher.generate(obj)
317
 
 
318
 
    def get_default_data(self, req):
319
 
        raise NotImplementedError()
320
 
 
321
 
    def save_object(self, req, data):
322
 
        raise NotImplementedError()
323
 
 
324
 
    @property
325
 
    def validator(self):
326
 
        raise NotImplementedError()
327
 
 
328
 
    def populate(self, req, ctx):
329
 
        if req.method == 'POST':
330
 
            data = dict(req.get_fieldstorage())
331
 
            try:
332
 
                self.populate_state(req)
333
 
                data = self.validator.to_python(data, state=req)
334
 
 
335
 
                obj = self.save_object(req, data)
336
 
 
337
 
                req.store.commit()
338
 
                req.throw_redirect(self.get_return_url(obj))
339
 
            except formencode.Invalid, e:
340
 
                error_value = e.msg
341
 
                errors = e.unpack_errors()
342
 
        else:
343
 
            data = self.get_default_data(req)
344
 
            error_value = None
345
 
            errors = {}
346
 
 
347
 
        if errors:
348
 
            req.store.rollback()
349
 
 
350
 
        ctx['req'] = 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
357
 
 
358
 
 
359
307
class OfferingEdit(BaseFormView):
360
308
    """A form to edit an offering's details."""
361
309
    template = 'templates/offering-edit.html'