RESTful and HTTP APIs in Pyramid Transcripts
Chapter: Authenticated services
Lecture: Creating a few users

Login or purchase this course to watch this video and the rest of the course contents.
0:01 So, we've got our users modeled here,
0:04 we've got them created in the database
0:06 we've got the repository able to query some of them
0:09 but we still have one thing we'd like to take care of.
0:11 If I open this up and I say select star from user
0:15 and I run this, what amazing users do we have, none, no users,
0:20 so there's a couple of things we could do
0:23 we could go and actually manage this like outside of our program
0:26 which I guess would be okay, but then we wouldn't have
0:29 some of those defaults run and things like that,
0:32 so let's just go ahead and add a little bit of work down here,
0:35 let's write one more function that we can do call just to say
0:39 hey let's create some that temporary users,
0:43 so we'll say create test users,
0:46 actually let's just call it like this, create user and we'll pass in a user,
0:49 so what are we going to require, let's go look at the non non default things
0:54 so this we don't have to pass it's auto incrementing
0:57 this we don't have to pass it's got a default,
0:59 this we don't have to pass it as a default,
1:02 this, I don't really know what to set here,
1:04 we're not actually hashing passwords
1:06 and it's not required, so we're not going to pass that.
1:08 So really, all we need is the name actually that's kind of cool.
1:10 So let's go over here and say it's going to take a user name,
1:14 and so we want to create a session
1:15 and then we'll say such user equals user like that
1:20 and then just say name = username and then session.add user
1:26 and don't forget to commit,
1:31 and we can go ahead and pass this back
1:33 and now this should have all those defaults
1:35 set by the database so that will be handy
1:38 all right so now let's go in our little init here just for a moment,
1:40 let's go over here and say repository.add user, create user
1:48 so this will be jeff chloe sarah and mike,
1:55 all right, so we're going to create all those
1:57 so that we can get them back, I guess I get them out with the tools, right
2:01 we could go and print out there their password or the API key that was generated
2:04 but it'll be over here, let's just run this really quick
2:07 now you see it's already done it, so let's comment those out
2:12 so we don't create the same users twice, that would really crash
2:14 because the uniqueness constraints,
2:16 so if we go over here and say a table editor,
2:18 you can see we have our users and there's their API keys right
2:21 no password, some of them created, their auto incremented id, their names and so on.
2:25 So let's copy that, we're going to work on behalf of this user mike for the rest of the time
2:31 so after this we need to talk about how do we take this api key
2:34 and pass this along to our function, how do we get it out,
2:37 what is the convention that we're going to follow, and things like that.
2:40 So now we have for users let's go work on the service.