root / lib / pyzinn / validator.py @ a899390a2a8aca3b3b4617dd7cd96a19e64b489c

View | Annotate | Download (8.1 KB)

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