Adding a CMS to Your Pyramid Web App Transcripts
Chapter: Redirects and the admin section
Lecture: Demo: Creating the CMS viewmodel

Login or purchase this course to watch this video and the rest of the course contents.
0:00 So here's our a few method, and it's getting kind of long and unwieldy. It's not too bad, but we're not doing that much validation yet.
0:08 You'll see. Like I said, when you're in this edit mode
0:11 where people can type a bunch of stuff in and then it has to be synced back and it has to be validated.
0:15 This could be four or five times long as this right here. This is a more simple case,
0:19 but still it's valuable to explore this idea of view models and clean this up nonetheless So let's do that.
0:25 Over here. Just like we have our controllers organized and our templates organized. We have our view models organized.
0:32 So we're gonna create one for the CMS controller Create a folder called CMS. Now we could just create a new Python file, start typing,
0:40 but I find it much simpler to start from an existing one.
0:43 So I'm just gonna copy this and paste because you'll see there's some stuff that's common all over. So we'll have CMS request view model
0:52 and first thing that I'll do is change this CMS request view model. And all of these derive from the view model base.
1:00 And here you can see that we're passing information that it needs along, and then we're going to set some properties fields. actually over here
1:07 So what is it that we need to work with? Well, we need to have a page, and we need to have a redirect, and that's pretty much it.
1:14 So let's actually copy all of this and come over here and drop it for a second. So can set the sub path like 'self.' that'll store it on a class.
1:26 We can also store the URL. And to get it back here, we got to say, 'self.subpath' and 'self.url'. Now this we have to import
1:34 Luckily, if we hit alt + Enter, PyCharm will just write it up here at the top for us.
1:40 That's nice, and I also need to important this from Pyramid, like so. We don't exactly want it to... well I guess we don't have to do that here.
1:48 We are not going to do that yet. Let's just hang onto the page for a minute
1:51 And the way we wouldn't do that store it on this view model class and same for the redirect.
1:55 Let's go a little bit farther for the redirect. Let's say we have a redirect URL. And the beginning is gonna be 'None'
2:03 but if there's a redirect, it needs to say 'self.' I have self.redirect.url is going to be this.
2:13 So and that's just going to stand in for our little destination thing.
2:17 So we're gonna see if there is a redirect. The url is this because we're going to need to modify it, like so. All right, so what do we get?
2:25 We're gonna go over here and we're gonna create one of these things and give it a request.
2:29 And it will automatically figure out there's a page or there's a redirect. And if there is, it's going to set up the right URL for all that.
2:35 So let's go and get rid of this We're going to create one of these view models. I'll just call it vm. And when that's gonna be CMS requests
2:42 Noticed PyCharm is not helping. If hit ctrl + space, It doesn't help, but if I hit it a second time, it will actually import it automatically here
2:49 and create that so it's pretty awesome. And we got to pass the request through
2:53 If you're unsure, you hit 'cmd + p' or 'ctrl + p' and it'll tell you a request goes there.
2:59 and then it's super easy, we say if vm.page, we want to return one of those responses. So we got to write that again.
3:06 Response body equals whatever we had Like this, I'll just say dot, dot, dot. And we'll say If the VM found a redirect,
3:17 one of those is here, then we're going to return it to the redirect URL like that. Otherwise it's not found.
3:23 So, first of all, this is cleaner than what we had before. We can come over here like this and go to the local history and see the difference.
3:39 You can see there's quite a bit more going on. But what's really nice is that we can take one of these and test it in lots of interesting ways
3:46 without having to go through this whole routing infrastructure or trying to call,you know,
3:50 creating one of these, or basically getting access to calling this route function
3:56 The other thing is, as I said before, if we were accepting Maur user input, we could have as much validation in here is we want
4:04 and then check the few pieces of data that we're interested in and go.
4:07 We'll see that as we work on the admin side of our request redirect backend of the pages and the redirects.
4:16 All right, well, last thing to do is see if this actually worked. We don't need to import that anymore. Let's go and run it and see if our page works.
4:25 But we come up here and we can try our company history Just make sure that the works and yeah, it sure is.
4:31 It doesn't have the body part, but that's fine, because we didn't type that back. And if we go over here and find one that's missing.
4:37 No, we still get our 404 And then finally,if we do our bytes. Like that.
4:43 Yeah, it looks like everything was refactored quickly changed on the inside, but not in its functionality
4:49 Let's just double check that, if we do the bytes with the query string, we still get there.
4:54 Yeah, we sure do over at Pythonbytes.fm slash the right information there.
4:58 Awesome! So now we've refactored the logic of this over to this view model, and now we just check. If there's a page we've got to do a response.
5:06 If there's a redirect, we send the http status code for redirect, otherwise we send them not found.
5:11 That still belongs here but all the other parts about getting the the CMS page or the redirect
5:17 and building the query string URL, all that belongs down in this view model We don't have to think about it in our view method, Love it.


Talk Python's Mastodon Michael Kennedy's Mastodon