Adding a CMS to Your Pyramid Web App Transcripts
Chapter: Appendix: Using SQLAchemy
Lecture: Package details data
0:00 We have our design in place but we don't really have the data being passed. Let's go fix that, so if we go over to our controller
0:08 here we're just passing package name. But we actually have to pass a whole bunch of other stuff. So let's start by treating this for real
0:16 and let's go over here and say package. It's going to be package_service, and find_package_by_name. Alright well that doesn't exist, does it?
0:27 So PyCharm can write it for us, thank you. And this is going to be a str and it's actually going to return an optional package.
0:36 Optional comes from the typing library. Okay so, what are we going to do? What we always do when talk the database. And then go here.
0:43 And we're create a session, and then we'll be able to return some result of a query. Turns out this is super easy, remember the package name is
0:51 also actually the ID and maybe want to strip off anything like spaces, or make this lowercase but we're just going to just do the query with it.
1:01 Assuming everything comes together, so we'll just say give us a query of package, and then we're going to filter on the package.id == to package_name.
1:14 What do we get if we do this? Well we get one or zero packages of course but what we actually get is a cursor into the database.
1:22 Cause, it has no way of knowing that that means just one so if we want to make sure we get just the one we'll say first.
1:28 So first, or None if it doesn't exist. Instead of returning like a query set thing we're going to get actually either the first package
1:36 or nothing if the name doesn't match, hence the optional. Okay so now instead of checking for this package name
1:44 we'll check to make sure the package is actually found. So there's a few things we're going to have to put here
1:48 and it's going to be a little complicated it's going to get more complicated before it gets better. Okay, so go over here and pass the package
1:55 that's not needed anymore. Now in order for this to actually work we're going to have to, let me point at some pieces here
2:03 we're going to have to come and indicate whether it's the latest version, and if it's not which version it is here is the numerical value
2:10 of the latest version and whether or not that numerical value is the latest, okay? And by the way, if it turns out to not be the latest
2:17 we'll get a different install statement right there, okay? So, for that we got to return the latest version. Now this is the part where it gets messier
2:29 before it gets better. I'm going to show you a way to vastly improve this but it adds another layer on top of things.
2:35 So first of all let's just come up with the details here and then we'll clean it up in another section under validation.
2:41 So we'll save the latest version we're going to default this to zero and we'll say if package.releases, remember this?
2:48 Our releases' navigating that relationship. Now, right now this is going back and doing another database interaction
2:55 which is not terrible but it's not awesome. We'll fix that in a minute. So we'll say r equals, let's call this latest release
3:02 we need the latest version to be set to nothing. An empty sort of nothing value or the real version but we're also going to need, if that exists
3:12 there is a latest release we'll actually need that object. So I'm going to say this is None. I'm going to pass that in a second.
3:19 Alright so the package has releases the latest release is going to be the first one, why? We go over to the package class, notice we're sorting
3:30 descending by the highest version number. Okay so it means the zeroth one is the latest release. Have the latest version text is going to be
3:43 alright pass these along, it's kind of big. So let's wrap it, and we're getting close. We're also going to need to pass the maintainers
3:49 we haven't dealt with that yet. So, I'm just going to pass that as an empty list so it doesn't crash. And the other thing we need to discuss is
3:58 is this the latest release? And I'm just going to say it is for now remember the ability to navigate to different versions.
4:03 We're just going to say is latest is true, for the moment. We don't actually take the versions here under these details exactly but down here
4:15 when we look at the released version. Pull up the details, here we're passing that. So we'll come back to that.
4:21 I think this might be enough, so what are we going to do? Going to go to our super simple query make sure we got something back.
4:27 If we didn't, must not be found. Come up with the various data here's a little default. If we actually do have releases we'll put those here.
4:36 I'm going to return these values. So one more thing we'll need when we get the more complicated bit
4:42 but we're just going to say that our current version we have out there is this, is the latest version. Oh, look at that.
4:49 Alright let's do quick comparison. Real, fake, real, fake. Looks like our, oh our fonts maybe are not the most amazing
5:01 here we might not be bringing those in. But nonetheless, it's close enough for what we're doing. And notice here's our description
5:07 right, if we could transform this restructured text that would be better, but it's not. Here's our project description, our release history.
5:15 I think we might need to put a wrap there. Got the home page. Here's the awscli, here's information on the status
5:24 here's the license, here's the license again. Here's all the extra metadata. So it's looking pretty good, and here our latest version.
5:32 Well, it's good, it is the latest version. We're also missing a little CSS, hold on I'll fix that.
5:40 There we go, now we got that wrapping and everything. Looks a little bit better I just forgot to move a little CSS over.
5:46 Like I said, we're skipping over the design. It's very very similar to before but just needed a few more styles. Alright what do you think?
5:53 Pretty close, again it's not exact like we don't actually have the copy feature but I think it's close enough for
5:59 our little demo app that we're building. Here's the summary that was entered in the database. Here's the package ID, here's the latest release.
6:07 Here's the date of the latest release that's those kind of are why we actually needed the object. And yeah, everything looks really good.
6:15 So I think that's pretty much working. Let's just review it real quick here. In order to do this, all we had to throw in a filter
6:22 and say package.field or column ==. I know Django and Mongoengine have equals here but in SQLAlchemy, you put the equality statement here.
6:33 So you can do greater than and stuff as well. So ID equal equal the value and then to get just one of them first.
6:41 And the rest was just passing the data off to the template.