Python for Entrepreneurs Transcripts
Chapter: Sending and receiving email
Lecture: Demo: The password reset table

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Alright, let's start adding the behaviors that we need to do this password reset.
0:05 The first thing that we are going to need to do is we are going to need to add data,
0:09 so we've got our account class, our albums, and our tracks,
0:13 we are going to need another table in the database, so let's add it.
0:17 Now, what goes in here is going to be very similar,
0:20 we are going to derive from SqlAlchemyBase,
0:23 we are going to set a __tablename__, we are going to add a bunch of columns.
0:26 Let me go ahead and just put those into place.
0:28 Alright, so here is our PasswordReset model,
0:31 and we are going to map it to the PasswordReset table.
0:33 Now, let's just review, so we have our id and remember we don't want it to be guessable,
0:37 this should not be an auto-incrementing id, but it should be the primary key,
0:41 so we are using a string, setting the primary key to True,
0:44 and then we are using this lambda to set a default value
0:47 that is generated from a random uuid4 which is a 32 bit alphanumeric string,
0:54 32 character id, which we are turning to a string
0:59 and then we are getting rid of the dashes that come with it.
1:02 And we are also keeping track of when it was created,
1:05 so that one, we just know when it was created,
1:07 and two, we can know when it should expire.
1:10 We keep track of whether or not it was used and when,
1:13 we probably could put those into one by checking for null on the use date,
1:16 but it's not a table we expect to have tons of records in, so that's fine.
1:20 And then, a little analytics like who actually did this, who was the user,
1:25 where did they come from, right, so if you need to go back
1:29 and auditing and you get a crazy number of reset requests
1:33 and it all coming from one IP address, or some IP block or something like that,
1:37 you could potentially use that information to block future reset requests.
1:41 Finally, last but not least, you are going to need to tie this reset request to a particular user,
1:46 so when somebody comes back with that email address to reset it,
1:49 it's actually going to reset the password for this user
1:52 so we have a foreign key relationship mapping back to that user, right.
1:56 So let's look at the database, over here, notice
2:01 we have Account, Album and Track but we have no reset request.
2:06 We run this, and we refresh, we should now have PasswordReset,
2:13 oh, we do not, and back to SQLAlchemy, why don't we,
2:17 apparently no one has loaded up this code, so let's go do that.
2:21 Remember this bit? Try again.
2:28 Oh oops, it looks like I called that User.id,
2:32 when I should have called that Account.id.
2:38 OK, great. It looks like everything ran well, if I refresh this,
2:43 we should have our PasswordReset, it looks like the database is ready for us
2:47 to put a bunch of PasswordResets in it.