root / lib / pyzinn / validator.py @ a899390a2a8aca3b3b4617dd7cd96a19e64b489c

View | Annotate | Download (8.1 KB)

1
# Copyright (c) 2009 Lost Oasis, IELO <info@ielo.net>
2
#
3
# This file is part of ZINN.
4
# 
5
# ZINN is free software: you can redistribute it and/or modify it under
6
# the terms of the GNU General Public License as published by the Free
7
# Software Foundation, either version 3 of the License, or (at your
8
# option) any later version.
9
# 
10
# ZINN is distributed in the hope that it will be useful, but WITHOUT
11
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13
# for more details.
14
# 
15
# You should have received a copy of the GNU General Public License
16
# along with ZINN.  If not, see <http://www.gnu.org/licenses/>.
17
18
import formencode, gettext, re
19
_ = gettext.gettext
20
from formencode import validators
21
22
import sys
23
24
class LabelValidator(formencode.FancyValidator):
25
    messages = {
26
        'invalid' : _('Enter only letters, numbers, . (dot), - (hyphen), : (double dot) or _ (underscore)')
27
    }
28
    def from_python(self, value, state=None):
29
        #store labels like 'client:vsite:object'
30
        r = None
31
        iv = 'Enter only letters, numbers, . (dot), - (hyphen), : (double dot)  or _ (underscore)'
32
        r = re.compile('^[\w:\\.\\-0-9]*$', re.UNICODE)
33
        v = formencode.All(
34
            validators.String(accept_python=True, list_joiner='.', strip=True),
35
            validators.PlainText(regex=r,
36
                                messages={'invalid': iv}, accept_python=False),
37
            validators.String(
38
                    min=2,
39
                    max=255,
40
                    accept_python=False,
41
                    messages={
42
                    'tooLong': _('Enter a value less than %(max)s characters long'),
43
                    'tooShort': _('Enter a value %(min)s characters long or more'),
44
                    }
45
            ),
46
        )
47
        r = v.from_python(value, state)
48
        if not r:
49
            raise formencode.Invalid(self.message('invalid', state),
50
            r, state)
51
        return r
52
53
    def to_python(self, value, state=None):
54
        return value
55
56
class UserPublicLabelValidator(formencode.FancyValidator):
57
    messages = {
58
        'invalid' : _('Enter only letters, numbers, space, . (dot), - (hyphen), : (double dot) or _ (underscore)')
59
    }
60
    def from_python(self, value, state=None):
61
        #store labels like 'client:vsite:object'
62
        r = None
63
        iv = 'Enter only letters, numbers, space, . (dot), - (hyphen), : (double dot)  or _ (underscore)'
64
        r = re.compile('^[\w:\\.\s\\ \\-0-9]*$', re.UNICODE)
65
        v = formencode.All(
66
            validators.String(accept_python=True, list_joiner='.', strip=True),
67
            validators.PlainText(regex=r,
68
                                messages={'invalid': iv}, accept_python=False),
69
            validators.String(
70
                    min=2,
71
                    max=255,
72
                    accept_python=False,
73
                    messages={
74
                    'tooLong': _('Enter a value less than %(max)s characters long'),
75
                    'tooShort': _('Enter a value %(min)s characters long or more'),
76
                    }
77
            ),
78
        )
79
        r = v.from_python(value, state)
80
        if not r:
81
            raise formencode.Invalid(self.message('invalid', state),
82
            r, state)
83
        return r
84
85
    def to_python(self, value, state=None):
86
        return value
87
88
class ServiceLabelValidator(formencode.FancyValidator):
89
    messages = {
90
        'invalid' : _('Enter only letters, numbers, - (hyphen), or _ (underscore)')
91
    }
92
    def from_python(self, value, state=None):
93
        #store labels like 'client:vsite:object'
94
        r = None
95
        iv = 'Enter only letters, numbers, - (hyphen), or _ (underscore)'
96
        r = re.compile('^[\w\\-0-9]*$')
97
        v = formencode.All(
98
            validators.String(accept_python=True, list_joiner='', strip=True),
99
            validators.PlainText(regex=r,
100
                                messages={'invalid': iv}, accept_python=False),
101
            validators.String(
102
                    min=2,
103
                    max=255,
104
                    accept_python=False,
105
                    messages={
106
                    'tooLong': _('Enter a value less than %(max)s characters long'),
107
                    'tooShort': _('Enter a value %(min)s characters long or more'),
108
                    }
109
            ),
110
        )
111
        r = v.from_python(value, state)
112
        if not r:
113
            raise formencode.Invalid(self.message('invalid', state),
114
            r, state)
115
        return r
116
117
    def to_python(self, value, state=None):
118
        return value
119
120
class ClientLabelValidator(formencode.FancyValidator):
121
    #messages = {
122
    #    'invalid' : _('Invalid client name')
123
    #}
124
    messages = {
125
        'invalid' : _('Enter only letters, numbers, - (hyphen), or _ (underscore)')
126
    }
127
    def from_python(self, value, state=None):
128
        import pprint
129
        pprint.pprint(value)
130
        print "hoooooooooooooooooooooooooooooooooooooooo"
131
	# TODO: restrict to a complete list of i18n countries, translate countries in alpha2 (eg: France -> FR)
132
        r = None
133
        iv = 'Enter only letters, numbers, - (hyphen), or _ (underscore)'
134
        r = re.compile('^[\w\\-0-9]*$')
135
        v = formencode.All(
136
            validators.String(accept_python=True, list_joiner=' ', strip=True),
137
            validators.PlainText(regex=r,
138
                                messages={'invalid': iv}, accept_python=False),
139
            validators.String(
140
                    min=2,
141
                    max=128,
142
                    accept_python=False,
143
                    messages={
144
                    'tooLong': _('Enter a value less than %(max)s characters long'),
145
                    'tooShort': _('Enter a value %(min)s characters long or more'),
146
                    }
147
            ),
148
        )
149
        r = v.from_python(value, state)
150
	r = str.strip(r)
151
        if not r:
152
            raise formencode.Invalid(self.message('invalid', state),
153
            r, state)
154
        return r
155
156
    def to_python(self, value, state=None):
157
        import pprint
158
        pprint.pprint(value)
159
        print "hooooooooaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaao"
160
	# TODO: translate alpha2 countries into i18n name (eg: FR -> France)
161
        return value
162
163
class ClientAddressValidator(formencode.FancyValidator):
164
    messages = {
165
        'invalid' : _('Invalid Address')
166
    }
167
    def from_python(self, value, state=None):
168
        r = None
169
        v = formencode.All(
170
            validators.String(accept_python=True, list_joiner=' ', strip=True),
171
            validators.String(
172
                    min=2,
173
                    max=250,
174
                    accept_python=False,
175
                    messages={
176
                    'tooLong': _('Enter a value less than %(max)s characters long'),
177
                    'tooShort': _('Enter a value %(min)s characters long or more'),
178
                    }
179
            ),
180
        )
181
        r = v.from_python(value, state)
182
        if not r:
183
            raise formencode.Invalid(self.message('invalid', state),
184
            r, state)
185
        return r
186
187
    def to_python(self, value, state=None):
188
        return value
189
190
class ClientCountryValidator(formencode.FancyValidator):
191
    messages = {
192
        'invalid' : _('Invalid Country')
193
    }
194
    def from_python(self, value, state=None):
195
	# TODO: restrict to a complete list of i18n countries, translate countries in alpha2 (eg: France -> FR)
196
        r = None
197
        v = formencode.All(
198
            validators.String(accept_python=True, list_joiner='.', strip=True),
199
            validators.String(
200
                    min=2,
201
                    max=60,
202
                    accept_python=False,
203
                    messages={
204
                    'tooLong': _('Enter a value less than %(max)s characters long'),
205
                    'tooShort': _('Enter a value %(min)s characters long or more'),
206
                    }
207
            ),
208
        )
209
        r = v.from_python(value, state)
210
        if not r:
211
            raise formencode.Invalid(self.message('invalid', state),
212
            r, state)
213
        return r
214
215
    def to_python(self, value, state=None):
216
	# TODO: translate alpha2 countries into i18n name (eg: FR -> France)
217
        return value
218
219
# vim: tabstop=4 expandtab shiftwidth=4 textwidth=79