1
00:00:05,950 --> 00:00:07,470
Welcome back everyone to part two.

2
00:00:07,470 --> 00:00:09,520
A flask loggin in this lecture.

3
00:00:09,540 --> 00:00:15,540
We're going to set up our forms both a registration form to register new users as well as a logon form.

4
00:00:15,540 --> 00:00:20,190
We'll also show you a couple of new things with forms such as checking to make sure that two fields

5
00:00:20,190 --> 00:00:25,860
of a form match such as when you're providing a password and then you're prompted to confirm your password.

6
00:00:26,070 --> 00:00:26,430
OK.

7
00:00:26,520 --> 00:00:27,400
Let's get started.

8
00:00:28,710 --> 00:00:30,150
Welcome back everyone.

9
00:00:30,150 --> 00:00:32,170
Here we are we left off last time.

10
00:00:32,250 --> 00:00:38,670
Previously we filled out our PI file as well as our models of Pi file and created the user model as

11
00:00:38,670 --> 00:00:40,680
well as this load user function.

12
00:00:40,680 --> 00:00:45,900
Now we're going to need to set up some forms for the user to log into the Web site and register it to

13
00:00:45,900 --> 00:00:47,070
the Web site.

14
00:00:47,070 --> 00:00:52,630
So we're going to come to forums up high and start creating some forums here in order to do this will

15
00:00:52,630 --> 00:00:58,590
we'll start for basic imports for forms which is going to be flask underscored that be T.F. will import

16
00:00:58,590 --> 00:01:04,740
flask form and then from W.T. forms are going to import the fields we're going to be using We'll be

17
00:01:04,740 --> 00:01:12,090
using a string field a password field and they submit field then we're also going to be using validators.

18
00:01:12,090 --> 00:01:22,860
So from W.T. forms validators I'm going to import data required e-mail and equal to.

19
00:01:22,860 --> 00:01:28,110
So essentially these validators can be passed in two instances of string field password field or submit

20
00:01:28,110 --> 00:01:33,060
field and they're actually a little bit self-explanatory based off their names that basically data required

21
00:01:33,060 --> 00:01:38,820
means you can't leave that specific field in the form blank e-mail checks to make sure that you're actually

22
00:01:38,820 --> 00:01:41,900
using an e-mail so it checks for something like an at symbol.

23
00:01:41,970 --> 00:01:45,700
It ends and dot org or dot com that sort of thing and then equal to.

24
00:01:45,840 --> 00:01:48,600
We're going to be using that for a password confirmation.

25
00:01:48,660 --> 00:01:52,380
So often when you're registering you're going to want to make sure that you confirm the user's password

26
00:01:52,650 --> 00:01:54,350
so that they don't accidentally mess it up.

27
00:01:55,750 --> 00:01:58,050
OK so we have our lips.

28
00:01:58,060 --> 00:02:05,220
One more important issue is those Aksel validation errors to from the witty forms import validation

29
00:02:05,240 --> 00:02:06,140
here.

30
00:02:06,760 --> 00:02:10,950
OK now I'm going to start off by creating our registration form.

31
00:02:11,170 --> 00:02:18,910
Since that's the more complicated of the two will say class registration form and we're going to inherit

32
00:02:19,000 --> 00:02:23,940
flask form and in order to register we're going to have a couple of fields.

33
00:02:24,230 --> 00:02:27,100
A user is going to provide their e-mail.

34
00:02:27,240 --> 00:02:34,490
So we'll say string field call it e-mail as it's label and get a pass and some validators.

35
00:02:34,520 --> 00:02:38,780
So remember the validators it's going to be a list of instances of Daval dater.

36
00:02:39,020 --> 00:02:43,320
So we're going to say data is required for the e-mail so they have to provide an e-mail.

37
00:02:43,640 --> 00:02:50,130
And we'll also say e-mail validation which basically checks that it is an actual e-mail they'll say

38
00:02:50,140 --> 00:02:51,190
user name.

39
00:02:51,250 --> 00:02:55,840
This is also going to be a string field and we'll give it the label user name.

40
00:02:55,840 --> 00:02:58,990
And for this we'll just have the validators be that it's required.

41
00:02:58,990 --> 00:03:04,260
So even if it's just a single validator it still goes inside of a list data required.

42
00:03:04,300 --> 00:03:09,520
Open and close print sees so it's an instance of data required then we're going to have the passwords.

43
00:03:09,880 --> 00:03:14,380
So say that the password the user provides is going to go inside of a password field.

44
00:03:14,410 --> 00:03:23,050
That way it's hidden from you as you begin typing it in and will say password as the label and the validators

45
00:03:23,060 --> 00:03:30,920
we're going to have here are data required an instance of that and will also have equal to.

46
00:03:31,630 --> 00:03:37,570
So we're going to actually leave equal to blank for now but we will come back to this in order to sync

47
00:03:37,570 --> 00:03:42,360
it with another field called Pass confirm.

48
00:03:42,730 --> 00:03:47,440
And it's kind of up to you what variable names you choose over here on the left hand side but technically

49
00:03:47,560 --> 00:03:51,730
should come choose something that looks like this it matches your user model but this will also be a

50
00:03:51,730 --> 00:03:53,260
password field.

51
00:03:53,590 --> 00:04:02,880
And we're going to say the label on this one is confirmed password and the validators will be data required.

52
00:04:02,900 --> 00:04:09,920
OK so essentially what I want to do here is I want to link this password field the first one to my past

53
00:04:09,920 --> 00:04:15,140
confirm essentially checking that these two match which is where the equal to validator is going to

54
00:04:15,140 --> 00:04:16,040
come into play.

55
00:04:16,280 --> 00:04:18,780
So by itself equal to won't do anything.

56
00:04:18,860 --> 00:04:20,580
We have to do a couple of things here.

57
00:04:20,720 --> 00:04:24,030
We have to provide actual attributes of our form.

58
00:04:24,140 --> 00:04:25,430
It needs to be equal to.

59
00:04:25,490 --> 00:04:27,690
So we passed it as a string.

60
00:04:27,690 --> 00:04:34,070
And we say pass underscore confirm basically matching this first string here that we're passing into

61
00:04:34,070 --> 00:04:37,390
equal to to whatever field you want to make sure it's equal to.

62
00:04:37,640 --> 00:04:39,370
So right here we have passed confirm.

63
00:04:39,500 --> 00:04:41,630
So maybe you want to also confirm their e-mail.

64
00:04:41,630 --> 00:04:47,330
It would be the same thing you would create a new field called email confirm and then you would just

65
00:04:47,330 --> 00:04:48,470
match it up there.

66
00:04:49,190 --> 00:04:52,610
So right now matching this equal to to pass confirm.

67
00:04:52,610 --> 00:04:56,410
So this password field must be equal to the date you supplied here.

68
00:04:56,510 --> 00:05:03,020
And as a second argument inside of equal to you can give a message to give in case the values are equal

69
00:05:03,020 --> 00:05:03,520
to.

70
00:05:03,710 --> 00:05:08,650
So I'm going to say passwords must match.

71
00:05:08,870 --> 00:05:14,050
OK so if equal to is it actually meant I send them back a little message saying Hey The password is

72
00:05:14,050 --> 00:05:17,340
much much must match a little bit of a tongue twister there.

73
00:05:17,650 --> 00:05:18,430
OK.

74
00:05:18,430 --> 00:05:21,940
So we have our e-mail username password and our confirmation for the password.

75
00:05:21,970 --> 00:05:24,940
Finally our form should have a submit button with it.

76
00:05:25,180 --> 00:05:28,880
So this will just be a submit field this is our registration form.

77
00:05:28,900 --> 00:05:32,910
I'm going to go up and say register OK.

78
00:05:33,050 --> 00:05:34,710
We have our fields ready to go.

79
00:05:34,940 --> 00:05:37,140
Let's add in two methods.

80
00:05:37,340 --> 00:05:40,000
One is going to be called the check email method.

81
00:05:40,250 --> 00:05:47,510
So say self field and this is going to check if not none for that user e-mail.

82
00:05:47,510 --> 00:05:55,810
So watch what I'm going to do here I'm going to say if user queery and then I will call filter by essentially

83
00:05:55,820 --> 00:06:01,370
or or am calls that we discussed during or database lecture will say if user thought queries that filter

84
00:06:01,370 --> 00:06:07,130
by email is equal to field the data

85
00:06:10,320 --> 00:06:11,900
first.

86
00:06:11,940 --> 00:06:13,610
So what is this actually doing.

87
00:06:13,680 --> 00:06:20,970
Basically this function is going to check to see if that email has already been activated or registered

88
00:06:21,810 --> 00:06:28,020
and if it has been ongoing say raise a validation error which is what we imported up here from W.T.

89
00:06:28,020 --> 00:06:28,700
forms.

90
00:06:29,750 --> 00:06:36,980
And then we can send back a message as a string here so we can see your e-mail has been already registered

91
00:06:38,490 --> 00:06:39,690
registered.

92
00:06:40,130 --> 00:06:40,690
OK.

93
00:06:40,910 --> 00:06:45,470
So there's actually a lot going on in the back end for us due to the fact that this validation error

94
00:06:45,470 --> 00:06:47,000
is just simply imported.

95
00:06:47,060 --> 00:06:53,870
But the main idea here again is that this method check email is going to query our user model.

96
00:06:53,960 --> 00:06:57,650
Essentially our user table and it's good to check hey where's that e-mail.

97
00:06:57,650 --> 00:07:00,490
Matching the field of the data that was passed in.

98
00:07:00,490 --> 00:07:04,850
So in this case going to be the e-mail data grab the first one since we only expect there to be one

99
00:07:04,850 --> 00:07:08,750
e-mail and then say hey if that already exists.

100
00:07:08,750 --> 00:07:12,410
So essentially if it's not none then raise a validation error.

101
00:07:12,410 --> 00:07:17,840
If that email is not already in the table then this will be essentially false and we won't raise that

102
00:07:17,840 --> 00:07:22,190
validation error and we're going to do the exact same thing for check.

103
00:07:22,210 --> 00:07:34,290
Username so I'll say the F check the username self and the field that's passed then I'll say if user

104
00:07:35,400 --> 00:07:49,000
queery filter by where user name is equal to that field data that's passed then off the first one then

105
00:07:49,030 --> 00:07:55,830
raise the validation error and we can just say user name is taken.

106
00:07:57,960 --> 00:07:58,470
OK.

107
00:07:58,680 --> 00:08:03,690
So again just checking if the e-mail is valid and checking if the user name is valid and this field

108
00:08:03,690 --> 00:08:04,330
does data.

109
00:08:04,350 --> 00:08:08,120
Again that's just how we're grabbing the data off the form once it's submitted.

110
00:08:08,640 --> 00:08:09,230
OK.

111
00:08:09,420 --> 00:08:12,270
So we have a registration form now.

112
00:08:12,320 --> 00:08:15,240
The user after their register should be able to log in.

113
00:08:15,590 --> 00:08:22,090
So we're going to create a log in form here and will inherit again from a flask form.

114
00:08:22,090 --> 00:08:23,880
And this is going to be quite similar.

115
00:08:23,950 --> 00:08:33,700
Well just speak email is equal to a string field will give the label email and we'll have some validators

116
00:08:33,700 --> 00:08:33,960
here.

117
00:08:33,970 --> 00:08:39,280
Basically they'll have to provide an email so say that are required and it should be an email so I'll

118
00:08:39,310 --> 00:08:46,210
say also e-mails validate or their Then we're going to create a password field instead of a log in form.

119
00:08:46,240 --> 00:08:52,720
So this will be a password field label will be password and then we'll set the validators equal to just

120
00:08:52,720 --> 00:08:53,860
data required.

121
00:08:55,790 --> 00:08:57,000
An instance of data required.

122
00:08:57,020 --> 00:09:05,640
And after that they'll just submit using a submit field and we'll have it say just log in.

123
00:09:06,420 --> 00:09:07,000
OK.

124
00:09:07,080 --> 00:09:08,810
So we set up our forms again.

125
00:09:08,820 --> 00:09:13,680
All we're doing here is setting up a form for registering the user and logging in the user.

126
00:09:13,680 --> 00:09:18,060
Notice here how we're using validators in order to make sure that they're providing data and also that

127
00:09:18,060 --> 00:09:19,690
the data is in the right format.

128
00:09:19,710 --> 00:09:25,910
We also showed you how you can have one field make sure to match another field by using equal to.

129
00:09:25,920 --> 00:09:30,480
And then we set up these two methods to check the email and the username to make sure they're not already

130
00:09:30,480 --> 00:09:33,830
in our tables and we'll be using those later on.

131
00:09:33,930 --> 00:09:34,410
OK.

132
00:09:34,560 --> 00:09:35,930
So that's it for this lecture.

133
00:09:35,940 --> 00:09:38,450
Again this lecture is pretty much just on setting up the forms.

134
00:09:38,520 --> 00:09:41,300
Coming up next is where we actually start setting up our views.

135
00:09:41,310 --> 00:09:42,300
We'll see you there.
