Building Data-Driven Web Apps with Pyramid and SQLAlchemy Transcripts
Chapter: User input and HTML forms
Lecture: One source of data

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's look at again how we're getting data from the browser. It's actually coming to us in multiple locations
0:07 and we saw this in the beginning of the chapter. Here we're getting data from the Post dictionary. But remember we also have the Get
0:15 we also have the matchdict and it's quite common to get them from multiple locations. So imagine, you'll have like part of the URL
0:23 which maybe describes an item you're trying to let's say it's a book store, you're going to view a book and make a review of that book.
0:31 The URL might actually say which book the review is but the form might contain the details of your email address and your review details and so on.
0:40 So you have to get data both from matchdict and from Post. Maybe there's a query string going on as well and you got to get it from Get.
0:47 All of these things can be confusing and they're sort of needlessly confusing. What I would like to do is just go
0:54 "Hey, what data did they just give me? "Is there a thing called email, great, what's its value?" And whether that comes from Post
1:02 or from somewhere else in the URL, great we'll just take it wherever it comes from. Of course we can set up priorities
1:09 so things like the URL has a higher preference or higher priority than say, the form which is maybe easier to edit
1:17 which still has higher preference than Get which is the query string, which is super easy to mess with.
1:22 Things like that, so we'll set up an order of priority here. What we're going to do is really quickly introduce this ability
1:28 to sort of merge these in a nice automatic way. And then we're going to use that internally in the next chapter even better.
1:37 So let's over to infrastructure and create a file, I'm going to call, requestdict. So it's going to take all the data passed in
1:46 and is bundled up there, and just convert it into one big source of data that we can ask "What's the email, what's the password
1:54 "is the user logged in," things like that. So I have a function just called create it's going to take a request, it's going to be a pyramid request
2:04 and it's going to going to return, any. I'd like to say it returns dictionary but we're going to give it a little extra feature
2:12 that dictionaries don't have so any's what we're going with. So the request, remember it has let's just go over here and we'll say it has some data.
2:21 We'd like to create a new dictionary based on the various dictionaries in there and as of Python 3.5, I think it is
2:29 the cleanest way to create a dictionary out of say two dictionaries is you **d1, **d2, as many as you have.
2:37 This little star will let you accumulate these dictionaries. Let's do it like this we want to have the least priority first
2:44 and the highest priority last so let's say request.get. And then we can even throw in the headers so any information passed along
2:52 the header we can ask for in this way. Of course we'll have Post and we'll have matchdict. This is the actual URL that we told maps over
3:01 to this part of our site, so go with that. And then we can just say return data. This is already better. This let's us create this thing where
3:10 it doesn't matter if the data goes in POST or matchdict we can just ask for it back and boom, we get it.
3:17 Let's go over here and see how we would go about that. So here we'll say data = requestdict we need to import that, .create from the request
3:32 and now you don't have to know that it comes from Post. Or really where it comes from, we're just going to say
3:39 "Give me that data." And let's just check really quick that we're able to log in, that this is working. Great, we're logged out, so let's log in.
3:50 Here we go. Boom, logged in, perfect, log back out. That's working pretty well, we could go a little bit farther
3:59 and have it so you could just say .email, like this instead of even using this dictionary access. I'll go and add this, you can decide
4:07 whether or not it's worth it for you. So we could easily do that by adding just a little class here called RequestDictionary.
4:15 And it's going to derive from dict so that means it basically is a dictionary and it can do all the stuff we're doing here
4:23 but in addition to this, we're going to define get_attr and it's going to pass in the item that it wants to get.
4:28 This is basically the key, maybe a better word there. And then we just say return self.get(key). And that's what we're doing above
4:39 but it lets us do it in a simpler way. So down here we can convert this to a RequestDictionary like so and let's just change this one to email
4:50 you don't get any help because it's super dynamic there. It's just reaching inside the dictionary and these are the keys.
4:57 So let's see we can get email and password I'll do a sort of mixed mode here for just a minute. Sure we log in one more time.
5:08 Boom, looks like it still works. Logged in, of course it had to get the email to get the right user back so that must have worked.
5:15 And I guess we'll go with this. It's kind of nice to have this simple little thing and let's just ask really quick for something
5:23 that doesn't exist to make sure what we get back is just null, rather than a crash. Yeah, password2 is just None.
5:35 It didn't come back and give you a key error, for example. This thing is pretty slick, it really helps us. We could even throw in the cookies
5:43 if for some reason we wanted to get data from cookies but I think these four locations are pretty good for starters.
5:53 Oh, one really quick thing, why don't we just now that we've done this let's tell it we're going to return a RequestDictionary.
6:01 Which is pretty cool, that way and when we go up here we do get at least the dictionary things.
6:05 We don't get help with email and password but that's okay.


Talk Python's Mastodon Michael Kennedy's Mastodon