Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: MongoDB edition
Lecture: Package details cleanup

Login or purchase this course to watch this video and the rest of the course contents.
0:00 We've cleaned up the homepage. Now let's just click on one of these and see how amazing this is. Oh, darn it, not so amazing.
0:06 Remember we had the relationship from release over to package. Well now we're running into the issue but in the other direction.
0:12 We will go into a package and asking for its releases. So we just need to change a little bit how this works in order to match with our new data model.
0:19 So what we need to do is let's close these off and it turns out that the error is actually right here. We can go show you that
0:30 if we just scroll down and click right there. It's this, it says, hey, we don't given a package it doesn't have our releases.
0:37 Well, what are we really trying to do here? We're trying to go to this and say well, if there's a package, go get me its latest release
0:43 so we can set the latest release in the latest version text. So let's just change how this works. Let's say self.latest_release = package_service.
0:52 get_latest_release_for_package( Like so. Now, this function doesn't exist yet but we're going to write in a second.
1:05 Now, let's just fix this up really quick here. So the goal over here was to say, go to the and get us the latest release that's already done
1:13 and then it's here's to say what is the text for that latest one. We probably want to add one more check if there is actually a latest release, okay.
1:23 If we make this change and we write this function this page, this package details page should be back in action. So let's go write this.
1:30 It's going to be package_id, which is a string. What's going to give it? What are we getting back? We're getting an optional of release. Okay.
1:39 We want to do a query on releases, super easy. We go to release, we say objects and we don't need to use filter here.
1:44 We can just put it right inside of object and say package_id equals package_id that we're passing in and we want to do an order by.
1:56 What do you want to order by? Let's go over here. created_date. We want the latest one, remember? So we say, - that and then here we say .first.
2:05 Give us one or None and that's it. That will fix this or go to the database, do a query get us the latest release for our package.
2:13 And here you can see we have an index on created_date and the thing we're sorted on as well as the package_id. So this should be really, really fast.
2:22 All right, let's go and see how this works. Try again. No, what have we missed? Oh we still are doing this test
2:32 even though it doesn't make sense anymore. Try again. Tada, there we go. Our site is fully back in action
2:39 and we can come down here and see all the details here you can see we got our latest release for that one on 2018 in May 30th. Super.
2:48 So we've now done a little tiny cleanup that we had to do based on the change in the structure and the data model
2:54 and now our app is back and working and that's it. We've converted this app over to MongoDB. I've done a lot of talking and not that much typing.
3:01 If I just went and heads down and started converting and converting you can see this would go pretty quick.
3:06 We did come with the MongoDB data model already done so I don't know. What do you think? An hour, two hours to do this conversion.
3:13 That's not a ton of work and why is it not a ton of work? Because we were able to change just these couple of files
3:18 'cause all of the data access was there and because we're using MongoEngine it was very similar in the way we wrote our queries and got objects back
3:27 to the way that we work with sequel alchemy. Super cool on both counts.

Talk Python's Mastodon Michael Kennedy's Mastodon