Full Web Apps with FastAPI Transcripts
Chapter: View models and services
Lecture: Create the package and release classes

Login or purchase this course to watch this video and the rest of the course contents.
0:00 I think it's probably about time to create a class for our package. Eventually, this is gonna to come out of the database using SQLAlchemy.
0:08 But for the moment, let's just get sort of started down. Cause you saw when you looked over here and we reviewed this.
0:14 There's a lot of stuff, like a package has an id, and it has a summary and there's one more thing down in a home page and
0:22 so on. So what we want to do is take all those things and put them into a class that ultimately we're gonna map into the database.
0:28 So let's make a new folder. I'll call this data, something like that. Alright, ultimately this. Like I said,
0:43 it's gonna be a SQLAlchemy model, along with many other things. But for now, it's just gonna be a simple class. For now, when we create it,
0:54 we want to just pass over this name. Here we go, we won't add any validation or anything like that.
0:59 We just need enough going on here so that this works. So I need to pass over the package name,
1:06 which also turns out to be the id, gonna use that in the database for the id. And this now, we can say is going to return an Optional[Package].
1:24 OK, so we're gonna just return an empty one for now. Just return None, were gonna get this working in a second,
1:29 but let's go and finish filling this out so that it will work with our details. Let's just go through and see what we need.
1:35 We need an id, we need a summary. Here's the id again for just the URL, home_page.
1:54 And I need a license like, is it MIT, BSD or whatever. author_name, here's a bunch of maintainers. So it's going to need maintainers.
2:11 I'll just make that an empty list, license, I think we've got that already, yes license again. Description and that's gonna be it
2:19 I think, perfect. The short description and a long description. OK, so let's say we also need to pass over a couple more things here
2:28 I need a summary, description. I'll just knock these out and we'll assign them. All right, we've got our package created. Now,
2:42 there's one other thing we're gonna need to do to make this page work. Looking here, a little bit simpler, but we do have more information.
2:50 We have a latest_release, which has a created_date. I think that that might be all that the release has.
2:58 But for the same reason, let's go and create a class here, call it release. We'll add this latest_release over here.
3:12 This is a datetime.datetime, perfect. And it's gonna have other things as well.
3:21 Let's go ahead and say it has a version, it's a string like "1.0.0" or so on. Great, so that will be what we can add over there as well.
3:31 Now back to our view model. And let's just make sure that we're passing enough details over. Again, there's a lot going on here so we'll have self,
3:39 and we have the latest_release. I will just call this. Actually, this was created_date, I believe. The variable was called latest_release.
3:51 Perfect. It's gonna be a release. We'll just make up some fake data for a minute. What do we need? let's say "1.2.0"
3:59 and this will be datetime.datetime.now and for our get_latest_package_by_id, let's put it in here and just have it create package, and put some
4:10 info into it. So what we're gonna need? we're gonna need package_name, that's what we're passing over. Summary will be "This is the summary",
4:20 "Full details here!". And for the home page let's just put, passing over FastAPI, obviously this is hard coding it,
4:27 but we got a whole bunch of data coming along soon. The author will be Sebastian. Here we go. Sebastian Ramirez, and the maintainers can be empty.
4:36 All right, that's gonna be our package, and we'll just return that. Again, this is gonna come from the database,
4:41 but for now, we're just gonna return some fake data. And while we're doing this, let's also, one more thing,
4:46 let's have this package_service.get_latest_release_for_package and let PyCharm add that final function, which is going to get a string
5:00 and it's gonna return this. It's an Optional[Release]. Maybe they're asking for the release of a package that doesn't exist or the package doesn't
5:07 have a release yet. Something like that. Here we go. I think we might be good. Let's go and run this and see what happens.
5:13 There's still gonna be one or two little pieces. We need to put in. The moment of truth. What happens if I click this? Crash!
5:20 I'm sure there's a NameError somewhere. Let's go find it. And latest_version.
5:24 So latest_version we'll be. There's a few little things like this that we gotta
5:28 figure out, let's just go "0.0.0" and if there is a package and there's a release, then we'll have to leave. But if there is one, latest_version,
5:39 latest_release.version, I'm gonna say that. For the moment, let's just say self.is_latest is True.
5:46 We're going to come back and we'll be able to check what release they've asked for and whether or not that is the latest. Down below,
5:59 Yes! we've done it. We've definitely done it. OK, so there's a couple of things we're gonna need to do to make this work a little bit better,
6:08 but we're quite close. So here, like if we go to the project's Homepage. Notice, it takes us to FastAPI. Here's the description, here's FastAPI
6:16 for the name. The license is MIT, the author is Sebastian Ramirez. Here's a little summary. One thing that's missing
6:24 though, is there's an extra CSS file for this page. Come over here to static and you see this package? We need to use this. Well,
6:34 where does it go? Remember our layouts and our shared look, we had this define-slot additional-css that goes right there. This is that structured way.
6:45 This is the location where additional CSS goes. Not before site, after site, for example. So we're just gonna go down to our details,
6:53 and if we're doing any of those things, we're not. Let's close this up, a little easier to read. So what we do is we just put a <div> and we say
7:04 fill-slot and we can omit the tag. And then what do we want to put here?
7:08 We just put a link to /static/css/package.css. Format it a little bit, run it again. And tada! There we go, We have all the final designs brought in.
7:21 If we do a View Page Source, notice the indentation. There's not much we can do about,
7:26 but right in the location we expected, there is now a package.css included. I love that, and layout that we got working here.
7:34 So this is really, really nice. Now we're using fake data. Again, we're about to get to the database,
7:39 but this is really nice. What we've got going on here. We've got our home page, so let's go back home, we got our list here.
7:46 Click on uvicorn, and of course, it's not using any of that data other than just that little bit right there.
7:53 Still, pretty cool, right? About close to a functioning website.


Talk Python's Mastodon Michael Kennedy's Mastodon