Files @ 967f5182a74e
Branch filter:

Location: kallithea/rhodecode/model/forms.py - annotation

Marcin Kuzminski
Added tag v1.1.2 for changeset 6c01c12eafb8
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
56c2850a5b5f
56c2850a5b5f
56c2850a5b5f
56c2850a5b5f
56c2850a5b5f
1e757ac98988
1e757ac98988
1e757ac98988
56c2850a5b5f
1e757ac98988
56c2850a5b5f
56c2850a5b5f
56c2850a5b5f
9e9f1b919c0c
1e757ac98988
7e536d1af60d
7e536d1af60d
7e536d1af60d
56c2850a5b5f
56c2850a5b5f
1e757ac98988
7e536d1af60d
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
7e536d1af60d
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
7e536d1af60d
7e536d1af60d
1e757ac98988
7e536d1af60d
1e757ac98988
1e757ac98988
1e757ac98988
7e536d1af60d
1e757ac98988
1e757ac98988
7e536d1af60d
7e536d1af60d
7e536d1af60d
1377a9d4bdb9
1377a9d4bdb9
1e757ac98988
1e757ac98988
c366b237c91d
c366b237c91d
c366b237c91d
c9fa3f53143b
c366b237c91d
c366b237c91d
c366b237c91d
c9fa3f53143b
c366b237c91d
c366b237c91d
c366b237c91d
7e536d1af60d
7e536d1af60d
1e757ac98988
7e536d1af60d
1e757ac98988
02bdf2f296ff
1e757ac98988
02bdf2f296ff
02bdf2f296ff
02bdf2f296ff
02bdf2f296ff
02bdf2f296ff
02bdf2f296ff
02bdf2f296ff
02bdf2f296ff
02bdf2f296ff
02bdf2f296ff
02bdf2f296ff
02bdf2f296ff
02bdf2f296ff
02bdf2f296ff
02bdf2f296ff
02bdf2f296ff
f1629c0c28cc
f1629c0c28cc
f1629c0c28cc
f1629c0c28cc
f1629c0c28cc
f1629c0c28cc
f1629c0c28cc
f1629c0c28cc
02bdf2f296ff
02bdf2f296ff
02bdf2f296ff
02bdf2f296ff
02bdf2f296ff
02bdf2f296ff
02bdf2f296ff
02bdf2f296ff
02bdf2f296ff
02bdf2f296ff
7e536d1af60d
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
9e9f1b919c0c
7e536d1af60d
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
7e536d1af60d
1e757ac98988
1e757ac98988
1e757ac98988
7e536d1af60d
52da7cba88a6
56c2850a5b5f
52da7cba88a6
52da7cba88a6
52da7cba88a6
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
52da7cba88a6
52da7cba88a6
52da7cba88a6
52da7cba88a6
52da7cba88a6
7e536d1af60d
1e757ac98988
7e536d1af60d
1e757ac98988
7e536d1af60d
1e757ac98988
7e536d1af60d
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
7e536d1af60d
1e757ac98988
1e757ac98988
7e536d1af60d
1e757ac98988
7e536d1af60d
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
7e536d1af60d
dbec976d9975
1e757ac98988
1e757ac98988
7e536d1af60d
7e536d1af60d
7e536d1af60d
1e757ac98988
1e757ac98988
758f64f3fbda
758f64f3fbda
758f64f3fbda
758f64f3fbda
758f64f3fbda
1377a9d4bdb9
1377a9d4bdb9
758f64f3fbda
758f64f3fbda
758f64f3fbda
1e757ac98988
1e757ac98988
7e536d1af60d
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
7e536d1af60d
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1377a9d4bdb9
1377a9d4bdb9
1e757ac98988
7e536d1af60d
1e757ac98988
7e536d1af60d
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
7e536d1af60d
1e757ac98988
7e536d1af60d
1e757ac98988
1e757ac98988
05528ad948c4
05528ad948c4
7e536d1af60d
05528ad948c4
7e536d1af60d
05528ad948c4
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
54684e071457
1e757ac98988
7e536d1af60d
1e757ac98988
1e757ac98988
1e757ac98988
18a3ca35d501
1e757ac98988
1e757ac98988
1e757ac98988
7e536d1af60d
1e757ac98988
7e536d1af60d
1e757ac98988
7e536d1af60d
1e757ac98988
1e757ac98988
54684e071457
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
18a3ca35d501
1e757ac98988
1e757ac98988
1e757ac98988
7e536d1af60d
7e536d1af60d
1e757ac98988
9e9f1b919c0c
9e9f1b919c0c
9e9f1b919c0c
9e9f1b919c0c
9e9f1b919c0c
9e9f1b919c0c
9e9f1b919c0c
9e9f1b919c0c
9e9f1b919c0c
9e9f1b919c0c
aaf2fc59a39a
aaf2fc59a39a
aaf2fc59a39a
aaf2fc59a39a
aaf2fc59a39a
aaf2fc59a39a
aaf2fc59a39a
aaf2fc59a39a
aaf2fc59a39a
aaf2fc59a39a
aaf2fc59a39a
aaf2fc59a39a
aaf2fc59a39a
aaf2fc59a39a
aaf2fc59a39a
aaf2fc59a39a
aaf2fc59a39a
aaf2fc59a39a
aaf2fc59a39a
aaf2fc59a39a
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
03676d39dd0a
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
7e536d1af60d
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1377a9d4bdb9
1377a9d4bdb9
1e757ac98988
02bdf2f296ff
1e757ac98988
1e757ac98988
02bdf2f296ff
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
7e536d1af60d
02bdf2f296ff
02bdf2f296ff
1e757ac98988
1e757ac98988
02bdf2f296ff
02bdf2f296ff
02bdf2f296ff
02bdf2f296ff
1377a9d4bdb9
1377a9d4bdb9
02bdf2f296ff
02bdf2f296ff
02bdf2f296ff
02bdf2f296ff
02bdf2f296ff
02bdf2f296ff
02bdf2f296ff
02bdf2f296ff
02bdf2f296ff
02bdf2f296ff
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
7e536d1af60d
1e757ac98988
1e757ac98988
e2f3c8e6939d
1e757ac98988
1e757ac98988
1e757ac98988
1377a9d4bdb9
1377a9d4bdb9
1e757ac98988
1e757ac98988
bd57d1cb9dc3
e2f3c8e6939d
1e757ac98988
1e757ac98988
7e536d1af60d
1e757ac98988
1e757ac98988
1e757ac98988
e2f3c8e6939d
1e757ac98988
1e757ac98988
1e757ac98988
1377a9d4bdb9
1377a9d4bdb9
1e757ac98988
1e757ac98988
e2f3c8e6939d
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1377a9d4bdb9
1377a9d4bdb9
1e757ac98988
1e757ac98988
7e536d1af60d
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
b75b77ef649d
b75b77ef649d
7e536d1af60d
1e757ac98988
7e536d1af60d
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
673de12e6bf6
673de12e6bf6
7e536d1af60d
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
9e9f1b919c0c
dd532af216d9
1e757ac98988
1e757ac98988
1e757ac98988
7e536d1af60d
1e757ac98988
9e9f1b919c0c
9e9f1b919c0c
9e9f1b919c0c
9e9f1b919c0c
9e9f1b919c0c
9e9f1b919c0c
9e9f1b919c0c
9e9f1b919c0c
9e9f1b919c0c
9e9f1b919c0c
9e9f1b919c0c
9e9f1b919c0c
9e9f1b919c0c
aaf2fc59a39a
9e9f1b919c0c
9e9f1b919c0c
""" this is forms validation classes
http://formencode.org/module-formencode.validators.html
for list off all availible validators

we can create our own validators

The table below outlines the options which can be used in a schema in addition to the validators themselves
pre_validators          []     These validators will be applied before the schema
chained_validators      []     These validators will be applied after the schema
allow_extra_fields      False     If True, then it is not an error when keys that aren't associated with a validator are present
filter_extra_fields     False     If True, then keys that aren't associated with a validator are removed
if_key_missing          NoDefault If this is given, then any keys that aren't available but are expected will be replaced with this value (and then validated). This does not override a present .if_missing attribute on validators. NoDefault is a special FormEncode class to mean that no default values has been specified and therefore missing keys shouldn't take a default value.
ignore_key_missing      False     If True, then missing keys will be missing in the result, if the validator doesn't have .if_missing on it already    
  
  
<name> = formencode.validators.<name of validator>
<name> must equal form name
list=[1,2,3,4,5]
for SELECT use formencode.All(OneOf(list), Int())
    
"""
import os
import re
import logging

import formencode
from formencode import All
from formencode.validators import UnicodeString, OneOf, Int, Number, Regex, \
    Email, Bool, StringBoolean

from pylons.i18n.translation import _

import rhodecode.lib.helpers as h
from rhodecode.lib.auth import authenticate, get_crypt_password
from rhodecode.lib.exceptions import LdapImportError
from rhodecode.model import meta
from rhodecode.model.user import UserModel
from rhodecode.model.repo import RepoModel
from rhodecode.model.db import User
from rhodecode import BACKENDS

from webhelpers.pylonslib.secure_form import authentication_token

log = logging.getLogger(__name__)

#this is needed to translate the messages using _() in validators
class State_obj(object):
    _ = staticmethod(_)

#===============================================================================
# VALIDATORS
#===============================================================================
class ValidAuthToken(formencode.validators.FancyValidator):
    messages = {'invalid_token':_('Token mismatch')}

    def validate_python(self, value, state):

        if value != authentication_token():
            raise formencode.Invalid(self.message('invalid_token', state,
                                            search_number=value), value, state)

def ValidUsername(edit, old_data):
    class _ValidUsername(formencode.validators.FancyValidator):

        def validate_python(self, value, state):
            if value in ['default', 'new_user']:
                raise formencode.Invalid(_('Invalid username'), value, state)
            #check if user is unique
            old_un = None
            if edit:
                old_un = UserModel().get(old_data.get('user_id')).username

            if old_un != value or not edit:
                if UserModel().get_by_username(value, cache=False,
                                               case_insensitive=True):
                    raise formencode.Invalid(_('This username already exists') ,
                                             value, state)


            if re.match(r'^[a-zA-Z0-9]{1}[a-zA-Z0-9\-\_]+$', value) is None:
                raise formencode.Invalid(_('Username may only contain '
                                           'alphanumeric characters underscores '
                                           'or dashes and must begin with '
                                           'alphanumeric character'),
                                      value, state)



    return _ValidUsername

class ValidPassword(formencode.validators.FancyValidator):

    def to_python(self, value, state):

        if value:

            if value.get('password'):
                try:
                    value['password'] = get_crypt_password(value['password'])
                except UnicodeEncodeError:
                    e_dict = {'password':_('Invalid characters in password')}
                    raise formencode.Invalid('', value, state, error_dict=e_dict)

            if value.get('password_confirmation'):
                try:
                    value['password_confirmation'] = \
                        get_crypt_password(value['password_confirmation'])
                except UnicodeEncodeError:
                    e_dict = {'password_confirmation':_('Invalid characters in password')}
                    raise formencode.Invalid('', value, state, error_dict=e_dict)

            if value.get('new_password'):
                try:
                    value['new_password'] = \
                        get_crypt_password(value['new_password'])
                except UnicodeEncodeError:
                    e_dict = {'new_password':_('Invalid characters in password')}
                    raise formencode.Invalid('', value, state, error_dict=e_dict)

            return value

class ValidPasswordsMatch(formencode.validators.FancyValidator):

    def validate_python(self, value, state):

        if value['password'] != value['password_confirmation']:
            e_dict = {'password_confirmation':
                   _('Password do not match')}
            raise formencode.Invalid('', value, state, error_dict=e_dict)

class ValidAuth(formencode.validators.FancyValidator):
    messages = {
            'invalid_password':_('invalid password'),
            'invalid_login':_('invalid user name'),
            'disabled_account':_('Your account is disabled')

            }
    #error mapping
    e_dict = {'username':messages['invalid_login'],
              'password':messages['invalid_password']}
    e_dict_disable = {'username':messages['disabled_account']}

    def validate_python(self, value, state):
        password = value['password']
        username = value['username']
        user = UserModel().get_by_username(username)

        if authenticate(username, password):
            return value
        else:
            if user and user.active is False:
                log.warning('user %s is disabled', username)
                raise formencode.Invalid(self.message('disabled_account',
                                         state=State_obj),
                                         value, state,
                                         error_dict=self.e_dict_disable)
            else:
                log.warning('user %s not authenticated', username)
                raise formencode.Invalid(self.message('invalid_password',
                                         state=State_obj), value, state,
                                         error_dict=self.e_dict)

class ValidRepoUser(formencode.validators.FancyValidator):

    def to_python(self, value, state):
        sa = meta.Session()
        try:
            self.user_db = sa.query(User)\
                .filter(User.active == True)\
                .filter(User.username == value).one()
        except Exception:
            raise formencode.Invalid(_('This username is not valid'),
                                     value, state)
        finally:
            meta.Session.remove()

        return self.user_db.user_id

def ValidRepoName(edit, old_data):
    class _ValidRepoName(formencode.validators.FancyValidator):

        def to_python(self, value, state):
            slug = h.repo_name_slug(value)
            if slug in ['_admin']:
                raise formencode.Invalid(_('This repository name is disallowed'),
                                         value, state)
            if old_data.get('repo_name') != value or not edit:
                if RepoModel().get_by_repo_name(slug, cache=False):
                    raise formencode.Invalid(_('This repository already exists') ,
                                             value, state)
            return slug


    return _ValidRepoName

def ValidForkType(old_data):
    class _ValidForkType(formencode.validators.FancyValidator):

        def to_python(self, value, state):
            if old_data['repo_type'] != value:
                raise formencode.Invalid(_('Fork have to be the same type as original'),
                                         value, state)
            return value
    return _ValidForkType

class ValidPerms(formencode.validators.FancyValidator):
    messages = {'perm_new_user_name':_('This username is not valid')}

    def to_python(self, value, state):
        perms_update = []
        perms_new = []
        #build a list of permission to update and new permission to create
        for k, v in value.items():
            if k.startswith('perm_'):
                if  k.startswith('perm_new_user'):
                    new_perm = value.get('perm_new_user', False)
                    new_user = value.get('perm_new_user_name', False)
                    if new_user and new_perm:
                        if (new_user, new_perm) not in perms_new:
                            perms_new.append((new_user, new_perm))
                else:
                    usr = k[5:]
                    if usr == 'default':
                        if value['private']:
                            #set none for default when updating to private repo
                            v = 'repository.none'
                    perms_update.append((usr, v))
        value['perms_updates'] = perms_update
        value['perms_new'] = perms_new
        sa = meta.Session
        for k, v in perms_new:
            try:
                self.user_db = sa.query(User)\
                    .filter(User.active == True)\
                    .filter(User.username == k).one()
            except Exception:
                msg = self.message('perm_new_user_name',
                                     state=State_obj)
                raise formencode.Invalid(msg, value, state,
                                         error_dict={'perm_new_user_name':msg})
        return value

class ValidSettings(formencode.validators.FancyValidator):

    def to_python(self, value, state):
        #settings  form can't edit user
        if value.has_key('user'):
            del['value']['user']

        return value

class ValidPath(formencode.validators.FancyValidator):
    def to_python(self, value, state):

        if not os.path.isdir(value):
            msg = _('This is not a valid path')
            raise formencode.Invalid(msg, value, state,
                                     error_dict={'paths_root_path':msg})
        return value

def UniqSystemEmail(old_data):
    class _UniqSystemEmail(formencode.validators.FancyValidator):
        def to_python(self, value, state):
            value = value.lower()
            if old_data.get('email') != value:
                sa = meta.Session()
                try:
                    user = sa.query(User).filter(User.email == value).scalar()
                    if user:
                        raise formencode.Invalid(_("This e-mail address is already taken") ,
                                                 value, state)
                finally:
                    meta.Session.remove()

            return value

    return _UniqSystemEmail

class ValidSystemEmail(formencode.validators.FancyValidator):
    def to_python(self, value, state):
        value = value.lower()
        sa = meta.Session
        try:
            user = sa.query(User).filter(User.email == value).scalar()
            if  user is None:
                raise formencode.Invalid(_("This e-mail address doesn't exist.") ,
                                         value, state)
        finally:
            meta.Session.remove()

        return value

class LdapLibValidator(formencode.validators.FancyValidator):

    def to_python(self, value, state):

        try:
            import ldap
        except ImportError:
            raise LdapImportError
        return value

class BaseDnValidator(formencode.validators.FancyValidator):

    def to_python(self, value, state):

        try:
            value % {'user':'valid'}

            if value.find('%(user)s') == -1:
                raise formencode.Invalid(_("You need to specify %(user)s in "
                                           "template for example uid=%(user)s "
                                           ",dc=company...") ,
                                         value, state)

        except KeyError:
            raise formencode.Invalid(_("Wrong template used, only %(user)s "
                                       "is an valid entry") ,
                                         value, state)

        return value

#===============================================================================
# FORMS        
#===============================================================================
class LoginForm(formencode.Schema):
    allow_extra_fields = True
    filter_extra_fields = True
    username = UnicodeString(
                             strip=True,
                             min=1,
                             not_empty=True,
                             messages={
                                       'empty':_('Please enter a login'),
                                       'tooShort':_('Enter a value %(min)i characters long or more')}
                            )

    password = UnicodeString(
                            strip=True,
                            min=6,
                            not_empty=True,
                            messages={
                                      'empty':_('Please enter a password'),
                                      'tooShort':_('Enter %(min)i characters or more')}
                                )


    #chained validators have access to all data
    chained_validators = [ValidAuth]

def UserForm(edit=False, old_data={}):
    class _UserForm(formencode.Schema):
        allow_extra_fields = True
        filter_extra_fields = True
        username = All(UnicodeString(strip=True, min=1, not_empty=True),
                       ValidUsername(edit, old_data))
        if edit:
            new_password = All(UnicodeString(strip=True, min=6, not_empty=False))
            admin = StringBoolean(if_missing=False)
        else:
            password = All(UnicodeString(strip=True, min=6, not_empty=True))
        active = StringBoolean(if_missing=False)
        name = UnicodeString(strip=True, min=1, not_empty=True)
        lastname = UnicodeString(strip=True, min=1, not_empty=True)
        email = All(Email(not_empty=True), UniqSystemEmail(old_data))

        chained_validators = [ValidPassword]

    return _UserForm

def RegisterForm(edit=False, old_data={}):
    class _RegisterForm(formencode.Schema):
        allow_extra_fields = True
        filter_extra_fields = True
        username = All(ValidUsername(edit, old_data),
                       UnicodeString(strip=True, min=1, not_empty=True))
        password = All(UnicodeString(strip=True, min=6, not_empty=True))
        password_confirmation = All(UnicodeString(strip=True, min=6, not_empty=True))
        active = StringBoolean(if_missing=False)
        name = UnicodeString(strip=True, min=1, not_empty=True)
        lastname = UnicodeString(strip=True, min=1, not_empty=True)
        email = All(Email(not_empty=True), UniqSystemEmail(old_data))

        chained_validators = [ValidPasswordsMatch, ValidPassword]

    return _RegisterForm

def PasswordResetForm():
    class _PasswordResetForm(formencode.Schema):
        allow_extra_fields = True
        filter_extra_fields = True
        email = All(ValidSystemEmail(), Email(not_empty=True))
    return _PasswordResetForm

def RepoForm(edit=False, old_data={}, supported_backends=BACKENDS.keys()):
    class _RepoForm(formencode.Schema):
        allow_extra_fields = True
        filter_extra_fields = False
        repo_name = All(UnicodeString(strip=True, min=1, not_empty=True),
                        ValidRepoName(edit, old_data))
        description = UnicodeString(strip=True, min=1, not_empty=True)
        private = StringBoolean(if_missing=False)
        enable_statistics = StringBoolean(if_missing=False)
        repo_type = OneOf(supported_backends)
        if edit:
            user = All(Int(not_empty=True), ValidRepoUser)

        chained_validators = [ValidPerms]
    return _RepoForm

def RepoForkForm(edit=False, old_data={}, supported_backends=BACKENDS.keys()):
    class _RepoForkForm(formencode.Schema):
        allow_extra_fields = True
        filter_extra_fields = False
        fork_name = All(UnicodeString(strip=True, min=1, not_empty=True),
                        ValidRepoName(edit, old_data))
        description = UnicodeString(strip=True, min=1, not_empty=True)
        private = StringBoolean(if_missing=False)
        repo_type = All(ValidForkType(old_data), OneOf(supported_backends))
    return _RepoForkForm

def RepoSettingsForm(edit=False, old_data={}):
    class _RepoForm(formencode.Schema):
        allow_extra_fields = True
        filter_extra_fields = False
        repo_name = All(UnicodeString(strip=True, min=1, not_empty=True),
                        ValidRepoName(edit, old_data))
        description = UnicodeString(strip=True, min=1, not_empty=True)
        private = StringBoolean(if_missing=False)

        chained_validators = [ValidPerms, ValidSettings]
    return _RepoForm


def ApplicationSettingsForm():
    class _ApplicationSettingsForm(formencode.Schema):
        allow_extra_fields = True
        filter_extra_fields = False
        rhodecode_title = UnicodeString(strip=True, min=1, not_empty=True)
        rhodecode_realm = UnicodeString(strip=True, min=1, not_empty=True)

    return _ApplicationSettingsForm

def ApplicationUiSettingsForm():
    class _ApplicationUiSettingsForm(formencode.Schema):
        allow_extra_fields = True
        filter_extra_fields = False
        web_push_ssl = OneOf(['true', 'false'], if_missing='false')
        paths_root_path = All(ValidPath(), UnicodeString(strip=True, min=1, not_empty=True))
        hooks_changegroup_update = OneOf(['True', 'False'], if_missing=False)
        hooks_changegroup_repo_size = OneOf(['True', 'False'], if_missing=False)
        hooks_pretxnchangegroup_push_logger = OneOf(['True', 'False'], if_missing=False)
        hooks_preoutgoing_pull_logger = OneOf(['True', 'False'], if_missing=False)

    return _ApplicationUiSettingsForm

def DefaultPermissionsForm(perms_choices, register_choices, create_choices):
    class _DefaultPermissionsForm(formencode.Schema):
        allow_extra_fields = True
        filter_extra_fields = True
        overwrite_default = StringBoolean(if_missing=False)
        anonymous = OneOf(['True', 'False'], if_missing=False)
        default_perm = OneOf(perms_choices)
        default_register = OneOf(register_choices)
        default_create = OneOf(create_choices)

    return _DefaultPermissionsForm


def LdapSettingsForm():
    class _LdapSettingsForm(formencode.Schema):
        allow_extra_fields = True
        filter_extra_fields = True
        pre_validators = [LdapLibValidator]
        ldap_active = StringBoolean(if_missing=False)
        ldap_host = UnicodeString(strip=True,)
        ldap_port = Number(strip=True,)
        ldap_ldaps = StringBoolean(if_missing=False)
        ldap_dn_user = UnicodeString(strip=True,)
        ldap_dn_pass = UnicodeString(strip=True,)
        ldap_base_dn = All(BaseDnValidator, UnicodeString(strip=True,))

    return _LdapSettingsForm