You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
1.5 KiB

  1. from flask_wtf import FlaskForm
  2. from wtforms import StringField, PasswordField, BooleanField, SubmitField
  3. from wtforms.validators import DataRequired, Length, Email, Regexp, EqualTo
  4. from wtforms import ValidationError
  5. from ..models import User
  6. class LoginForm(FlaskForm):
  7. email = StringField('Email', validators=[DataRequired(), Length(1, 64),
  8. Email()])
  9. password = PasswordField('Password', validators=[DataRequired()])
  10. remember_me = BooleanField('Keep me logged in')
  11. submit = SubmitField('Log in')
  12. class RegistrationForm(FlaskForm):
  13. email = StringField('Email', validators=[DataRequired(), Length(1, 64,
  14. Email())])
  15. username = StringField('Username', validators=[
  16. DataRequired(), Length(1, 64),
  17. Regexp('^[A-Za-z][A-Za-z0-9_.]*$', 0,
  18. 'Usernames must have only letters, numbers, dots or '
  19. 'underscores')])
  20. password = PasswordField('Password', validators=[
  21. DataRequired(), EqualTo('password2', message='Passwords must match.')])
  22. password2 = PasswordField('Confirm password', validators=[DataRequired()])
  23. submit = SubmitField('Register')
  24. def validate_email(self, field):
  25. if User.query.filter_by(email=field.data).first():
  26. raise ValidationError('Email already registered.')
  27. def validate_username(self, field):
  28. if User.query.filter_by(username=field.data).first():
  29. raise ValidationError('Username already in use.')