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.

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