Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1from flask_wtf import FlaskForm 

2from sqlalchemy import func 

3from wtforms import PasswordField 

4from wtforms.fields import EmailField 

5from wtforms.validators import DataRequired 

6from wtforms.validators import Email 

7from wtforms.validators import EqualTo 

8from wtforms.validators import InputRequired 

9from wtforms.validators import Length 

10from wtforms.validators import ValidationError 

11 

12from .models import User 

13 

14 

15class LoginForm(FlaskForm): 

16 email = EmailField( 

17 "email", 

18 [DataRequired(), Email(message="Not a valid email address.")], 

19 render_kw={"class": "form-control", "autocomplete": "off"}, 

20 ) 

21 password = PasswordField( 

22 "Password", 

23 [DataRequired()], 

24 render_kw={"class": "form-control", "autocomplete": "off"}, 

25 ) 

26 

27 

28class RegistrationForm(FlaskForm): 

29 """Registration Form""" 

30 

31 email = EmailField( 

32 "email_label", 

33 [DataRequired(), Email(message="Not a valid email address.")], 

34 ) 

35 

36 password = PasswordField( 

37 "New Password", 

38 validators=[ 

39 InputRequired("Password is required"), 

40 Length( 

41 min=6, 

42 max=25, 

43 message="Password must be between 6 and 25 characters", 

44 ), 

45 EqualTo("confirm", message="Passwords must match"), 

46 ], 

47 ) 

48 confirm = PasswordField( 

49 "Repeat Password", 

50 ) 

51 

52 def validate_email(self, field): 

53 """ 

54 Inline validator for email. Checks to see if a user object with 

55 entered email already present in the database 

56 

57 Args: 

58 field : The form field that contains email data. 

59 

60 Raises: 

61 ValidationError: if the username entered in the field is already 

62 in the database 

63 """ 

64 user = User.query.filter( 

65 func.lower(User.email) == func.lower(field.data) 

66 ).scalar() 

67 

68 if user is not None: 

69 raise ValidationError(f"email '{field.data}' is already in use.")