forms.py
364 lines
| 14.2 KiB
| text/x-python
|
PythonLexer
r547 | """ 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. | ||||
r1203 | 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 | |||
r547 | <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()) | ||||
r1203 | ||||
r547 | """ | |||
r761 | import logging | |||
import formencode | ||||
r547 | from formencode import All | |||
r761 | ||||
r547 | from pylons.i18n.translation import _ | |||
r761 | ||||
r2467 | from rhodecode.model import validators as v | |||
r761 | from rhodecode import BACKENDS | |||
r547 | log = logging.getLogger(__name__) | |||
r1898 | ||||
r547 | class LoginForm(formencode.Schema): | |||
allow_extra_fields = True | ||||
filter_extra_fields = True | ||||
r2467 | username = v.UnicodeString( | |||
r1950 | strip=True, | |||
min=1, | ||||
not_empty=True, | ||||
messages={ | ||||
r2467 | 'empty': _(u'Please enter a login'), | |||
'tooShort': _(u'Enter a value %(min)i characters long or more')} | ||||
r1950 | ) | |||
r547 | ||||
r2467 | password = v.UnicodeString( | |||
r2181 | strip=False, | |||
r1950 | min=3, | |||
not_empty=True, | ||||
messages={ | ||||
r2467 | 'empty': _(u'Please enter a password'), | |||
'tooShort': _(u'Enter %(min)i characters or more')} | ||||
r1950 | ) | |||
r547 | ||||
r2467 | remember = v.StringBoolean(if_missing=False) | |||
r1818 | ||||
r2467 | chained_validators = [v.ValidAuth()] | |||
r629 | ||||
r1898 | ||||
r547 | def UserForm(edit=False, old_data={}): | |||
class _UserForm(formencode.Schema): | ||||
allow_extra_fields = True | ||||
filter_extra_fields = True | ||||
r2467 | username = All(v.UnicodeString(strip=True, min=1, not_empty=True), | |||
v.ValidUsername(edit, old_data)) | ||||
r547 | if edit: | |||
r2467 | new_password = All( | |||
r2544 | v.ValidPassword(), | |||
r2467 | v.UnicodeString(strip=False, min=6, not_empty=False) | |||
) | ||||
password_confirmation = All( | ||||
v.ValidPassword(), | ||||
v.UnicodeString(strip=False, min=6, not_empty=False), | ||||
) | ||||
admin = v.StringBoolean(if_missing=False) | ||||
r547 | else: | |||
r2467 | password = All( | |||
v.ValidPassword(), | ||||
v.UnicodeString(strip=False, min=6, not_empty=True) | ||||
) | ||||
password_confirmation = All( | ||||
v.ValidPassword(), | ||||
v.UnicodeString(strip=False, min=6, not_empty=False) | ||||
) | ||||
r1722 | ||||
r2467 | active = v.StringBoolean(if_missing=False) | |||
r2544 | firstname = v.UnicodeString(strip=True, min=1, not_empty=False) | |||
r2467 | lastname = v.UnicodeString(strip=True, min=1, not_empty=False) | |||
email = All(v.Email(not_empty=True), v.UniqSystemEmail(old_data)) | ||||
r629 | ||||
r2467 | chained_validators = [v.ValidPasswordsMatch()] | |||
r722 | ||||
r547 | return _UserForm | |||
r959 | ||||
r972 | def UsersGroupForm(edit=False, old_data={}, available_members=[]): | |||
r959 | class _UsersGroupForm(formencode.Schema): | |||
allow_extra_fields = True | ||||
filter_extra_fields = True | ||||
r2467 | users_group_name = All( | |||
v.UnicodeString(strip=True, min=1, not_empty=True), | ||||
v.ValidUsersGroup(edit, old_data) | ||||
) | ||||
r959 | ||||
r2467 | users_group_active = v.StringBoolean(if_missing=False) | |||
r959 | ||||
r972 | if edit: | |||
r2467 | users_group_members = v.OneOf( | |||
available_members, hideList=False, testValueList=True, | ||||
if_missing=None, not_empty=False | ||||
) | ||||
r972 | ||||
r959 | return _UsersGroupForm | |||
r1898 | ||||
r1345 | def ReposGroupForm(edit=False, old_data={}, available_groups=[]): | |||
class _ReposGroupForm(formencode.Schema): | ||||
allow_extra_fields = True | ||||
r1982 | filter_extra_fields = False | |||
r1345 | ||||
r2467 | group_name = All(v.UnicodeString(strip=True, min=1, not_empty=True), | |||
v.SlugifyName()) | ||||
group_description = v.UnicodeString(strip=True, min=1, | ||||
r1345 | not_empty=True) | |||
r2467 | group_parent_id = v.OneOf(available_groups, hideList=False, | |||
r1345 | testValueList=True, | |||
if_missing=None, not_empty=False) | ||||
r2749 | enable_locking = v.StringBoolean(if_missing=False) | |||
r2820 | recursive = v.StringBoolean(if_missing=False) | |||
r2467 | chained_validators = [v.ValidReposGroup(edit, old_data), | |||
v.ValidPerms('group')] | ||||
r1345 | ||||
return _ReposGroupForm | ||||
r1898 | ||||
r722 | def RegisterForm(edit=False, old_data={}): | |||
class _RegisterForm(formencode.Schema): | ||||
allow_extra_fields = True | ||||
filter_extra_fields = True | ||||
r2467 | username = All( | |||
v.ValidUsername(edit, old_data), | ||||
v.UnicodeString(strip=True, min=1, not_empty=True) | ||||
) | ||||
password = All( | ||||
v.ValidPassword(), | ||||
v.UnicodeString(strip=False, min=6, not_empty=True) | ||||
) | ||||
password_confirmation = All( | ||||
v.ValidPassword(), | ||||
v.UnicodeString(strip=False, min=6, not_empty=True) | ||||
) | ||||
active = v.StringBoolean(if_missing=False) | ||||
r2595 | firstname = v.UnicodeString(strip=True, min=1, not_empty=False) | |||
r2467 | lastname = v.UnicodeString(strip=True, min=1, not_empty=False) | |||
email = All(v.Email(not_empty=True), v.UniqSystemEmail(old_data)) | ||||
r722 | ||||
r2467 | chained_validators = [v.ValidPasswordsMatch()] | |||
r722 | ||||
return _RegisterForm | ||||
r547 | ||||
r1950 | ||||
r547 | def PasswordResetForm(): | |||
class _PasswordResetForm(formencode.Schema): | ||||
allow_extra_fields = True | ||||
filter_extra_fields = True | ||||
r2467 | email = All(v.ValidSystemEmail(), v.Email(not_empty=True)) | |||
r547 | return _PasswordResetForm | |||
r1950 | ||||
r1112 | def RepoForm(edit=False, old_data={}, supported_backends=BACKENDS.keys(), | |||
r2460 | repo_groups=[], landing_revs=[]): | |||
r547 | class _RepoForm(formencode.Schema): | |||
allow_extra_fields = True | ||||
r3056 | filter_extra_fields = True | |||
r2467 | repo_name = All(v.UnicodeString(strip=True, min=1, not_empty=True), | |||
v.SlugifyName()) | ||||
r2835 | repo_group = All(v.CanWriteGroup(), | |||
v.OneOf(repo_groups, hideList=True)) | ||||
r2467 | repo_type = v.OneOf(supported_backends) | |||
r3056 | repo_description = v.UnicodeString(strip=True, min=1, not_empty=False) | |||
repo_private = v.StringBoolean(if_missing=False) | ||||
repo_enable_statistics = v.StringBoolean(if_missing=False) | ||||
repo_enable_downloads = v.StringBoolean(if_missing=False) | ||||
repo_enable_locking = v.StringBoolean(if_missing=False) | ||||
repo_landing_rev = v.OneOf(landing_revs, hideList=True) | ||||
clone_uri = All(v.UnicodeString(strip=True, min=1, not_empty=False)) | ||||
r1112 | ||||
r547 | if edit: | |||
r1014 | #this is repo owner | |||
r2467 | user = All(v.UnicodeString(not_empty=True), v.ValidRepoUser()) | |||
r629 | ||||
r2467 | chained_validators = [v.ValidCloneUri(), | |||
v.ValidRepoName(edit, old_data), | ||||
v.ValidPerms()] | ||||
r547 | return _RepoForm | |||
r1950 | ||||
r2460 | def RepoForkForm(edit=False, old_data={}, supported_backends=BACKENDS.keys(), | |||
r2485 | repo_groups=[], landing_revs=[]): | |||
r547 | class _RepoForkForm(formencode.Schema): | |||
allow_extra_fields = True | ||||
filter_extra_fields = False | ||||
r2467 | repo_name = All(v.UnicodeString(strip=True, min=1, not_empty=True), | |||
v.SlugifyName()) | ||||
r2835 | repo_group = All(v.CanWriteGroup(), | |||
v.OneOf(repo_groups, hideList=True)) | ||||
r2467 | repo_type = All(v.ValidForkType(old_data), v.OneOf(supported_backends)) | |||
description = v.UnicodeString(strip=True, min=1, not_empty=True) | ||||
private = v.StringBoolean(if_missing=False) | ||||
copy_permissions = v.StringBoolean(if_missing=False) | ||||
update_after_clone = v.StringBoolean(if_missing=False) | ||||
fork_parent_id = v.UnicodeString() | ||||
chained_validators = [v.ValidForkName(edit, old_data)] | ||||
r2485 | landing_rev = v.OneOf(landing_revs, hideList=True) | |||
r1366 | ||||
r547 | return _RepoForkForm | |||
r1950 | ||||
r3056 | def RepoSettingsForm(edit=False, old_data={}, supported_backends=BACKENDS.keys(), | |||
repo_groups=[], landing_revs=[]): | ||||
r547 | class _RepoForm(formencode.Schema): | |||
allow_extra_fields = True | ||||
filter_extra_fields = False | ||||
r2467 | repo_name = All(v.UnicodeString(strip=True, min=1, not_empty=True), | |||
v.SlugifyName()) | ||||
description = v.UnicodeString(strip=True, min=1, not_empty=True) | ||||
r2986 | repo_group = All(v.CanWriteGroup(), | |||
v.OneOf(repo_groups, hideList=True)) | ||||
r2467 | private = v.StringBoolean(if_missing=False) | |||
landing_rev = v.OneOf(landing_revs, hideList=True) | ||||
chained_validators = [v.ValidRepoName(edit, old_data), v.ValidPerms(), | ||||
v.ValidSettings()] | ||||
r547 | return _RepoForm | |||
def ApplicationSettingsForm(): | ||||
class _ApplicationSettingsForm(formencode.Schema): | ||||
allow_extra_fields = True | ||||
filter_extra_fields = False | ||||
r2467 | rhodecode_title = v.UnicodeString(strip=True, min=1, not_empty=True) | |||
rhodecode_realm = v.UnicodeString(strip=True, min=1, not_empty=True) | ||||
rhodecode_ga_code = v.UnicodeString(strip=True, min=1, not_empty=False) | ||||
r629 | ||||
r547 | return _ApplicationSettingsForm | |||
r629 | ||||
r1950 | ||||
r2674 | def ApplicationVisualisationForm(): | |||
class _ApplicationVisualisationForm(formencode.Schema): | ||||
allow_extra_fields = True | ||||
filter_extra_fields = False | ||||
rhodecode_show_public_icon = v.StringBoolean(if_missing=False) | ||||
rhodecode_show_private_icon = v.StringBoolean(if_missing=False) | ||||
rhodecode_stylify_metatags = v.StringBoolean(if_missing=False) | ||||
r2936 | rhodecode_lightweight_dashboard = v.StringBoolean(if_missing=False) | |||
r2952 | rhodecode_lightweight_journal = v.StringBoolean(if_missing=False) | |||
r2936 | ||||
r2674 | return _ApplicationVisualisationForm | |||
r547 | def ApplicationUiSettingsForm(): | |||
class _ApplicationUiSettingsForm(formencode.Schema): | ||||
allow_extra_fields = True | ||||
filter_extra_fields = False | ||||
r2674 | web_push_ssl = v.StringBoolean(if_missing=False) | |||
r2467 | paths_root_path = All( | |||
v.ValidPath(), | ||||
v.UnicodeString(strip=True, min=1, not_empty=True) | ||||
) | ||||
r2674 | hooks_changegroup_update = v.StringBoolean(if_missing=False) | |||
hooks_changegroup_repo_size = v.StringBoolean(if_missing=False) | ||||
hooks_changegroup_push_logger = v.StringBoolean(if_missing=False) | ||||
r2726 | hooks_outgoing_pull_logger = v.StringBoolean(if_missing=False) | |||
r629 | ||||
r2708 | extensions_largefiles = v.StringBoolean(if_missing=False) | |||
extensions_hgsubversion = v.StringBoolean(if_missing=False) | ||||
extensions_hggit = v.StringBoolean(if_missing=False) | ||||
r547 | return _ApplicationUiSettingsForm | |||
r1950 | ||||
r3056 | def DefaultPermissionsForm(repo_perms_choices, group_perms_choices, | |||
register_choices, create_choices, fork_choices): | ||||
r547 | class _DefaultPermissionsForm(formencode.Schema): | |||
allow_extra_fields = True | ||||
filter_extra_fields = True | ||||
r3052 | overwrite_default_repo = v.StringBoolean(if_missing=False) | |||
overwrite_default_group = v.StringBoolean(if_missing=False) | ||||
r2674 | anonymous = v.StringBoolean(if_missing=False) | |||
r3052 | default_repo_perm = v.OneOf(repo_perms_choices) | |||
default_group_perm = v.OneOf(group_perms_choices) | ||||
r2467 | default_register = v.OneOf(register_choices) | |||
default_create = v.OneOf(create_choices) | ||||
r2709 | default_fork = v.OneOf(fork_choices) | |||
r629 | ||||
r547 | return _DefaultPermissionsForm | |||
r705 | ||||
r3056 | def DefaultsForm(edit=False, old_data={}, supported_backends=BACKENDS.keys()): | |||
class _DefaultsForm(formencode.Schema): | ||||
allow_extra_fields = True | ||||
filter_extra_fields = True | ||||
default_repo_type = v.OneOf(supported_backends) | ||||
default_repo_private = v.StringBoolean(if_missing=False) | ||||
default_repo_enable_statistics = v.StringBoolean(if_missing=False) | ||||
default_repo_enable_downloads = v.StringBoolean(if_missing=False) | ||||
default_repo_enable_locking = v.StringBoolean(if_missing=False) | ||||
return _DefaultsForm | ||||
r2467 | def LdapSettingsForm(tls_reqcert_choices, search_scope_choices, | |||
tls_kind_choices): | ||||
r705 | class _LdapSettingsForm(formencode.Schema): | |||
allow_extra_fields = True | ||||
filter_extra_fields = True | ||||
r2193 | #pre_validators = [LdapLibValidator] | |||
r2467 | ldap_active = v.StringBoolean(if_missing=False) | |||
ldap_host = v.UnicodeString(strip=True,) | ||||
ldap_port = v.Number(strip=True,) | ||||
ldap_tls_kind = v.OneOf(tls_kind_choices) | ||||
ldap_tls_reqcert = v.OneOf(tls_reqcert_choices) | ||||
ldap_dn_user = v.UnicodeString(strip=True,) | ||||
ldap_dn_pass = v.UnicodeString(strip=True,) | ||||
ldap_base_dn = v.UnicodeString(strip=True,) | ||||
ldap_filter = v.UnicodeString(strip=True,) | ||||
ldap_search_scope = v.OneOf(search_scope_choices) | ||||
ldap_attr_login = All( | ||||
v.AttrLoginValidator(), | ||||
v.UnicodeString(strip=True,) | ||||
) | ||||
ldap_attr_firstname = v.UnicodeString(strip=True,) | ||||
ldap_attr_lastname = v.UnicodeString(strip=True,) | ||||
ldap_attr_email = v.UnicodeString(strip=True,) | ||||
r705 | ||||
return _LdapSettingsForm | ||||
r2479 | ||||
def UserExtraEmailForm(): | ||||
class _UserExtraEmailForm(formencode.Schema): | ||||
email = All(v.UniqSystemEmail(), v.Email) | ||||
r2480 | return _UserExtraEmailForm | |||
r2711 | ||||
r2893 | def PullRequestForm(repo_id): | |||
r2711 | class _PullRequestForm(formencode.Schema): | |||
allow_extra_fields = True | ||||
filter_extra_fields = True | ||||
user = v.UnicodeString(strip=True, required=True) | ||||
org_repo = v.UnicodeString(strip=True, required=True) | ||||
org_ref = v.UnicodeString(strip=True, required=True) | ||||
other_repo = v.UnicodeString(strip=True, required=True) | ||||
other_ref = v.UnicodeString(strip=True, required=True) | ||||
r2893 | revisions = All(v.NotReviewedRevisions(repo_id)(), v.UniqueList(not_empty=True)) | |||
r2719 | review_members = v.UniqueList(not_empty=True) | |||
r2711 | ||||
pullrequest_title = v.UnicodeString(strip=True, required=True, min=3) | ||||
pullrequest_desc = v.UnicodeString(strip=True, required=False) | ||||
r2815 | return _PullRequestForm | |||