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, we've got them created in the database we've got the repository able to query some of them
0:10 but we still have one thing we'd like to take care of. If I open this up and I say select star from user
0:16 and I run this, what amazing users do we have, none, no users, so there's a couple of things we could do
0:24 we could go and actually manage this like outside of our program which I guess would be okay, but then we wouldn't have
0:30 some of those defaults run and things like that, so let's just go ahead and add a little bit of work down here,
0:36 let's write one more function that we can do call just to say hey let's create some that temporary users, so we'll say create test users,
0:47 actually let's just call it like this, create user and we'll pass in a user,
0:50 so what are we going to require, let's go look at the non non default things so this we don't have to pass it's auto incrementing
0:58 this we don't have to pass it's got a default, this we don't have to pass it as a default, this, I don't really know what to set here,
1:05 we're not actually hashing passwords and it's not required, so we're not going to pass that.
1:09 So really, all we need is the name actually that's kind of cool. So let's go over here and say it's going to take a user name,
1:15 and so we want to create a session and then we'll say such user equals user like that and then just say name = username and then session.add user
1:27 and don't forget to commit, and we can go ahead and pass this back and now this should have all those defaults
1:36 set by the database so that will be handy all right so now let's go in our little init here just for a moment,
1:41 let's go over here and say repository.add user, create user so this will be jeff chloe sarah and mike, all right, so we're going to create all those
1:58 so that we can get them back, I guess I get them out with the tools, right
2:02 we could go and print out there their password or the API key that was generated but it'll be over here, let's just run this really quick
2:08 now you see it's already done it, so let's comment those out so we don't create the same users twice, that would really crash
2:15 because the uniqueness constraints, so if we go over here and say a table editor, you can see we have our users and there's their API keys right
2:22 no password, some of them created, their auto incremented id, their names and so on.
2:26 So let's copy that, we're going to work on behalf of this user mike for the rest of the time
2:32 so after this we need to talk about how do we take this api key and pass this along to our function, how do we get it out,
2:38 what is the convention that we're going to follow, and things like that. So now we have for users let's go work on the service.


Talk Python's Mastodon Michael Kennedy's Mastodon