Building Data-Driven Web Apps with Pyramid and SQLAlchemy Transcripts
Chapter: MongoDB edition
Lecture: Final adaptation: user login
0:00 Well, we had everything working, seemed like. Come over here and all the queries are running. There's no data, in fact, even if we go over here
0:08 and we refresh, we now have our packages and their indexes, but again, if we try to look, no records. So, everything looks like it's fine.
0:18 Actually, here's our six users. That's pretty sweet, must have been playing with that. In fact, I know exactly where those six users are coming from.
0:25 Let's get rid of that. Remember this? Yeah, let's drop that. So, it even looks like our little count thing is actually working, 'cause
0:35 apparently we've run this six times now. Okay, so, here's the problem, if I try to login it crashes, and it says hash code has kind of gone
0:45 insane, well, what is going on here? We're trying to hash like a string, or some... Let's go, let's go get, let's go figure this out.
0:58 We can even set a break point here and see what's going on. Try to login again, our user object comes along our user hashed_password is ... not set.
1:08 It's 'cause we haven't actually logged in, right? So there's this sort of messed up data let's go ahead and just clear that out.
1:15 Now if we go try to login, it says the user wasn't found. So, that's good, hey great, that just must mean we need to register, so let's go register.
1:26 Here's where we're going to run into the mismatch between the data types. None type is callable, oh, maybe not yet.
1:32 Our user_service, find_user_by_id, apparently we didn't rewrite that one, did we? No, we're looking straight at it. Alright, let's fix that.
1:43 Not terribly hard to fix, is it? Try again. Now, check this out, we were able to register, and then we redirected
1:51 to account, see this is not account/register it did that get post redirect success. However, here's the data type mis-match.
1:59 It said, look, you can't pass me zero an integer, when I expected an ObjectId. It has to be this weird, sort of UUID format for ObjectIds.
2:10 And we can fix that really easy. If we go over to our cookie auth you'll see we're passing integers but we don't want to pass integers
2:16 'cause that's not what the id is anymore. It's now this thing called bson which comes from the dependency we installed, and an ObjectId.
2:24 So, we're going to turn that to a string this is fine, nothing major happened there. Here is where we get the mistake.
2:31 We're returning an integer, and we want to return this. So instead of doing this, try int thing we're going to say try return bson.ObjectId
2:39 of user ID, except ... return none. K, so, we have to convert this from a string to this ObjectId class it knows how to parse it's own strings
2:50 it's fine, but we can't do it as an integer, it's not an integer. Okay, now we should be able to use the site. Look at that, our account is here!
2:59 Michael Kennedy, we can go home. We can see we have one user. Let's logout and create one more do this whole path successfully.
3:06 Register ... Sarah Smith ... firstname.lastname@example.org and ... she just likes the letter s. Boom, looks like she logged in fine.
3:15 Our ... formatting here isn't amazing, but ... That's fine, we're just going to leave it. So, now we've got this entirely working
3:22 with MongoDB, and we come over here and we can go look at our users here we can see this one with a massive crazy, hashed_password, same thing
3:31 for Sarah Smith, with her massive password over here, right, we can scroll way over. That's a serious password. It's pretty sweet, it looks like we've
3:39 converted successfully from SQLAlchemy over to MongoDB. Hopefully you felt like wow we didn't really do that much, I mean, I talked a lot
3:47 to help you understand some of the new concepts but if I didn't talk that much we could have cranked that out really quick.
3:53 Because we add the new entities rewrite the services, we're kind of there.