Building Data-Driven Web Apps with Pyramid and SQLAlchemy Transcripts
Chapter: Using SQLAchemy
Lecture: Package details data

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