Building data-driven web apps with Pyramid and SQLAlchemy Transcripts
Chapter: MongoDB edition
Lecture: Final adaptation: user login

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