Adding a CMS to Your Pyramid Web App Transcripts
Chapter: Advanced Markdown templates
Lecture: Custom storage-engine (DB)

Login or purchase this course to watch this video and the rest of the course contents.
0:00 next up to use this with our database, we have to.
0:04 Either we could use it with file system and we got a set that up and give it a path and basically say, Here is your top level directory.
0:11 Much like to say this templates folder is right here for that part of the site.
0:15 Or we have to create a separate one that implements some other way to do things like, for example, the database.
0:20 So we're gonna go over here and we're gonna create a classic drive rooms storage, which comes from a sub package and the one derived from sub template storage.
0:29 And there's a few methods we have to override. So let's just go do that. Well, that's the template storage engine or something like that.
0:39 And the way it works is gonna create a class.
0:41 I copied this from the integration section, but let's just start here, and that's going to be imported from markdown sub template, and it's gonna go like that.
0:50 We say Pass says, Look, this is an abstract based class, and we want to implement all the methods.
0:58 So here's what we have to do in order for this to basically plug in to mark down sub templates. And there's two very similar methods here.
1:06 This is initialized.
1:08 Would it really need to know Is has the underlying database so db session as that thing as that thing been initialized and we go look at it, it has.
1:19 This is initialized property, which is set you may. We should even you can maybe move that down the very, very end of this method here, right?
1:28 But assuming that it's set up the connection and it's gotten all the way through those steps, it's initialized.
1:33 So here we can just say return TV session do the session dot is initialized this one. It's not really anything to do here for this.
1:43 We're just going to ignore that one. Just do a pass and this part gets pretty interesting.
1:48 So here we have to go and write the code just to get one of those pages. And guess what? We already have the CMS service, right?
1:57 And it knows how to get a page on.
1:59 This template path is basically the standing for the euro, so we'll get her page and notice we don't return the page.
2:06 We just return a string so we'll say, if not page in a return. None. Otherwise, we're in turn page Ott contents. Now it says it returning none.
2:18 And this to be optional gonna go with. We're gonna change that optional of stir so it allows us to do that. Perfect. Perfect. Okay.
2:28 And then this one, this one is going to be the same. For now, we're gonna add another feature to mark our pages as being shared or not shared.
2:36 So this will allow us to do a query that slightly different. But for now, when you put this Okay, this is step one.
2:44 Step two, If we go back to the documentation here is we need to go in, create one of these and then set the storage engine or the markdown storage.
2:53 And that's going to happen over here in this section here. So let's add a in it mark down, But configure Guess not. Really? Sure.
3:03 We're gonna need to use it, but that's fine. They wouldn't call this section here.
3:09 We got a storage and say set storage, and we have to give it some store thing that we're gonna create. What is it?
3:16 Well, that's going to be one of these sub template Devi storage things. Just like so and that's technically feels like it's all we got to do.
3:26 All right, Well, we created this class and then we're just going to go here and set it. Think we're gonna run to a problem? Let's see. No, no, no, no.
3:35 Is gay. It's working. Well, maybe this is gonna be great. Let's give it a shot. We come over here and with that it's working.
3:43 However, it's not just taking the mark down and displaying it like it was before.
3:48 In fact, it's doing in memory, cashing in a bunch of different work to make sure that this is as fast as possible.
3:55 Let's go back and make this a super slow, a long thing like it took before we go over here to this engine.
4:03 Yeah, I'm not really sure how we can how we can fake it to go in there. I actually maybe, yeah, yeah, Let's do this. I'll say Page equals that.
4:13 And then page is not what you generally want to do, but equals pays that contents. 20 return page. There we go.
4:21 So now the page will be 20 times as long you go click on this notice right now. Yet this should be a little bit slow. Been spin, spin, spin.
4:31 It was good. Really, really long here. But now watch. Go back. Instant! Instant! We got fast. It is you can you can even see the screen Refresh.
4:40 Now I want to show you some numbers really quick about how it's better.
4:43 But this thing this we haven't talked about this but this cool performance analyzing the bug, Tobar actually makes it hard to see the performance.
4:51 So I'm gonna go and turn that off for a second. That's turned on right there. Let's run it one more time. No, over here.
4:59 Inspect Element turned on the network.
5:01 Say show only HTML and persist the logs across calls The first time I hit it, it's not gonna be cache and memory, so it's going to generate it just like we basically saw before.
5:12 It's gonna take about half a second and there it is. 872 milliseconds. That is some slow business. Compare that to 15 milliseconds for the home page.
5:23 Quick, that is. But watch it now. Bam! Eight milliseconds.
5:28 That's 100 times faster 67 more than 100 times faster to get this page, you can see all that content. Yeah, it's still totally there. All right.
5:37 It's the same amount of work, but this markdown subsystem, one of its magical powers, is it knows how to cash these things and make him really quick and could even cash them to external systems like red ists or do the database or something like that.
5:50 And you could look in the extension section, see how to do that same place where we're looking at for this database integration.
5:57 Okay, so really nice, right?
5:59 All we got to Dio is go over here, implement this one class, this class just more less delegates to our existing CMS service.
6:08 Another win for using that, a p I or that pattern. And yeah, that's pretty much it over here.
6:14 Instead of calling our own convert to mark down, we just let it do it itself.
6:18 Say, give me the page and all the work you got to do to put that together, drop it in here and off it goes.
6:24 And here you can see the actual marked down time that it took was 769 milliseconds. It 00 afterwards, right after it hits it the first time.
6:33 It's super super fast, so this is a huge, huge improvement, and it's basically no extra work on our behalf.
6:39 But it also has more features that we're gonna get to like that shared content and whatever that reusable pieces and so on.


Talk Python's Mastodon Michael Kennedy's Mastodon