forms.py (4137B)
1 from flask_login import current_user 2 from flask_wtf import FlaskForm 3 from flask_wtf.file import FileAllowed, FileField 4 from wtforms import BooleanField, HiddenField, PasswordField, SelectField, StringField, SubmitField, TextAreaField 5 from wtforms.validators import DataRequired, Email, EqualTo, Length, ValidationError 6 7 from .models import User 8 9 """ registration form for register.html """ 10 class RegistrationForm(FlaskForm): 11 username = StringField('Naam', validators=[ DataRequired(), Length(min=2, max=20) ]) 12 email = StringField('E-Mail', validators=[ DataRequired(), Email() ]) 13 password = PasswordField('Wachtwoord', validators=[ DataRequired() ]) 14 confirm_password = PasswordField('Wachtwoord herhalen', validators=[ DataRequired(), EqualTo('password') ]) 15 submit = SubmitField('Registeren') 16 17 """ validates whether username is already in use """ 18 def validate_username(self, username): 19 if User.query.filter_by(username=username.data).first(): 20 raise ValidationError('Deze gebruikersnaam bestaat al, kies een andere.') 21 22 """ validates whether e-mail is already in use """ 23 def validate_email(self, email): 24 if User.query.filter_by(email=email.data).first(): 25 raise ValidationError('Deze e-mail bestaat al, log in als dat uw e-mail is') 26 27 28 """ login form for login.html """ 29 class LoginForm(FlaskForm): 30 email = StringField('E-Mail', validators=[ DataRequired(), Email() ]) 31 password = PasswordField('Wachtwoord', validators=[ DataRequired() ]) 32 remember = BooleanField('Herinneren') 33 submit = SubmitField('Inloggen') 34 35 36 """ update account form for account.html """ 37 class UpdateAccountForm(FlaskForm): 38 username = StringField('Naam', validators=[ DataRequired(), Length(min=2, max=20) ]) 39 email = StringField('E-Mail', validators=[ DataRequired(), Email() ]) 40 password = PasswordField('Wachtwoord', validators=[]) 41 confirm_password = PasswordField('Wachtwoord herhalen', validators=[ EqualTo('password') ]) 42 picture = FileField('Profielfoto bewerken', validators=[ FileAllowed(['jpg', 'png']) ]) 43 submit = SubmitField('Bewerken') 44 45 """ validates whether username is already in use """ 46 def validate_username(self, username): 47 if username.data != current_user.username and User.query.filter_by(username=username.data).first(): 48 raise ValidationError('Deze gebruikersnaam bestaat al, kies een andere.') 49 50 """ validates whether e-mail is already in use """ 51 def validate_email(self, email): 52 if email.data != current_user.email and User.query.filter_by(email=email.data).first(): 53 raise ValidationError('Deze e-mail bestaat al, log in als dat uw e-mail is') 54 55 """ update/new course form for new_course.html """ 56 class NewCourseForm(FlaskForm): 57 name = StringField('Naam', validators=[ DataRequired(), Length(min=1, max=100) ]) 58 description = TextAreaField('Beschrijving', validators=[ DataRequired() ]) 59 teacher_id = SelectField('Docent', validators=[ DataRequired() ], coerce=int) 60 weekday = SelectField('Weekdag', choices=list(enumerate([ 'Maandag', 'Dinsdag', 'Woensdag', 'Donderdag', 'Vrijdag', 'Zaterdag', 'Zondag' ]))) 61 start = StringField('Begin', validators=[ DataRequired() ]) 62 end = StringField('Einde', validators=[ DataRequired() ]) 63 location = StringField('Locatie', validators=[ DataRequired(), Length(min=1, max=100) ]) 64 submit = SubmitField('Versturen') 65 66 """ subscribe form for course.html """ 67 class SubscribeForm(FlaskForm): 68 lang_id = HiddenField() 69 submit = SubmitField('Inschrijven') 70 71 72 """ unsubscribe form for course.html """ 73 class UnsubscribeForm(FlaskForm): 74 lang_id = HiddenField() 75 submit = SubmitField('Uitschrijven') 76 77 """ search form for admin.html """ 78 class SearchForm(FlaskForm): 79 username = StringField('Naam', validators=[ DataRequired(), Length(min=2, max=20)]) 80 submit = SubmitField('Zoeken') 81 82 """ account-settings form for admin_user.html """ 83 class AdminForm(FlaskForm): 84 type = SelectField('Type', choices=[('client', 'Klant'), ('teacher', 'Docent'), ('admin', 'Administrator')]) 85 submit = SubmitField('Bewerken')