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