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