forms.py (4076B)
1 from flask_wtf import FlaskForm 2 from flask_wtf.file import FileAllowed, FileField 3 from wtforms import BooleanField, PasswordField, StringField, SubmitField, IntegerField, SelectField 4 from wtforms.validators import DataRequired, Email, EqualTo, Length, ValidationError 5 6 from .models import User 7 8 current_user: User 9 10 """ registration form for register.html """ 11 12 13 class RegistrationForm(FlaskForm): 14 name = StringField('Naam', validators=[ 15 DataRequired(), Length(min=5, max=20)]) 16 email = StringField('E-Mail', validators=[DataRequired(), Email()]) 17 password = PasswordField('Wachtwoord', validators=[DataRequired()]) 18 confirm_password = PasswordField('Wachtwoord herhalen', validators=[ 19 DataRequired(), EqualTo('password')]) 20 phone = StringField('Telefoon', validators=[DataRequired(), Length(min=5)]) 21 street = StringField('Straat', validators=[DataRequired()]) 22 housenumber = IntegerField('Huisnummer', validators=[DataRequired()]) 23 postcode = StringField('Postcode', validators=[DataRequired()]) 24 place = StringField('Plaats', validators=[DataRequired()]) 25 submit = SubmitField('Registeren') 26 27 """ validates whether name is already in use """ 28 29 def validate_name(self, name): 30 if User.query.filter_by(name=name.data).first(): 31 raise ValidationError( 32 'Deze gebruikersnaam bestaat al, kies een andere.') 33 34 """ validates whether e-mail is already in use """ 35 36 def validate_email(self, email): 37 if User.query.filter_by(email=email.data).first(): 38 raise ValidationError( 39 'Deze e-mail bestaat al, log in als dat uw e-mail is.') 40 41 def validate_phone(self, phone): 42 for c in phone.data: 43 if c not in '0123456789- ': 44 raise ValidationError('Dit belnummer is niet geldig.') 45 46 def validate_postcode(self, code): 47 if len(code.data) != 6 or not code.data[0:4].isnumeric() or not code.data[4:6].isalpha(): 48 raise ValidationError('De postcode is niet geldig.') 49 50 51 """ login form for login.html """ 52 53 54 class LoginForm(FlaskForm): 55 email = StringField('E-Mail', validators=[DataRequired(), Email()]) 56 password = PasswordField('Wachtwoord', validators=[DataRequired()]) 57 remember = BooleanField('Herinneren') 58 submit = SubmitField('Inloggen') 59 60 61 """ update account form for account.html """ 62 63 64 class UpdateAccountForm(FlaskForm): 65 name = StringField('Naam', validators=[ 66 DataRequired(), Length(min=2, max=20)]) 67 email = StringField('E-Mail', validators=[DataRequired(), Email()]) 68 password = PasswordField('Wachtwoord', validators=[]) 69 confirm_password = PasswordField( 70 'Wachtwoord herhalen', validators=[EqualTo('password')]) 71 picture = FileField('Profielfoto bewerken', validators=[ 72 FileAllowed(['jpg', 'png'])]) 73 submit = SubmitField('Bewerken') 74 75 """ validates whether name is already in use """ 76 77 def validate_name(self, name): 78 if name.data != current_user.name and User.query.filter_by(name=name.data).first(): 79 raise ValidationError( 80 'Deze gebruikersnaam bestaat al, kies een andere.') 81 82 """ validates whether e-mail is already in use """ 83 84 def validate_email(self, email): 85 if email.data != current_user.email and User.query.filter_by(email=email.data).first(): 86 raise ValidationError( 87 'Deze e-mail bestaat al, log in als dat uw e-mail is') 88 89 90 class UpdateTrapForm(FlaskForm): 91 name = StringField('Naam') 92 submit = SubmitField('Bewerken') 93 94 95 """ search form for admin.html """ 96 97 98 class SearchForm(FlaskForm): 99 username = StringField('Naam', validators=[ 100 DataRequired(), Length(min=2, max=20)]) 101 submit = SubmitField('Zoeken') 102 103 104 """ account-settings form for admin_user.html """ 105 106 107 class AdminForm(FlaskForm): 108 type = SelectField('Type', choices=[ 109 ('client', 'Klant'), ('admin', 'Administrator')]) 110 submit = SubmitField('Bewerken')