Adding a CMS to Your Pyramid Web App Transcripts
Chapter: A tour of our demo application
Lecture: Exploring the starter source code
0:00 Well, you saw our PyPI clone running. Now, let's look inside and look at the source code and see how it's built. This is a pyramid web application.
0:10 We're not gonna talk about the foundations of Pyramid, assuming that if you're taking this course, you're at least somewhat familiar with Pyramid.
0:16 We actually have a whole course where literally we build that PyPI clone in pyramid called "Data Driven Web Apps with Pyramid".
0:24 So if you really want to dig in and you're completely starting from scratch, that that might be a good course to check out.
0:29 Over here, I'm just gonna show you what we got going on because we're going to start from this code,
0:33 and we're gonna start extending it and working with it.
0:36 Now, we brought this '.bin' folder and this just has a couple of little utilities that we'll use to, like, load up the initial data into the database.
0:43 It's probably already been done for the one you're working with, but if not, then you can use that.
0:47 Here's where it gets interesting. We have our controllers. This is where our view methods go. So, for example, we go over to the packages controller.
0:55 We clicked on one of those packages like '/project/pyramid' that we saw. This is the code that runs. Now, This is a pattern I love to use.
1:05 It doesn't appear in many, many web apps, but I think you'll appreciate it. I use this concept of the view model that will exchange the data.
1:12 That is necessary to show, basically render the HTML. Okay, so a lot of the work is gonna happen here, but we're gonna handle this request.
1:20 We're going to basically repopulate the data. If we weren't able to find a package, we're gonna return 'Not Found'.
1:26 Otherwise, we're going to return the dictionary to this 'details.pt' which is over here.
1:33 Like, right there. Notice the controllers have names like packages, and then the templates have directories for those controllers.
1:41 and then files HTML files for the view methods. Really nicely structured there Let's look at that view model real-quick again Same structure
1:49 We have the models. Then, we have the controller name. Then, we have some kind of naming a package details view model,
1:55 And what it's gonna do is, it's just going to do a little bit of base work with the request.
1:59 And then it's going to get the package name that was passed over. Try to use the package name to get the package from the database.
2:05 Set up some information that has to be shown like, what's the latest version and the releases and so on.
2:10 And if it's able to find a package, it's going to populate that from the package and it's releases.
2:16 Of course it has to have a release, or we're just going to say there's no releases All right, so that's how we're showing this content.
2:23 And what else do we need to talk about? We're going to be using SQLAlchemy.
2:28 So, for example, here we have the SQLAlchemy class that's stored into our database for that package,
2:34 and it has things like an ID, Created Date, Summary, and a Description Homepage, Doc URL, Package Installation URL,
2:43 the Authors and the Releases in a relationship.
2:46 So we're gonna be working with these SQLAlchemy classes, and we can query those. And the way that we generally work with them,
2:53 if we go back here notice we have this package service that has some cool methods, like 'Find Package By Name', 'Get the latest releases count',
3:02 'How many packages There are' 'How many releases are there for a given package' And then just give us all the packages.
3:10 We can check this query out really quick. We're using type hints to make this nicer
3:14 Now we're gonna create a DB session, and then we're just going to go and do a query So this is standard SQLAlchemy
3:20 Create session and you say I want a query the package. I want to filter by the package ID is the name.
3:25 That's the way we set up our models of the ID is the name.
3:28 Then we only want one of those or we want to get 'None' back if there's no match for that package name
3:34 So first is either going to give us the one and only one that matches or it's gonna give nothing back. And that's pretty much it.
3:41 We also probably want to look at the routing really quick because that's gonna be super important.
3:45 So the main start up for the whole app is in this '__init__' here for the package. So this method runs, and It's gonna come in here.
3:52 It's going to set up some things that are included, like the template in language here It's going to set up the SQLAlchemy database.
3:59 And then it's going to set up the URLs that are running our Web app, and that's pretty much it.
4:03 This 'init routing' stuff is gonna be really interesting. And we're going to spend a lot of time in the beginning getting this extended for our CMS.
4:12 So you noticed that we have a static view. We've got some routes, like when you just visit the website, it gives you the home view.
4:18 If you visit '/about', it gives you that For the packages, we have a couple of things. We could go to '/project/package name',
4:26 with or without the little slash on the end and that's going to go to the method that we've been looking at You can also get the releases for a package
4:34 So project, the name that's passed and then releases. And then we have some stuff around 'log in', 'log out', 'site map'. We have those kind of things.
4:45 We don't have it really set up yet, but we're gonna have some admin routes
4:49 where we can basically go and manage our CMS and other stuff behind the scenes,
4:53 and that's about it. Here's the database setup. It's just a simple SQLite database, so you don't have to have a server.
4:59 Just really keep it as simple as possible You can change this bit here to talk to real databases like Postgres, MongoDB or whatever it is
5:07 All right, well, that's a whirlwind tour of our Web application that we're going to be running, and you can see right here it looks just like PyPI.
5:17 not exactly, It doesn't have to be 100% clone, but good enough that we have this idea of Here's our main page. Here's our data driven list of packages.
5:25 Here's the details for the packages. This is the part we've been exploring just now. That's how our web app works.
5:31 And this is what we're gonna use is our starting point to build from in this course