Adding a CMS to Your Pyramid Web App Transcripts
Chapter: Routing for the CMS
Lecture: The cms_service

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Well, our simple little CMS view method here is fun. We can request anything on the Web site, and it will give us back, you know?
0:08 Hello, CMS, you requested whatever URL you requested. That is not very practical, however, is it? So let's build something better than this.
0:16 Now, if you look over in the services folder subpackage, notice there's one for packages and there's one for users.
0:23 And what is happening over here in these files is either all the requests,
0:28 all the logic around, accessing the thing they describe. So access and users.
0:32 Here's how many users air in the system is creating a new user years validating their password
0:38 Here's logging them in and so on and so on. So let's create something like this for the CMS.
0:43 That'll be where we talk to the database to look or what pages or other things we might want to work with are there.
0:50 So I'm going to add another Python file called cms_service,
0:55 And in the beginning we're going to do two very simple things. We'll have a function, so do def.
1:00 It's gonna be get page, and we'll have the URL here, which is gonna be a string
1:05 and we're just gonna return a dictionary for the beginning. I have that. Now another thing that's really, really helpful that we're gonna have is
1:13 The ability to do redirects to make a request and send somebody somewhere else.
1:18 So I'm gonna go and add get redirect. So if we wanted this to say well, I'd like you go to '/news'.
1:25 and that actually goes, you know, just some complicated URL either on our site or entirely somewhere else.
1:31 So that's where the idea is we can use our CMS for redirecting the other places and keeping track of those requests in addition to just having pages
1:38 Now, if you look over here in this DB section, I've added a little bit of fake data. These pages, they have a title and a contents.
1:46 So I'm just gonna grab this eventually that's gonna go into a database and be more proper. But let's just go over here and start with our fake data.
1:55 So what we can do is we can go to our fake data and has pages, and We can try to get by the URL. Se we'll say the page is this.
2:03 If it's not there is just going to turn nothing. So return the page like so perfect. this one Lets just do 'return None'.
2:10 All right, so let's see if we can get the age to come back over to our CMS controller.
2:16 so instead of doing all of this business, let's go and see the page is going to be CMS service. Import that and say, get page, give it the url.
2:27 I'll say, if not page to raise http, I think it's like this. Not found. perfect. Otherwise, let's put the message of here something better
2:38 We'll, say title, and we don't really have a view, a better way to show them this.
2:42 We'll, say content just just show you that hey something's coming through.
2:47 We'll say 'page.get(title)' 'page.get(contents)' if that's what we really called it. title, contents, Super.
2:58 Oh, and I just noticed that I probably want to change this. The way we're requesting it. this is not gonna work.
3:04 I was assuming that was a dictionary, and it's just a list. So let's put this in as a dictionary where or the key is the URL.
3:13 That way we could just ask give me the value or the URL. We ask for 'company/history'. We get this, we ask for a 'company/employees'. We get this.
3:21 We ask for something else, we get nothing Okay, I think we're gonna be good to go. Let's see what happens now. I go here and I slash Ask for '/ABC'?
3:31 None type has no attribute. Okay, I need to work on that What about 'company/history'? Okay, great.
3:40 Well, uh, I think we did something wrong here. Let's see what's going on. Oh, have it backwards, If the page didn't come back, it's not found
3:51 Let's try this again. But now I should say, not found. And if we have 'company/history'? Here's the company history. Awesome!
4:00 And then, what was it, employees? Yeah, our team. And if we don't have the s, Obviously that's not a page that's specified so doesn't exist.
4:09 Now, we're a long way from making this look nice, but here we're simulating going to the database based on URL were passing along.
4:18 We're either getting nothing or we're getting something back from the database
4:23 and we're rudimentarily communicating that back to the user that says it's not found, or giving them, like a really basic view of the data.
4:31 So we have this kernel, this beginning of our CMS forming right here.

Talk Python's Mastodon Michael Kennedy's Mastodon