1
0
forked from moepman/bk-dss

Simplify form validation.

This commit is contained in:
Markus 2016-02-10 16:56:54 +01:00
parent 42718082a1
commit 516b448422

View File

@ -23,8 +23,8 @@ class ReadonlyStringField(StringField):
class EditForm(Form): class EditForm(Form):
user = ReadonlyStringField('Username') user = ReadonlyStringField('Username')
pwd1 = PasswordField('New Password') pwd1 = PasswordField('New Password', validators = [Required()])
pwd2 = PasswordField('New Password (repeat)') pwd2 = PasswordField('New Password (repeat)', validators = [Required(), EqualTo('pwd1', "Passwords must match")])
submit = SubmitField('Submit') submit = SubmitField('Submit')
class LoginForm(Form): class LoginForm(Form):
@ -59,24 +59,21 @@ def edit():
user = rdb.hget(session['uuid'], 'user') user = rdb.hget(session['uuid'], 'user')
if form.validate_on_submit(): if form.validate_on_submit():
if form.pwd1.data != form.pwd2.data: opwd = rdb.hget(session['uuid'], 'pswd')
form.pwd2.errors.append("Passwords do not match.") npwd = form.pwd1.data
l = ldap.initialize(app.config.get('LDAP_URI', 'ldaps://127.0.0.1'))
try:
l.simple_bind_s(user, opwd)
l.passwd_s(user, opwd, npwd)
except ldap.INVALID_CREDENTIALS as e:
form.user.errors.append(e.message['desc'])
l.unbind_s()
return render_template('edit.html', form=form, nav=nav)
else: else:
opwd = rdb.hget(session['uuid'], 'pswd') # TODO display success message
npwd = form.pwd1.data rdb.hset(session['uuid'], 'pswd', npwd)
l = ldap.initialize(app.config.get('LDAP_URI', 'ldaps://127.0.0.1')) l.unbind_s()
try: return redirect(url_for('index'))
l.simple_bind_s(user, opwd)
l.passwd_s(user, opwd, npwd)
except ldap.INVALID_CREDENTIALS as e:
form.user.errors.append(e.message['desc'])
l.unbind_s()
return render_template('edit.html', form=form, nav=nav)
else:
# TODO display success message
rdb.hset(session['uuid'], 'pswd', npwd)
l.unbind_s()
return redirect(url_for('index'))
form.user.data = user form.user.data = user
return render_template('edit.html', form=form, nav=nav) return render_template('edit.html', form=form, nav=nav)