##// END OF EJS Templates
validation-schema: added StringBoolean type and IPAddr validator.
marcink -
r1149:94d0095b default
parent child Browse files
Show More
@@ -1,105 +1,137 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2016-2016 RhodeCode GmbH
4 4 #
5 5 # This program is free software: you can redistribute it and/or modify
6 6 # it under the terms of the GNU Affero General Public License, version 3
7 7 # (only), as published by the Free Software Foundation.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU Affero General Public License
15 15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 16 #
17 17 # This program is dual-licensed. If you wish to learn more about the
18 18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20 20
21 21 import colander
22 22
23 23 from rhodecode.model.db import User, UserGroup
24 24
25 25
26 26 class GroupNameType(colander.String):
27 27 SEPARATOR = '/'
28 28
29 29 def deserialize(self, node, cstruct):
30 30 result = super(GroupNameType, self).deserialize(node, cstruct)
31 31 return self._replace_extra_slashes(result)
32 32
33 33 def _replace_extra_slashes(self, path):
34 34 path = path.split(self.SEPARATOR)
35 35 path = [item for item in path if item]
36 36 return self.SEPARATOR.join(path)
37 37
38 38
39 class StringBooleanType(colander.String):
40 true_values = ['true', 't', 'yes', 'y', 'on', '1']
41 false_values = ['false', 'f', 'no', 'n', 'off', '0']
42
43 def serialize(self, node, appstruct):
44 if appstruct is colander.null:
45 return colander.null
46 if not isinstance(appstruct, bool):
47 raise colander.Invalid(node, '%r is not a boolean' % appstruct)
48
49 return appstruct and 'true' or 'false'
50
51 def deserialize(self, node, cstruct):
52 if cstruct is colander.null:
53 return colander.null
54
55 if isinstance(cstruct, bool):
56 return cstruct
57
58 if not isinstance(cstruct, basestring):
59 raise colander.Invalid(node, '%r is not a string' % cstruct)
60
61 value = cstruct.lower()
62 if value in self.true_values:
63 return True
64 elif value in self.false_values:
65 return False
66 else:
67 raise colander.Invalid(
68 node, '{} value cannot be translated to bool'.format(value))
69
70
39 71 class UserOrUserGroupType(colander.SchemaType):
40 72 """ colander Schema type for valid rhodecode user and/or usergroup """
41 73 scopes = ('user', 'usergroup')
42 74
43 75 def __init__(self):
44 76 self.users = 'user' in self.scopes
45 77 self.usergroups = 'usergroup' in self.scopes
46 78
47 79 def serialize(self, node, appstruct):
48 80 if appstruct is colander.null:
49 81 return colander.null
50 82
51 83 if self.users:
52 84 if isinstance(appstruct, User):
53 85 if self.usergroups:
54 86 return 'user:%s' % appstruct.username
55 87 return appstruct.username
56 88
57 89 if self.usergroups:
58 90 if isinstance(appstruct, UserGroup):
59 91 if self.users:
60 92 return 'usergroup:%s' % appstruct.users_group_name
61 93 return appstruct.users_group_name
62 94
63 95 raise colander.Invalid(
64 96 node, '%s is not a valid %s' % (appstruct, ' or '.join(self.scopes)))
65 97
66 98 def deserialize(self, node, cstruct):
67 99 if cstruct is colander.null:
68 100 return colander.null
69 101
70 102 user, usergroup = None, None
71 103 if self.users:
72 104 if cstruct.startswith('user:'):
73 105 user = User.get_by_username(cstruct.split(':')[1])
74 106 else:
75 107 user = User.get_by_username(cstruct)
76 108
77 109 if self.usergroups:
78 110 if cstruct.startswith('usergroup:'):
79 111 usergroup = UserGroup.get_by_group_name(cstruct.split(':')[1])
80 112 else:
81 113 usergroup = UserGroup.get_by_group_name(cstruct)
82 114
83 115 if self.users and self.usergroups:
84 116 if user and usergroup:
85 117 raise colander.Invalid(node, (
86 118 '%s is both a user and usergroup, specify which '
87 119 'one was wanted by prepending user: or usergroup: to the '
88 120 'name') % cstruct)
89 121
90 122 if self.users and user:
91 123 return user
92 124
93 125 if self.usergroups and usergroup:
94 126 return usergroup
95 127
96 128 raise colander.Invalid(
97 129 node, '%s is not a valid %s' % (cstruct, ' or '.join(self.scopes)))
98 130
99 131
100 132 class UserType(UserOrUserGroupType):
101 133 scopes = ('user',)
102 134
103 135
104 136 class UserGroupType(UserOrUserGroupType):
105 137 scopes = ('usergroup',)
@@ -1,25 +1,38 b''
1 1 import os
2 2 import re
3 3
4 4 import ipaddress
5 5 import colander
6 6
7 7 from rhodecode.translation import _
8 8 from rhodecode.lib.utils2 import glob2re
9 9
10 10
11 11 def ip_addr_validator(node, value):
12 12 try:
13 13 # this raises an ValueError if address is not IpV4 or IpV6
14 14 ipaddress.ip_network(value, strict=False)
15 15 except ValueError:
16 16 msg = _(u'Please enter a valid IPv4 or IpV6 address')
17 17 raise colander.Invalid(node, msg)
18 18
19 19
20 class IpAddrValidator(object):
21 def __init__(self, strict=True):
22 self.strict = strict
23
24 def __call__(self, node, value):
25 try:
26 # this raises an ValueError if address is not IpV4 or IpV6
27 ipaddress.ip_network(value, strict=self.strict)
28 except ValueError:
29 msg = _(u'Please enter a valid IPv4 or IpV6 address')
30 raise colander.Invalid(node, msg)
31
32
20 33 def glob_validator(node, value):
21 34 try:
22 35 re.compile('^' + glob2re(value) + '$')
23 36 except Exception:
24 37 msg = _(u'Invalid glob pattern')
25 38 raise colander.Invalid(node, msg)
General Comments 0
You need to be logged in to leave comments. Login now