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