Permalink
Browse files

Extra validations when setting username.

  • Loading branch information...
1 parent 5589c5f commit 83f490629f1e063152e775ffb9be387673797cf3 @avidal committed Apr 4, 2012
Showing with 21 additions and 3 deletions.
  1. +18 −1 wotmad/users/forms.py
  2. +3 −2 wotmad/users/views.py
View
@@ -1,9 +1,26 @@
+from flask import g
+
from flaskext import wtf
+from wotmad.users.models import User
+
class LoginForm(wtf.Form):
assertion = wtf.HiddenField(validators=[wtf.Required()])
+USERNAME_REGEX_ERROR = ("Username must begin with a letter and can only "
+ "contain letters and numbers.")
+
+
class SetupForm(wtf.Form):
- username = wtf.TextField('Username', [wtf.Required()])
+ username = wtf.TextField('Username',
+ [wtf.Required(), wtf.Length(5, 30),
+ wtf.Regexp(r'^[a-zA-Z][a-zA-Z0-9]+$',
+ message=USERNAME_REGEX_ERROR)])
+
+ def validate_username(self, field):
+ """Ensures the username is not already taken"""
+ matches = User.objects(username__iexact=field.data, id__ne=g.user.id)
+ if matches:
+ raise wtf.ValidationError("Username already taken.")
View
@@ -39,8 +39,9 @@ def do_user_setup():
@login_required
def setup_account():
if g.user.is_active:
- flash('Already setup!', 'error')
- return redirect(url_for('index'))
+ #flash('Already setup!', 'error')
+ #return redirect(url_for('index'))
+ pass
# If the user isn't active, they need to set their username
form = SetupForm(obj=g.user)

0 comments on commit 83f4906

Please sign in to comment.