Parent: [36f02a] (diff)

Child: [5f54cc] (diff)

Download this file

forms.py    155 lines (135 with data), 6.2 kB

  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
import logging
import ming.orm.ormsession
from allura.lib import helpers as h
from allura.lib import exceptions as forge_exc
from allura import model as M
from formencode import validators as fev
import formencode
import ew as ew_core
import ew.jinja2_ew as ew
log = logging.getLogger(__name__)
class ForgeForm(ew.SimpleForm):
template='jinja:allura:templates/widgets/forge_form.html'
defaults=dict(
ew.SimpleForm.defaults,
submit_text='Save',
style='standard',
method='post',
enctype=None)
def display_field_by_idx(self, idx, ignore_errors=False):
field = self.fields[idx]
ctx = self.context_for(field)
display = field.display(**ctx)
if ctx['errors'] and field.show_errors and not ignore_errors:
display = "%s<div class='error'>%s</div>" % (display, ctx['errors'])
return display
class PasswordChangeForm(ForgeForm):
class fields(ew_core.NameList):
oldpw = ew.PasswordField(
label='Old Password', validator=fev.UnicodeString(not_empty=True))
pw = ew.PasswordField(
label='New Password',
validator=fev.UnicodeString(not_empty=True, min=6))
pw2 = ew.PasswordField(
label='New Password (again)',
validator=fev.UnicodeString(not_empty=True))
@ew_core.core.validator
def to_python(self, value, state):
d = super(PasswordChangeForm, self).to_python(value, state)
if d['pw'] != d['pw2']:
raise formencode.Invalid('Passwords must match', value, state)
return d
class UploadKeyForm(ForgeForm):
class fields(ew_core.NameList):
key = ew.TextArea(label='SSH Public Key')
class RegistrationForm(ForgeForm):
class fields(ew_core.NameList):
display_name = ew.TextField(
label='Displayed Name',
validator=fev.UnicodeString(not_empty=True))
username = ew.TextField(
label='Desired Username',
validator=fev.Regex(
h.re_path_portion))
username.validator._messages['invalid'] = (
'Usernames must include only letters, numbers, and dashes.'
' They must also start with a letter and be at least 3 characters'
' long.')
pw = ew.PasswordField(
label='New Password',
validator=fev.UnicodeString(not_empty=True, min=8))
pw2 = ew.PasswordField(
label='New Password (again)',
validator=fev.UnicodeString(not_empty=True))
@ew_core.core.validator
def to_python(self, value, state):
d = super(RegistrationForm, self).to_python(value, state)
value['username'] = username = value['username'].lower()
if M.User.by_username(username):
raise formencode.Invalid('That username is already taken. Please choose another.',
value, state)
if d['pw'] != d['pw2']:
raise formencode.Invalid('Passwords must match', value, state)
return d
class AdminForm(ForgeForm):
template='jinja:allura:templates/widgets/admin_form.html'
class NeighborhoodAddProjectForm(ForgeForm):
template='jinja:allura:templates/widgets/neighborhood_add_project.html'
defaults=dict(
ForgeForm.defaults,
method='post',
submit_text='Start')
@property
def fields(self):
fields = [
ew.InputField(name='project_unixname', label='', field_type='text',
validator=formencode.All(
fev.String(not_empty=True),
fev.Regex(r'^[A-z][-A-z0-9]{2,}$', messages={'invalid':'Please use only letters, numbers, and dash characters.'}),
NeighborhoodAddProjectValidator())),
ew.HiddenField(name='project_name', label='Project Name'),
ew.HiddenField(name='project_description', label='Public Description'),
ew.HiddenField(name='neighborhood', label='Neighborhood'),
ew.Checkbox(name="Wiki", label="", attrs={'class':'unlabeled'}),
ew.Checkbox(name="Git", label="", attrs={'class':'labeled'}),
ew.Checkbox(name="Hg", label="", attrs={'class':'labeled'}),
ew.Checkbox(name="SVN", label="", attrs={'class':'labeled'}),
ew.Checkbox(name="Tickets", label="", attrs={'class':'unlabeled'}),
ew.Checkbox(name="Downloads", label="", attrs={'class':'unlabeled'}),
# ew.Checkbox(name="Stats", label="", attrs={'class':'unlabeled'}),
ew.Checkbox(name="Discussion", label="", attrs={'class':'unlabeled'})
]
return fields
def resources(self):
for r in super(NeighborhoodAddProjectForm, self).resources(): yield r
yield ew.CSSLink('css/add_project.css')
yield ew.JSScript('''
$(function(){
var $scms = $('[name=Git],[name=Hg],[name=SVN]');
$scms.change(function(){
if ( $(this).attr('checked') ) {
var on = this;
$scms.each(function(){
if ( this !== on ) {
$(this).removeAttr('checked');
}
});
}
});
});
''')
class NeighborhoodAddProjectValidator(fev.FancyValidator):
def _to_python(self, value, state):
try:
value = h.really_unicode(value or '').encode('utf-8').lower()
neighborhood = M.Neighborhood.query.get(name=state.full_dict['neighborhood'])
neighborhood.register_project(value)
return value
except forge_exc.ProjectConflict:
ming.orm.ormsession.ThreadLocalORMSession.close_all()
raise formencode.Invalid('A project already exists with that name, please choose another.',
value, state)
except Exception, ex:
log.exception('Unexpected error creating project')
ming.orm.ormsession.ThreadLocalORMSession.close_all()
raise formencode.Invalid(str(ex), value, state)