Coverage for modules/box__default/auth/forms.py: 79%

# Coverage for modules/box__default/auth/forms.py : 79%

## 22 statements

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

1 from flask_wtf import FlaskForm

2 from sqlalchemy import func

3 from wtforms import PasswordField

4 from wtforms.fields import EmailField

5 from wtforms.validators import DataRequired

6 from wtforms.validators import Email

7 from wtforms.validators import EqualTo

8 from wtforms.validators import InputRequired

9 from wtforms.validators import Length

10 from wtforms.validators import ValidationError

11

12 from .models import User

13

14

15 class 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

28 class 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.")