1539
by William Grant
Move BaseFormView into ivle.webapp.base.forms. |
1 |
# IVLE
|
2 |
# Copyright (C) 2010 The University of Melbourne
|
|
3 |
#
|
|
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.
|
|
8 |
#
|
|
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.
|
|
13 |
#
|
|
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
|
|
17 |
||
18 |
import formencode |
|
19 |
from genshi.filters import HTMLFormFiller |
|
20 |
||
21 |
from ivle.webapp.base.xhtml import XHTMLView |
|
22 |
||
23 |
class BaseFormView(XHTMLView): |
|
24 |
"""A base form view."""
|
|
25 |
||
1541
by William Grant
Document BaseFormView. |
26 |
@property
|
27 |
def validator(self): |
|
1545
by William Grant
Document the validator's state. |
28 |
"""The FormEncode validator to use.
|
29 |
||
30 |
The request will be passed in as state, after potentially being
|
|
31 |
modified by populate_state().
|
|
32 |
"""
|
|
1541
by William Grant
Document BaseFormView. |
33 |
raise NotImplementedError() |
1539
by William Grant
Move BaseFormView into ivle.webapp.base.forms. |
34 |
|
35 |
def populate_state(self, state): |
|
1541
by William Grant
Document BaseFormView. |
36 |
"""Populate the state given to the FormEncode validator.
|
37 |
||
38 |
Subclasses can override this and set additional attributes.
|
|
39 |
"""
|
|
1539
by William Grant
Move BaseFormView into ivle.webapp.base.forms. |
40 |
pass
|
41 |
||
42 |
def get_return_url(self, obj): |
|
1541
by William Grant
Document BaseFormView. |
43 |
"""Return the URL to which the completed form should redirect.
|
44 |
||
45 |
By default this will redirect to the saved object.
|
|
46 |
"""
|
|
1539
by William Grant
Move BaseFormView into ivle.webapp.base.forms. |
47 |
return self.req.publisher.generate(obj) |
48 |
||
49 |
def get_default_data(self, req): |
|
1541
by William Grant
Document BaseFormView. |
50 |
"""Return a dict mapping field names to default form values.
|
51 |
||
52 |
For an edit form, this should return the object's existing data.
|
|
53 |
For a creation form, this should probably return an empty dict.
|
|
54 |
||
55 |
This must be overridden by subclasses.
|
|
56 |
"""
|
|
1539
by William Grant
Move BaseFormView into ivle.webapp.base.forms. |
57 |
raise NotImplementedError() |
58 |
||
59 |
def save_object(self, req, data): |
|
1541
by William Grant
Document BaseFormView. |
60 |
"""Take the validated form data and turn it into an object.
|
61 |
||
62 |
The object must then be returned.
|
|
63 |
||
64 |
For an edit form, this should just overwrite data on an existing
|
|
65 |
object.
|
|
66 |
For a creation form, this should create a new object with the given
|
|
67 |
data and add it to the request's store.
|
|
68 |
"""
|
|
69 |
raise NotImplementedError() |
|
70 |
||
71 |
def filter(self, stream, ctx): |
|
72 |
return stream | HTMLFormFiller(data=ctx['data']) |
|
1539
by William Grant
Move BaseFormView into ivle.webapp.base.forms. |
73 |
|
74 |
def populate(self, req, ctx): |
|
75 |
if req.method == 'POST': |
|
76 |
data = dict(req.get_fieldstorage()) |
|
77 |
try: |
|
78 |
self.populate_state(req) |
|
79 |
data = self.validator.to_python(data, state=req) |
|
80 |
||
81 |
obj = self.save_object(req, data) |
|
82 |
||
83 |
req.store.commit() |
|
84 |
req.throw_redirect(self.get_return_url(obj)) |
|
85 |
except formencode.Invalid, e: |
|
86 |
error_value = e.msg |
|
87 |
errors = e.unpack_errors() |
|
88 |
else: |
|
89 |
data = self.get_default_data(req) |
|
90 |
error_value = None |
|
91 |
errors = {} |
|
92 |
||
93 |
if errors: |
|
94 |
req.store.rollback() |
|
95 |
||
96 |
ctx['req'] = req |
|
97 |
ctx['context'] = self.context |
|
98 |
ctx['data'] = data or {} |
|
99 |
ctx['errors'] = errors |
|
100 |
# If all of the fields validated, set the global form error.
|
|
101 |
if isinstance(errors, basestring): |
|
102 |
ctx['error_value'] = errors |
|
103 |
||
104 |