MongoDB with Async Python Transcripts
Chapter: PyPI Beanie
Lecture: Creating Users

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Okay, here we go. The last bit of our CLI app that we need is the create a user version
0:08 Create user option. So we got our create release working great. Now we want to create user again
0:14 Let me just give you the the UI interaction bits something to work with here So when I create a user ask, what is the user's name and email?
0:23 and we remember that Whenever you have accounts you can't really have duplicate emails because if a user is going to come along and say I need to reset
0:32 My password enter your email. Well if you have multiple users with one email Who do you send the reset to right?
0:39 So you really kind of need to treat the email as unique and so we're going to make sure that you
0:43 can't create an account with an email where the email already exists and
0:47 Then we're just going to gather some information. We're going to create a local Location and pass it on to this create user
0:55 Let's have that one created and this one created, then we'll fix them. Again, these all have to be async.
1:05 Let's do this find a user by email first because we're going to need to check that the user doesn't exist, aren't we?
1:11 First, we're going to do this real super, super easy. We can just await user.find1, user.email, equal equal email.
1:23 Now one thing I like to do all the time when I'm talking about emails is
1:27 canonicalize them, make them consistently the same no matter how the user enters them.
1:33 If there's an uppercase, you maybe don't want to store the uppercase, right? It doesn't really matter, but if you don't want it to vary by case, and
1:40 remember MongoDB, this is a case-sensitive compare. So we can just say email equals email.lower.
1:48 And if they happen to put a space in there as well, we can strip those off.
1:52 And while we're at it, let's go ahead and do that up here in the create user.
1:57 And we'll do that for the name, but you don't want a lowercase name, you just don't want spaces. So there's that.
2:04 To password take spaces, I'm going to say passwords can't have spaces either. So there's a little bit of cleanup before we get working with this data.
2:13 Now again, in this place, we want to check and make sure there's a user.
2:17 I think here, because of all the stuff we're going to need to do with the user, it probably makes sense to just say if user by email, email,
2:26 is exception, something like that user already exists with this email. So we're going to create a user, just like we had before. And what does it
2:38 take? It takes a name and email hash password, I'm going to set that in a second. Remember, we're getting past the raw password, we don't want to ever
2:47 ever store that, but we use it to generate this hash over here. So we'll say the name equals name, email equals email,
2:55 hash password equals hash password. Created date and login have defaults, so that's good. This, I'm gonna set the profile image is this,
3:07 and the location is our embedded location object. And then from here on out, it's pretty easy. We await user.save, which will do the insert,
3:17 and then we can return this user, which will have its auto generated ID, its underscore ID property set by MongoDB.
3:25 So if we need to take the ID that was created and run with it, we'll have it now. Okay, so let's go ahead and try to create a user
3:33 without their password, and then we can come back and talk about passwords separately. So try again. Let's go ahead and try to create a user.
3:41 Let me find an email. Let's go to the users here. Let's try to just create one with this email here just to test the error handling.
3:52 It'll be you as the full name, there's their email, error, a user with this email already exists. Okay, great. That seems to work.
4:01 Oh, it looks like I forgot this canonicalization bit, the just name strip, not email. Don't want to screw that up. Let's try again. Create a user.
4:14 Let's do it like this. at talkpython.fm space letter A still security conscious Oregon United States.
4:28 Awesome. We created Michael Kennedy with lowercase email, no space, and the ID generated by MongoDB.
4:38 So we could take that ID, go back over here and say I want to find the users. I'm going to say underscore ID is an object ID, lowercase D.
4:49 There you go, here's the one we just inserted. You can see all the canonicalization and the ID is set. Beautiful, beautiful, beautiful.
4:57 Okay, so creating a user works. Sorry about that mistake here. This is something that is so easy to make the mistake of.
5:06 Right, when I had this inline here like this, it's really easy to write code that looks right.
5:13 You look at it, it looks fine, but because it's a coroutine, it's never ever gonna be anything other than a potentially executable coroutine
5:22 till you await it to get the result out of it. So just be super, super careful.


Talk Python's Mastodon Michael Kennedy's Mastodon