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.

68 lines
2.9 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.')
  30. class ChangePasswordForm(FlaskForm):
  31. old_password = PasswordField('Old password', validators=[DataRequired()])
  32. password = PasswordField('New password', validators=[
  33. DataRequired(), EqualTo('password2', message='Passwords must match.')])
  34. password2 = PasswordField('Confirm new password',
  35. validators=[DataRequired()])
  36. submit = SubmitField('Update password')
  37. class PasswordResetRequestForm(FlaskForm):
  38. email = StringField('Email', validators=[DataRequired(), Length(1, 64),
  39. Email()])
  40. submit = SubmitField('Reset password')
  41. class PasswordResetForm(FlaskForm):
  42. password = PasswordField('New password', validators=[
  43. DataRequired(), EqualTo('password2', message='Passwords must match.')])
  44. password2 = PasswordField('Confirm password', validators=[DataRequired()])
  45. submit = SubmitField('Reset password')
  46. class ChangeEmailForm(FlaskForm):
  47. email = StringField('Email', validators=[DataRequired(), Length(1, 64,
  48. Email())])
  49. password = PasswordField('Password', validators=[DataRequired()])
  50. submit = SubmitField('Update Email address')
  51. def validate_email(self, field):
  52. if User.query.filter_by(email=field.data).first():
  53. raise ValidationError('Email already registered.')