annotate rhodecode/model/validators.py @ 2466:7010dc12f10c codereview

Added rewritten validators module + tests
author Marcin Kuzminski <marcin@python-works.com>
date Sun, 17 Jun 2012 21:31:31 +0200
parents
children 9225597688f4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2466
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1 """
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
2 Set of generic validators
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
3 """
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
4 import os
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
5 import re
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
6 import formencode
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
7 import logging
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
8 from pylons.i18n.translation import _
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
9 from webhelpers.pylonslib.secure_form import authentication_token
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
10
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
11 from formencode.validators import (
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
12 UnicodeString, OneOf, Int, Number, Regex, Email, Bool, StringBoolean, Set
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
13 )
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
14
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
15 from rhodecode.lib.utils import repo_name_slug
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
16 from rhodecode.model.db import RepoGroup, Repository, UsersGroup, User
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
17 from rhodecode.lib.auth import authenticate
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
18 from rhodecode.lib.exceptions import LdapImportError
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
19 from rhodecode.config.routing import ADMIN_PREFIX
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
20 # silence warnings and pylint
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
21 UnicodeString, OneOf, Int, Number, Regex, Email, Bool, StringBoolean, Set
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
22
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
23 log = logging.getLogger(__name__)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
24
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
25
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
26 class StateObj(object):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
27 """
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
28 this is needed to translate the messages using _() in validators
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
29 """
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
30 _ = staticmethod(_)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
31
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
32
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
33 def M(self, key, state=None, **kwargs):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
34 """
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
35 returns string from self.message based on given key,
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
36 passed kw params are used to substitute %(named)s params inside
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
37 translated strings
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
38
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
39 :param msg:
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
40 :param state:
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
41 """
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
42 if state is None:
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
43 state = StateObj()
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
44 else:
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
45 state._ = staticmethod(_)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
46 #inject validator into state object
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
47 return self.message(key, state, **kwargs)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
48
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
49
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
50 def ValidUsername(edit=False, old_data={}):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
51 class _validator(formencode.validators.FancyValidator):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
52 messages = {
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
53 'username_exists': _(u'Username "%(username)s" already exists'),
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
54 'system_invalid_username':
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
55 _(u'Username "%(username)s" is forbidden'),
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
56 'invalid_username':
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
57 _(u'Username may only contain alphanumeric characters '
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
58 'underscores, periods or dashes and must begin with '
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
59 'alphanumeric character')
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
60 }
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
61
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
62 def validate_python(self, value, state):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
63 if value in ['default', 'new_user']:
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
64 msg = M(self, 'system_invalid_username', state, username=value)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
65 raise formencode.Invalid(msg, value, state)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
66 #check if user is unique
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
67 old_un = None
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
68 if edit:
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
69 old_un = User.get(old_data.get('user_id')).username
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
70
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
71 if old_un != value or not edit:
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
72 if User.get_by_username(value, case_insensitive=True):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
73 msg = M(self, 'username_exists', state, username=value)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
74 raise formencode.Invalid(msg, value, state)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
75
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
76 if re.match(r'^[a-zA-Z0-9]{1}[a-zA-Z0-9\-\_\.]+$', value) is None:
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
77 msg = M(self, 'invalid_username', state)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
78 raise formencode.Invalid(msg, value, state)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
79 return _validator
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
80
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
81
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
82 def ValidRepoUser():
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
83 class _validator(formencode.validators.FancyValidator):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
84 messages = {
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
85 'invalid_username': _(u'Username %(username)s is not valid')
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
86 }
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
87
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
88 def validate_python(self, value, state):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
89 try:
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
90 User.query().filter(User.active == True)\
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
91 .filter(User.username == value).one()
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
92 except Exception:
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
93 msg = M(self, 'invalid_username', state, username=value)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
94 raise formencode.Invalid(msg, value, state,
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
95 error_dict=dict(username=msg)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
96 )
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
97
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
98 return _validator
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
99
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
100
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
101 def ValidUsersGroup(edit=False, old_data={}):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
102 class _validator(formencode.validators.FancyValidator):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
103 messages = {
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
104 'invalid_group': _(u'Invalid users group name'),
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
105 'group_exist': _(u'Users group "%(usersgroup)s" already exists'),
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
106 'invalid_usersgroup_name':
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
107 _(u'users group name may only contain alphanumeric '
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
108 'characters underscores, periods or dashes and must begin '
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
109 'with alphanumeric character')
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
110 }
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
111
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
112 def validate_python(self, value, state):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
113 if value in ['default']:
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
114 msg = M(self, 'invalid_group', state)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
115 raise formencode.Invalid(msg, value, state,
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
116 error_dict=dict(users_group_name=msg)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
117 )
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
118 #check if group is unique
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
119 old_ugname = None
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
120 if edit:
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
121 old_id = old_data.get('users_group_id')
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
122 old_ugname = UsersGroup.get(old_id).users_group_name
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
123
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
124 if old_ugname != value or not edit:
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
125 is_existing_group = UsersGroup.get_by_group_name(value,
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
126 case_insensitive=True)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
127 if is_existing_group:
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
128 msg = M(self, 'group_exist', state, usersgroup=value)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
129 raise formencode.Invalid(msg, value, state,
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
130 error_dict=dict(users_group_name=msg)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
131 )
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
132
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
133 if re.match(r'^[a-zA-Z0-9]{1}[a-zA-Z0-9\-\_\.]+$', value) is None:
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
134 msg = M(self, 'invalid_usersgroup_name', state)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
135 raise formencode.Invalid(msg, value, state,
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
136 error_dict=dict(users_group_name=msg)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
137 )
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
138
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
139 return _validator
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
140
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
141
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
142 def ValidReposGroup(edit=False, old_data={}):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
143 class _validator(formencode.validators.FancyValidator):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
144 messages = {
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
145 'group_parent_id': _(u'Cannot assign this group as parent'),
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
146 'group_exists': _(u'Group "%(group_name)s" already exists'),
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
147 'repo_exists':
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
148 _(u'Repository with name "%(group_name)s" already exists')
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
149 }
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
150
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
151 def validate_python(self, value, state):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
152 # TODO WRITE VALIDATIONS
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
153 group_name = value.get('group_name')
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
154 group_parent_id = value.get('group_parent_id')
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
155
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
156 # slugify repo group just in case :)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
157 slug = repo_name_slug(group_name)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
158
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
159 # check for parent of self
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
160 parent_of_self = lambda: (
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
161 old_data['group_id'] == int(group_parent_id)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
162 if group_parent_id else False
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
163 )
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
164 if edit and parent_of_self():
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
165 msg = M(self, 'group_parent_id', state)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
166 raise formencode.Invalid(msg, value, state,
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
167 error_dict=dict(group_parent_id=msg)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
168 )
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
169
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
170 old_gname = None
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
171 if edit:
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
172 old_gname = RepoGroup.get(old_data.get('group_id')).group_name
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
173
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
174 if old_gname != group_name or not edit:
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
175
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
176 # check group
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
177 gr = RepoGroup.query()\
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
178 .filter(RepoGroup.group_name == slug)\
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
179 .filter(RepoGroup.group_parent_id == group_parent_id)\
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
180 .scalar()
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
181
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
182 if gr:
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
183 msg = M(self, 'group_exists', state, group_name=slug)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
184 raise formencode.Invalid(msg, value, state,
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
185 error_dict=dict(group_name=msg)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
186 )
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
187
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
188 # check for same repo
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
189 repo = Repository.query()\
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
190 .filter(Repository.repo_name == slug)\
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
191 .scalar()
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
192
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
193 if repo:
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
194 msg = M(self, 'repo_exists', state, group_name=slug)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
195 raise formencode.Invalid(msg, value, state,
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
196 error_dict=dict(group_name=msg)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
197 )
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
198
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
199 return _validator
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
200
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
201
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
202 def ValidPassword():
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
203 class _validator(formencode.validators.FancyValidator):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
204 messages = {
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
205 'invalid_password':
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
206 _(u'Invalid characters (non-ascii) in password')
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
207 }
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
208
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
209 def validate_python(self, value, state):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
210 try:
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
211 (value or '').decode('ascii')
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
212 except UnicodeError:
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
213 msg = M(self, 'invalid_password', state)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
214 raise formencode.Invalid(msg, value, state,)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
215 return _validator
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
216
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
217
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
218 def ValidPasswordsMatch():
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
219 class _validator(formencode.validators.FancyValidator):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
220 messages = {
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
221 'password_mismatch': _(u'Passwords do not match'),
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
222 }
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
223
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
224 def validate_python(self, value, state):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
225
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
226 pass_val = value.get('password') or value.get('new_password')
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
227 if pass_val != value['password_confirmation']:
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
228 msg = M(self, 'password_mismatch', state)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
229 raise formencode.Invalid(msg, value, state,
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
230 error_dict=dict(password_confirmation=msg)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
231 )
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
232 return _validator
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
233
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
234
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
235 def ValidAuth():
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
236 class _validator(formencode.validators.FancyValidator):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
237 messages = {
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
238 'invalid_password': _(u'invalid password'),
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
239 'invalid_username': _(u'invalid user name'),
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
240 'disabled_account': _(u'Your account is disabled')
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
241 }
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
242
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
243 def validate_python(self, value, state):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
244 password = value['password']
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
245 username = value['username']
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
246
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
247 if not authenticate(username, password):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
248 user = User.get_by_username(username)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
249 if user and user.active is False:
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
250 log.warning('user %s is disabled' % username)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
251 msg = M(self, 'disabled_account', state)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
252 raise formencode.Invalid(msg, value, state,
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
253 error_dict=dict(username=msg)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
254 )
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
255 else:
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
256 log.warning('user %s failed to authenticate' % username)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
257 msg = M(self, 'invalid_username', state)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
258 msg2 = M(self, 'invalid_password', state)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
259 raise formencode.Invalid(msg, value, state,
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
260 error_dict=dict(username=msg, password=msg2)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
261 )
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
262 return _validator
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
263
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
264
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
265 def ValidAuthToken():
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
266 class _validator(formencode.validators.FancyValidator):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
267 messages = {
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
268 'invalid_token': _(u'Token mismatch')
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
269 }
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
270
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
271 def validate_python(self, value, state):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
272 if value != authentication_token():
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
273 msg = M(self, 'invalid_token', state)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
274 raise formencode.Invalid(msg, value, state)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
275 return _validator
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
276
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
277
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
278 def ValidRepoName(edit=False, old_data={}):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
279 class _validator(formencode.validators.FancyValidator):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
280 messages = {
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
281 'invalid_repo_name':
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
282 _(u'Repository name %(repo)s is disallowed'),
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
283 'repository_exists':
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
284 _(u'Repository named %(repo)s already exists'),
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
285 'repository_in_group_exists': _(u'Repository "%(repo)s" already '
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
286 'exists in group "%(group)s"'),
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
287 'same_group_exists': _(u'Repositories group with name "%(repo)s" '
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
288 'already exists')
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
289 }
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
290
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
291 def _to_python(self, value, state):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
292 repo_name = repo_name_slug(value.get('repo_name', ''))
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
293 repo_group = value.get('repo_group')
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
294 if repo_group:
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
295 gr = RepoGroup.get(repo_group)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
296 group_path = gr.full_path
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
297 group_name = gr.group_name
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
298 # value needs to be aware of group name in order to check
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
299 # db key This is an actual just the name to store in the
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
300 # database
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
301 repo_name_full = group_path + RepoGroup.url_sep() + repo_name
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
302 else:
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
303 group_name = group_path = ''
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
304 repo_name_full = repo_name
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
305
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
306 value['repo_name'] = repo_name
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
307 value['repo_name_full'] = repo_name_full
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
308 value['group_path'] = group_path
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
309 value['group_name'] = group_name
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
310 return value
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
311
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
312 def validate_python(self, value, state):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
313
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
314 repo_name = value.get('repo_name')
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
315 repo_name_full = value.get('repo_name_full')
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
316 group_path = value.get('group_path')
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
317 group_name = value.get('group_name')
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
318
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
319 if repo_name in [ADMIN_PREFIX, '']:
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
320 msg = M(self, 'invalid_repo_name', state, repo=repo_name)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
321 raise formencode.Invalid(msg, value, state,
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
322 error_dict=dict(repo_name=msg)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
323 )
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
324
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
325 rename = old_data.get('repo_name') != repo_name_full
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
326 create = not edit
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
327 if rename or create:
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
328
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
329 if group_path != '':
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
330 if Repository.get_by_repo_name(repo_name_full):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
331 msg = M(self, 'repository_in_group_exists', state,
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
332 repo=repo_name, group=group_name)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
333 raise formencode.Invalid(msg, value, state,
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
334 error_dict=dict(repo_name=msg)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
335 )
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
336 elif RepoGroup.get_by_group_name(repo_name_full):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
337 msg = M(self, 'same_group_exists', state,
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
338 repo=repo_name)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
339 raise formencode.Invalid(msg, value, state,
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
340 error_dict=dict(repo_name=msg)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
341 )
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
342
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
343 elif Repository.get_by_repo_name(repo_name_full):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
344 msg = M(self, 'repository_exists', state,
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
345 repo=repo_name)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
346 raise formencode.Invalid(msg, value, state,
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
347 error_dict=dict(repo_name=msg)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
348 )
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
349 return value
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
350 return _validator
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
351
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
352
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
353 def ValidForkName(*args, **kwargs):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
354 return ValidRepoName(*args, **kwargs)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
355
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
356
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
357 def SlugifyName():
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
358 class _validator(formencode.validators.FancyValidator):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
359
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
360 def _to_python(self, value, state):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
361 return repo_name_slug(value)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
362
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
363 def validate_python(self, value, state):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
364 pass
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
365
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
366 return _validator
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
367
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
368
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
369 def ValidCloneUri():
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
370 from rhodecode.lib.utils import make_ui
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
371
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
372 def url_handler(repo_type, url, proto, ui=None):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
373 if repo_type == 'hg':
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
374 from mercurial.httprepo import httprepository, httpsrepository
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
375 if proto == 'https':
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
376 httpsrepository(make_ui('db'), url).capabilities
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
377 elif proto == 'http':
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
378 httprepository(make_ui('db'), url).capabilities
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
379 elif repo_type == 'git':
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
380 #TODO: write a git url validator
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
381 pass
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
382
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
383 class _validator(formencode.validators.FancyValidator):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
384 messages = {
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
385 'clone_uri': _(u'invalid clone url'),
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
386 'invalid_clone_uri': _(u'Invalid clone url, provide a '
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
387 'valid clone http\s url')
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
388 }
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
389
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
390 def validate_python(self, value, state):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
391 repo_type = value.get('repo_type')
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
392 url = value.get('clone_uri')
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
393
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
394 if not url:
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
395 pass
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
396 elif url.startswith('https') or url.startswith('http'):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
397 _type = 'https' if url.startswith('https') else 'http'
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
398 try:
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
399 url_handler(repo_type, url, _type, make_ui('db'))
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
400 except Exception:
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
401 log.exception('Url validation failed')
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
402 msg = M(self, 'clone_uri')
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
403 raise formencode.Invalid(msg, value, state,
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
404 error_dict=dict(clone_uri=msg)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
405 )
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
406 else:
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
407 msg = M(self, 'invalid_clone_uri', state)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
408 raise formencode.Invalid(msg, value, state,
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
409 error_dict=dict(clone_uri=msg)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
410 )
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
411 return _validator
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
412
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
413
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
414 def ValidForkType(old_data={}):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
415 class _validator(formencode.validators.FancyValidator):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
416 messages = {
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
417 'invalid_fork_type': _(u'Fork have to be the same type as parent')
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
418 }
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
419
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
420 def validate_python(self, value, state):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
421 if old_data['repo_type'] != value:
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
422 msg = M(self, 'invalid_fork_type', state)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
423 raise formencode.Invalid(msg, value, state,
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
424 error_dict=dict(repo_type=msg)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
425 )
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
426 return _validator
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
427
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
428
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
429 def ValidPerms(type_='repo'):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
430 if type_ == 'group':
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
431 EMPTY_PERM = 'group.none'
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
432 elif type_ == 'repo':
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
433 EMPTY_PERM = 'repository.none'
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
434
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
435 class _validator(formencode.validators.FancyValidator):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
436 messages = {
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
437 'perm_new_member_name':
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
438 _(u'This username or users group name is not valid')
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
439 }
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
440
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
441 def to_python(self, value, state):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
442 perms_update = []
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
443 perms_new = []
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
444 # build a list of permission to update and new permission to create
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
445 for k, v in value.items():
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
446 # means new added member to permissions
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
447 if k.startswith('perm_new_member'):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
448 new_perm = value.get('perm_new_member', False)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
449 new_member = value.get('perm_new_member_name', False)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
450 new_type = value.get('perm_new_member_type')
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
451
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
452 if new_member and new_perm:
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
453 if (new_member, new_perm, new_type) not in perms_new:
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
454 perms_new.append((new_member, new_perm, new_type))
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
455 elif k.startswith('u_perm_') or k.startswith('g_perm_'):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
456 member = k[7:]
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
457 t = {'u': 'user',
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
458 'g': 'users_group'
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
459 }[k[0]]
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
460 if member == 'default':
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
461 if value.get('private'):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
462 # set none for default when updating to
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
463 # private repo
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
464 v = EMPTY_PERM
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
465 perms_update.append((member, v, t))
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
466
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
467 value['perms_updates'] = perms_update
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
468 value['perms_new'] = perms_new
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
469
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
470 # update permissions
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
471 for k, v, t in perms_new:
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
472 try:
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
473 if t is 'user':
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
474 self.user_db = User.query()\
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
475 .filter(User.active == True)\
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
476 .filter(User.username == k).one()
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
477 if t is 'users_group':
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
478 self.user_db = UsersGroup.query()\
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
479 .filter(UsersGroup.users_group_active == True)\
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
480 .filter(UsersGroup.users_group_name == k).one()
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
481
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
482 except Exception:
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
483 log.exception('Updated permission failed')
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
484 msg = M(self, 'perm_new_member_type', state)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
485 raise formencode.Invalid(msg, value, state,
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
486 error_dict=dict(perm_new_member_name=msg)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
487 )
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
488 return value
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
489 return _validator
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
490
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
491
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
492 def ValidSettings():
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
493 class _validator(formencode.validators.FancyValidator):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
494 def _to_python(self, value, state):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
495 # settings form can't edit user
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
496 if 'user' in value:
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
497 del value['user']
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
498 return value
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
499
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
500 def validate_python(self, value, state):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
501 pass
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
502 return _validator
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
503
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
504
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
505 def ValidPath():
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
506 class _validator(formencode.validators.FancyValidator):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
507 messages = {
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
508 'invalid_path': _(u'This is not a valid path')
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
509 }
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
510
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
511 def validate_python(self, value, state):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
512 if not os.path.isdir(value):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
513 msg = M(self, 'invalid_path', state)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
514 raise formencode.Invalid(msg, value, state,
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
515 error_dict=dict(paths_root_path=msg)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
516 )
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
517 return _validator
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
518
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
519
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
520 def UniqSystemEmail(old_data={}):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
521 class _validator(formencode.validators.FancyValidator):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
522 messages = {
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
523 'email_taken': _(u'This e-mail address is already taken')
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
524 }
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
525
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
526 def _to_python(self, value, state):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
527 return value.lower()
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
528
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
529 def validate_python(self, value, state):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
530 if (old_data.get('email') or '').lower() != value:
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
531 user = User.get_by_email(value, case_insensitive=True)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
532 if user:
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
533 msg = M(self, 'email_taken', state)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
534 raise formencode.Invalid(msg, value, state,
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
535 error_dict=dict(email=msg)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
536 )
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
537 return _validator
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
538
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
539
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
540 def ValidSystemEmail():
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
541 class _validator(formencode.validators.FancyValidator):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
542 messages = {
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
543 'non_existing_email': _(u'e-mail "%(email)s" does not exist.')
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
544 }
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
545
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
546 def _to_python(self, value, state):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
547 return value.lower()
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
548
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
549 def validate_python(self, value, state):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
550 user = User.get_by_email(value, case_insensitive=True)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
551 if user is None:
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
552 msg = M(self, 'non_existing_email', state, email=value)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
553 raise formencode.Invalid(msg, value, state,
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
554 error_dict=dict(email=msg)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
555 )
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
556
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
557 return _validator
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
558
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
559
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
560 def LdapLibValidator():
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
561 class _validator(formencode.validators.FancyValidator):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
562 messages = {
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
563
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
564 }
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
565
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
566 def validate_python(self, value, state):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
567 try:
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
568 import ldap
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
569 ldap # pyflakes silence !
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
570 except ImportError:
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
571 raise LdapImportError()
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
572
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
573 return _validator
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
574
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
575
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
576 def AttrLoginValidator():
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
577 class _validator(formencode.validators.FancyValidator):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
578 messages = {
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
579 'invalid_cn':
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
580 _(u'The LDAP Login attribute of the CN must be specified - '
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
581 'this is the name of the attribute that is equivalent '
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
582 'to "username"')
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
583 }
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
584
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
585 def validate_python(self, value, state):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
586 if not value or not isinstance(value, (str, unicode)):
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
587 msg = M(self, 'invalid_cn', state)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
588 raise formencode.Invalid(msg, value, state,
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
589 error_dict=dict(ldap_attr_login=msg)
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
590 )
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
591
7010dc12f10c Added rewritten validators module + tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
592 return _validator