Mercurial > kallithea
comparison pylons_app/model/forms.py @ 296:29370bb76fa6
first permissions commit: added permission managment on repository edit. Changed db rmissions, validators.
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Fri, 25 Jun 2010 02:09:33 +0200 |
parents | 0e5455fda8fd |
children | d303aacb3349 |
comparison
equal
deleted
inserted
replaced
295:248642ed1912 | 296:29370bb76fa6 |
---|---|
53 raise formencode.Invalid(self.message('invalid_token', state, | 53 raise formencode.Invalid(self.message('invalid_token', state, |
54 search_number=value), value, state) | 54 search_number=value), value, state) |
55 class ValidUsername(formencode.validators.FancyValidator): | 55 class ValidUsername(formencode.validators.FancyValidator): |
56 | 56 |
57 def validate_python(self, value, state): | 57 def validate_python(self, value, state): |
58 pass | 58 if value in ['default', 'new_user']: |
59 raise formencode.Invalid(_('Invalid username'), value, state) | |
59 | 60 |
60 class ValidPassword(formencode.validators.FancyValidator): | 61 class ValidPassword(formencode.validators.FancyValidator): |
61 | 62 |
62 def to_python(self, value, state): | 63 def to_python(self, value, state): |
63 return get_crypt_password(value) | 64 return get_crypt_password(value) |
143 raise formencode.Invalid(_('This repository already exists'), | 144 raise formencode.Invalid(_('This repository already exists'), |
144 value, state) | 145 value, state) |
145 | 146 |
146 return slug | 147 return slug |
147 return _ValidRepoName | 148 return _ValidRepoName |
149 | |
150 class ValidPerms(formencode.validators.FancyValidator): | |
151 messages = {'perm_new_user_name':_('This username is not valid')} | |
152 | |
153 def to_python(self, value, state): | |
154 perms_update = [] | |
155 perms_new = [] | |
156 #build a list of permission to update and new permission to create | |
157 for k, v in value.items(): | |
158 print k, v | |
159 if k.startswith('perm_'): | |
160 if k.startswith('perm_new_user'): | |
161 new_perm = value.get('perm_new_user', False) | |
162 new_user = value.get('perm_new_user_name', False) | |
163 if new_user and new_perm: | |
164 if (new_user, new_perm) not in perms_new: | |
165 perms_new.append((new_user, new_perm)) | |
166 else: | |
167 perms_update.append((k[5:], v)) | |
168 #clear from form list | |
169 #del value[k] | |
170 value['perms_updates'] = perms_update | |
171 value['perms_new'] = perms_new | |
172 sa = meta.Session | |
173 for k, v in perms_new: | |
174 try: | |
175 self.user_db = sa.query(User).filter(User.username == k).one() | |
176 except Exception: | |
177 msg = self.message('perm_new_user_name', | |
178 state=State_obj) | |
179 raise formencode.Invalid(msg, value, state, error_dict={'perm_new_user_name':msg}) | |
180 return value | |
181 | |
148 #=============================================================================== | 182 #=============================================================================== |
149 # FORMS | 183 # FORMS |
150 #=============================================================================== | 184 #=============================================================================== |
151 class LoginForm(formencode.Schema): | 185 class LoginForm(formencode.Schema): |
152 allow_extra_fields = True | 186 allow_extra_fields = True |
190 return _UserForm | 224 return _UserForm |
191 | 225 |
192 def RepoForm(edit=False): | 226 def RepoForm(edit=False): |
193 class _RepoForm(formencode.Schema): | 227 class _RepoForm(formencode.Schema): |
194 allow_extra_fields = True | 228 allow_extra_fields = True |
195 filter_extra_fields = True | 229 filter_extra_fields = False |
196 repo_name = All(UnicodeString(strip=True, min=1, not_empty=True), ValidRepoName(edit)) | 230 repo_name = All(UnicodeString(strip=True, min=1, not_empty=True), ValidRepoName(edit)) |
197 description = UnicodeString(strip=True, min=3, not_empty=True) | 231 description = UnicodeString(strip=True, min=3, not_empty=True) |
198 private = StringBoolean(if_missing=False) | 232 private = StringBoolean(if_missing=False) |
199 | 233 |
200 if edit: | 234 if edit: |
201 user = All(Int(not_empty=True), ValidRepoUser) | 235 user = All(Int(not_empty=True), ValidRepoUser) |
202 | 236 |
237 chained_validators = [ValidPerms] | |
203 return _RepoForm | 238 return _RepoForm |