2
# Copyright (C) 2010 The University of Melbourne
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
from genshi.filters import HTMLFormFiller
21
from ivle.webapp.base.xhtml import XHTMLView
23
class BaseFormView(XHTMLView):
24
"""A base form view."""
26
def filter(self, stream, ctx):
27
return stream | HTMLFormFiller(data=ctx['data'])
29
def populate_state(self, state):
32
def get_return_url(self, obj):
33
return self.req.publisher.generate(obj)
35
def get_default_data(self, req):
36
raise NotImplementedError()
38
def save_object(self, req, data):
39
raise NotImplementedError()
43
raise NotImplementedError()
45
def populate(self, req, ctx):
46
if req.method == 'POST':
47
data = dict(req.get_fieldstorage())
49
self.populate_state(req)
50
data = self.validator.to_python(data, state=req)
52
obj = self.save_object(req, data)
55
req.throw_redirect(self.get_return_url(obj))
56
except formencode.Invalid, e:
58
errors = e.unpack_errors()
60
data = self.get_default_data(req)
68
ctx['context'] = self.context
69
ctx['data'] = data or {}
70
ctx['errors'] = errors
71
# If all of the fields validated, set the global form error.
72
if isinstance(errors, basestring):
73
ctx['error_value'] = errors