Adding a CMS to Your Pyramid Web App Transcripts
Chapter: Saving content to a database
Lecture: Saving a page to the DB
0:00 we saw it was pretty seamless to move from redirects being dictionaries under this fake data place over to actually storing redirects in the database.
0:09 In fact, it was kind of as amazingly easy, which I think is is great.
0:14 It speaks to the patterns that we're using as well as to the power of sequel alchemy. Well, it's the time to do the same thing for pages.
0:21 So we're gonna go over here and have an optional page that we want to return from here, which is the class that we wrote.
0:28 It's gonna be really similar to work on these five methods as well.
0:32 So again, we're gonna say session equals db session not create, and then session dot clothes. And I decided I do kind of like that.
0:43 Try finally pattern, cause it lets us write things like this Instead of getting the page and returning it down here, we could just say return whatever this will save return session.
0:55 Not queried, not of redirect but off page filter Where the page out? What are we looking for? The euro equals the past in your oral. First has it.
1:08 Now we're getting the page out of the database. Isn't that ridiculous? All right, so let's do the same thing here.
1:14 We're going to get all of them, create session, want to close it, and instead of saying first or even instead of doing a filter, we're gonna dio and order by pages Do created date dot Sending again and let's say not all.
1:30 So we're sure that we get them back. When were we doing up here? In our list dot Aulas.
1:37 Well, just to be sure looks like it was working, but nonetheless, it's always good to be explicit.
1:43 Okay, so here we're getting all of the pages or getting a single page. This one is gonna be really similar to the one that we had were getting by euro.
1:53 But instead of Korean by Euro, we're just going to say I d is I d and must be explicit about the types appeared. That's not gonna be an integer.
2:04 And it's gonna return an optional page. Cool. Now an update.
2:10 Remember, we got to get the page, but we can't use this one cause the sessions close so we won't be able to commit it back When I say get the page is like this And if there's not a page or not the URL we'll get out of here.
2:29 So I will say you are l You were great. And then and go basically do this part on the inside.
2:42 Laura Casey or l set the contents to strip set the title of the Strip and we're gonna update it.
2:48 But not like this one to say dot title Got your URL and dot contents and he would just say session dot Commit. We're almost there.
3:02 Now go and copy this. It's gonna be actually similar down here. We want to set all of these things.
3:11 So instead of checking for this, we're gonna trim those back. We're going to create a new page here. Page equals page like that.
3:19 Set the title, and we got a say session dot add page and commit. We're not doing any tests and returning, so we don't really need this.
3:28 Try finally thing Super.
3:31 It looks like we're gonna be in to create a page Now, remember, it's not gonna work exactly right, because in our view, models we still have to update.
3:38 If you think so. Let's just jump over there and fix that up as well over on page.
3:43 Noticed the type hints are already telling us They're already helping us here. So let us find all the little errors, which I just love those.
3:49 So you're l title contents and then page list? No, it doesn't look like it matters.
3:55 It does look like we can clean that up a little, but nothing really to change there.
3:59 And then, yeah, I think, Hey, we might be in good shape already, other than we don't need some of these things. Super.
4:06 Well, I looks like we've updated our CMS toe work with the database for pages instead of our fake data.