~azzar1/unity/add-show-desktop-key

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
1606.1.2 by William Grant
Add a FormEncode validator that matches the DB's valid_url_name.
18
import re
19
1539 by William Grant
Move BaseFormView into ivle.webapp.base.forms.
20
import formencode
1606.1.2 by William Grant
Add a FormEncode validator that matches the DB's valid_url_name.
21
import formencode.validators
1539 by William Grant
Move BaseFormView into ivle.webapp.base.forms.
22
from genshi.filters import HTMLFormFiller
23
24
from ivle.webapp.base.xhtml import XHTMLView
25
1606.1.2 by William Grant
Add a FormEncode validator that matches the DB's valid_url_name.
26
1539 by William Grant
Move BaseFormView into ivle.webapp.base.forms.
27
class BaseFormView(XHTMLView):
28
    """A base form view."""
29
1541 by William Grant
Document BaseFormView.
30
    @property
31
    def validator(self):
1545 by William Grant
Document the validator's state.
32
        """The FormEncode validator to use.
33
34
        The request will be passed in as state, after potentially being
35
        modified by populate_state().
36
        """
1541 by William Grant
Document BaseFormView.
37
        raise NotImplementedError()
1539 by William Grant
Move BaseFormView into ivle.webapp.base.forms.
38
39
    def populate_state(self, state):
1541 by William Grant
Document BaseFormView.
40
        """Populate the state given to the FormEncode validator.
41
42
        Subclasses can override this and set additional attributes.
43
        """
1539 by William Grant
Move BaseFormView into ivle.webapp.base.forms.
44
        pass
45
46
    def get_return_url(self, obj):
1541 by William Grant
Document BaseFormView.
47
        """Return the URL to which the completed form should redirect.
48
49
        By default this will redirect to the saved object.
50
        """
1539 by William Grant
Move BaseFormView into ivle.webapp.base.forms.
51
        return self.req.publisher.generate(obj)
52
53
    def get_default_data(self, req):
1541 by William Grant
Document BaseFormView.
54
        """Return a dict mapping field names to default form values.
55
56
        For an edit form, this should return the object's existing data.
57
        For a creation form, this should probably return an empty dict.
58
59
        This must be overridden by subclasses.
60
        """
1539 by William Grant
Move BaseFormView into ivle.webapp.base.forms.
61
        raise NotImplementedError()
62
63
    def save_object(self, req, data):
1541 by William Grant
Document BaseFormView.
64
        """Take the validated form data and turn it into an object.
65
66
        The object must then be returned.
67
68
        For an edit form, this should just overwrite data on an existing
69
        object.
70
        For a creation form, this should create a new object with the given
71
        data and add it to the request's store.
72
        """
73
        raise NotImplementedError()
74
75
    def filter(self, stream, ctx):
76
        return stream | HTMLFormFiller(data=ctx['data'])
1539 by William Grant
Move BaseFormView into ivle.webapp.base.forms.
77
78
    def populate(self, req, ctx):
79
        if req.method == 'POST':
80
            data = dict(req.get_fieldstorage())
81
            try:
82
                self.populate_state(req)
83
                data = self.validator.to_python(data, state=req)
84
85
                obj = self.save_object(req, data)
86
87
                req.store.commit()
88
                req.throw_redirect(self.get_return_url(obj))
89
            except formencode.Invalid, e:
90
                error_value = e.msg
91
                errors = e.unpack_errors()
92
        else:
93
            data = self.get_default_data(req)
94
            error_value = None
95
            errors = {}
96
97
        if errors:
98
            req.store.rollback()
99
100
        ctx['req'] = req
101
        ctx['context'] = self.context
102
        ctx['data'] = data or {}
103
        ctx['errors'] = errors
104
        # If all of the fields validated, set the global form error.
105
        if isinstance(errors, basestring):
106
            ctx['error_value'] = errors
107
108
1606.1.15 by William Grant
Permit underscores in all names.
109
VALID_URL_NAME = re.compile(r'^[a-z0-9][a-z0-9_\+\.\-]*$')
1606.1.2 by William Grant
Add a FormEncode validator that matches the DB's valid_url_name.
110
111
112
class URLNameValidator(formencode.validators.UnicodeString):
113
    def validate_python(self, value, state):
114
        super(URLNameValidator, self).validate_python(value, state)
115
        if not VALID_URL_NAME.match(value):
116
            raise formencode.Invalid(
1606.1.16 by William Grant
Fix validation messages to inform users that alphanumerics in names must be lowercase.
117
                'Must consist of a lowercase alphanumeric character followed '
118
                'by any number of lowercase alphanumerics, ., +, - or _.',
1606.1.2 by William Grant
Add a FormEncode validator that matches the DB's valid_url_name.
119
                value, state)