Python for Entrepreneurs Transcripts
Chapter: Build web apps with Pyramid: Applied web development
Lecture: A peek inside Talk Python

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Maybe it's time to pause for a moment and take a look inside Talk Python Training,
0:05 see what the web app there looks like now that you know all the moving pieces,
0:08 you can see, I am pretty sure you are familiar with this site
0:11 but we can come over and here is the courses,
0:12 and these are the courses, and this is a data-driven bit here
0:16 we could go down to this particular course here, pull out the details,
0:20 you can see it's rendering all these things,
0:23 it's rendering for each chapter all the lectures in that chapter, point out the times,
0:28 so if you click this, it will launch the video as you know.
0:32 It's got account management log out and so on.
0:35 Let's go look and see what the Talk Python Training website looks like.
0:40 So if you look over here, you should see things that are quite similar,
0:44 so for example we have a controllers folder,
0:46 and there is a good number of them in there,
0:50 our data access layer is sort of twofold, or use a set of services
0:54 that are like high level orchestration of the lower level
0:58 SQLAlchemy-based data stuff of course,
1:00 we haven't talked about these yet, we are getting there,
1:02 I just want to kind of give you the tour,
1:04 you can see our SQLAlchemy course definition for example,
1:07 and if you look at each controller,
1:09 so for example we have our account controller here
1:12 and just like I was recommending, down here we have our various organization
1:18 of all these pieces and if we look at the account, you can see
1:21 there is a bunch of options, views and actions you might do,
1:24 like you can change your email, change your password, you know,
1:26 view your account, login, logout, etc.
1:29 So let's look over here, we have a register method, this actually shows you
1:33 the page with the register form and it checks: "Hey, are you already logged in?
1:37 Well why don't you just go to your account, there is no reason for you to register
1:41 if you are logged in." Otherwise it sets that title like I talked about
1:44 and we are going to talk about our view models and how we use those but there it goes,
1:48 so here this one is mapped to the GET and it just shows the blank data basically,
1:53 and then this one, is mapped to the POST
1:57 and so here is what happens when you register for the account,
1:59 it comes in and it gets the data from a dictionary, see that in a little bit,
2:03 verify some stuff, maybe you can set some warnings like
2:07 "hey, if I already have a user by that username you are trying to register",
2:11 maybe it will tell you that this username is already taken,
2:14 and return just to the form with your data filled out.
2:16 Similarly, email taken, too bad email is taken, but if it's not,
2:19 let's create you a new account, let's set you as logged in
2:23 with a cookie authentication layer, save some logs, send you a new email,
2:27 add you to the mailing list and depending on where we are trying to send you back to,
2:33 maybe you registered but you have registered as part of trying to join a class,
2:38 I'll send you to one place or if you just registered straight up the website
2:42 I will send you to a welcome page.
2:45 Alright, so that's sort of the way the templates and the controllers interact,
2:48 there is at the top of course a base controller here,
2:54 you can see there is a lot more going on in the base controller that we'll get to,
3:00 let's look over here at this __init__ I told you it gets more complicated,
3:05 we have our includes, handlers, dv, email, e-commerce,
3:09 video players all these sorts of things, so for example for initializing log in,
3:13 we go to the settings get the log in and so on.
3:17 We are going to come back to this, we are going to dig into this in more detail
3:20 as we get to the various pieces, but I kind of wanted to give you a tour
3:23 to see what are we actually doing,
3:26 recall I said there is a way to sort of build these handlers,
3:31 the routes for the handlers in the nicer way
3:34 so notice build_handlers_sequence given the config, the home,
3:38 designation our courses or player,
3:41 right, so you have HomeController or CourseController and so on,
3:44 and here we map to the various combinations
3:46 like /courses, /courses/, /courses/all, or /courses/show, details, things like that.
3:56 Finally, we also have some unit tests over here,
3:59 which we haven't really talked much about but over here
4:02 we have this TopLevelWebTests class
4:05 and we did something that is a little bit maybe unorthodox
4:08 but I think is highly effective, if you look at the website,
4:15 there is the sitemap.xml,
4:19 and if you actually view it in a way you can look at,
4:22 there is a whole bunch of URLs that talk about what courses you have,
4:24 what courses have transcripts, how you register, how you log in and so on
4:29 and so for our test what we actually do is we go and we get the site map,
4:36 and then for each URL on the site map we come back
4:39 and we do a request against it and verify that the request is supposed to be
4:44 either a 200 or a redirect.
4:48 So if there is some problem with any of those pages,
4:50 there is a really good chance we are going to get
4:52 a 500, of 404 or something else that's not great.
4:56 Like I said, we'll be back here but hopefully this gives you a view of sort of
5:00 you can see what we've learned, and where we are going
5:04 as we build up more realistic and full-featured web apps.