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.

50 lines
2.0 KiB

  1. from flask_wtf import FlaskForm
  2. from wtforms import StringField, SubmitField, TextAreaField, BooleanField, \
  3. SelectField
  4. from wtforms.validators import DataRequired, Length, Email, Regexp
  5. from wtforms import ValidationError
  6. from ..models import Role, User
  7. class NameForm(FlaskForm):
  8. name = StringField('What is your name?', validators=[DataRequired()])
  9. submit = SubmitField('Submit')
  10. class EditProfileForm(FlaskForm):
  11. name = StringField('Real name', validators=[Length(0, 64)])
  12. location = StringField('Location', validators=[Length(0, 64)])
  13. about_me = TextAreaField('About me')
  14. submit = SubmitField('Submit')
  15. class EditProfileAdminForm(FlaskForm):
  16. email = StringField('Email', validators=[DataRequired(), Length(1, 64),
  17. Email()])
  18. username = StringField('Username', validators=[
  19. DataRequired(), Length(1, 64),
  20. Regexp('^[A-Za-z][A-Za-z0-9_.]*$', 0,
  21. 'Usernames must have only letters, numbers, dots or '
  22. 'underscores')])
  23. confirmed = BooleanField('Confirmed')
  24. role = SelectField('Role', coerce=int)
  25. name = StringField('Real name', validators=[Length(0, 64)])
  26. location = StringField('Location', validators=[Length(0, 64)])
  27. about_me = TextAreaField('About me')
  28. submit = SubmitField('Submit')
  29. def __init__(self, user, *args, **kwargs):
  30. super(EditProfileAdminForm, self).__init__(*args, **kwargs)
  31. self.role.choices = [(role.id, role.name)
  32. for role in Role.query.order_by(Role.name).all()]
  33. self.user = user
  34. def validate_email(self, field):
  35. if field.data != self.user.email and \
  36. User.query.filter_by(email=field.data).first():
  37. raise ValidationError('Email already registered')
  38. def validate_username(self, field):
  39. if field.data != self.user.username and \
  40. User.query.filter_by(username=field.data).first():
  41. raise ValidationError('Username already in use.')