from flask_wtf import FlaskForm from wtforms import ( BooleanField, FileField, StringField, SubmitField, TextAreaField, ValidationError ) from wtforms.validators import ( DataRequired, Email, Length, Regexp ) from app.models import User from app.auth import USERNAME_REGEX from app.wtf_validators import FileSizeLimit class EditProfileSettingsForm(FlaskForm): email = StringField( 'E-Mail', validators=[DataRequired(), Length(max=254), Email()] ) username = StringField( 'Username', validators=[ DataRequired(), Length(max=64), Regexp( USERNAME_REGEX, message=( 'Usernames must have only letters, numbers, dots or ' 'underscores' ) ) ] ) submit = SubmitField() def __init__(self, user, *args, **kwargs): super().__init__(*args, **kwargs) self.user = user def validate_email(self, field): if (field.data != self.user.email and User.query.filter_by(email=field.data).first()): raise ValidationError('Email already registered') def validate_username(self, field): if (field.data != self.user.username and User.query.filter_by(username=field.data).first()): raise ValidationError('Username already in use') class EditPublicProfileInformationForm(FlaskForm): avatar = FileField( 'Image File', [FileSizeLimit(max_size_in_mb=2)] ) full_name = StringField( 'Full name', validators=[Length(max=128)] ) about_me = TextAreaField( 'About me', validators=[ Length(max=254) ] ) website = StringField( 'Website', validators=[ Length(max=254) ] ) organization = StringField( 'Organization', validators=[ Length(max=128) ] ) location = StringField( 'Location', validators=[ Length(max=128) ] ) submit = SubmitField() def validate_image_file(self, field): if not field.data.filename.lower().endswith('.jpg' or '.png' or '.jpeg'): raise ValidationError('only .jpg, .png and .jpeg!') class EditPrivacySettingsForm(FlaskForm): is_public = BooleanField('Public profile') show_email = BooleanField('Show email') show_last_seen = BooleanField('Show last seen') show_member_since = BooleanField('Show member since') submit = SubmitField()