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