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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# IVLE
# Copyright (C) 2010 The University of Melbourne
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import re

import formencode
import formencode.validators
from genshi.filters import HTMLFormFiller

from ivle.webapp.base.xhtml import XHTMLView


class BaseFormView(XHTMLView):
    """A base form view."""

    @property
    def validator(self):
        """The FormEncode validator to use.

        The request will be passed in as state, after potentially being
        modified by populate_state().
        """
        raise NotImplementedError()

    def populate_state(self, state):
        """Populate the state given to the FormEncode validator.

        Subclasses can override this and set additional attributes.
        """
        pass

    def get_return_url(self, obj):
        """Return the URL to which the completed form should redirect.

        By default this will redirect to the saved object.
        """
        return self.req.publisher.generate(obj)

    def get_default_data(self, req):
        """Return a dict mapping field names to default form values.

        For an edit form, this should return the object's existing data.
        For a creation form, this should probably return an empty dict.

        This must be overridden by subclasses.
        """
        raise NotImplementedError()

    def save_object(self, req, data):
        """Take the validated form data and turn it into an object.

        The object must then be returned.

        For an edit form, this should just overwrite data on an existing
        object.
        For a creation form, this should create a new object with the given
        data and add it to the request's store.
        """
        raise NotImplementedError()

    def filter(self, stream, ctx):
        return stream | HTMLFormFiller(data=ctx['data'])

    def populate(self, req, ctx):
        if req.method == 'POST':
            data = dict(req.get_fieldstorage())
            try:
                self.populate_state(req)
                data = self.validator.to_python(data, state=req)

                obj = self.save_object(req, data)

                req.store.commit()
                req.throw_redirect(self.get_return_url(obj))
            except formencode.Invalid, e:
                error_value = e.msg
                errors = e.unpack_errors()
        else:
            data = self.get_default_data(req)
            error_value = None
            errors = {}

        if errors:
            req.store.rollback()

        ctx['req'] = req
        ctx['context'] = self.context
        ctx['data'] = data or {}
        ctx['errors'] = errors
        # If all of the fields validated, set the global form error.
        if isinstance(errors, basestring):
            ctx['error_value'] = errors


VALID_URL_NAME = re.compile(r'^[a-z0-9][a-z0-9_\+\.\-]*$')


class URLNameValidator(formencode.validators.UnicodeString):
    def validate_python(self, value, state):
        super(URLNameValidator, self).validate_python(value, state)
        if not VALID_URL_NAME.match(value):
            raise formencode.Invalid(
                'Must consist of an alphanumeric character followed by any '
                'number of alphanumerics, ., +, - or _.',
                value, state)