Adding a CMS to Your Flask Web App Transcripts
Chapter: The redirects (our bit.ly)
Lecture: Capturing data from the form
0:00 well, we have our form in place to give us the data. The next thing to do is to work on our view model.
0:06 Remember, the View models job is to take the data from the form validated, transform it and then say Yes,
0:13 everything's okay. Here's the data or it's not. And here's the problem. So let's go do that now. So we just copied this. Remember,
0:22 from the all redirects one. Give it a new name, but, Graham, that data that we had submitted over So let's wrap this around
0:31 These are the things. These are the things that we gotta get from the post. So remember, we write things like that.
0:40 Self dot name equals self, not request addict dot Get dictionary. Right. That's where this coming from name.
0:50 And if it's not there, we don't wanna have none. We want to just have it like this. Then we're gonna do the same thing for you.
0:59 Well, and you are l But writing this over and over comm get tiresome. So let's create a little temporary variable here.
1:07 Just call it D. Normally don't like short variable names that are not descriptive, but in this case, I'm gonna get with it.
1:12 We also want to make sure that we don't keep like, the weird white space at the end. If that was, the things will say strip and we want to short you,
1:25 Earl. And this one, we're also gonna make it lower case because we don't wanna have upper case and lower case be a different thing.
1:35 That would be weird. And then finally, let's Graham the I d Call it read Dry, wrecked I d. That was just coming in as I d Now this we don't
1:47 care about stripping, but remember, everything that gets submitted to the website is a string and kind of see that here as well. When it comes back,
1:56 it's gonna be a string as in like if it was a number to be Quote seven, not the value seven. But the database wants riel numbers,
2:04 not strings that contained numbers. So we're gonna use this numb convert library here that has a try to convert to energy.
2:14 And what it does is it tries to parse this as an energy, and if it fails, it returns zero.
2:20 I think there's a way to set the default on default here toe be like whatever it is you want, so it's either going to give us a number back or
2:29 it's going to give us something that is false, which is exactly what we want now. One other thing that's gonna be helpful later is when we're editing.
2:39 We would like to actually have need tohave the redirect data to work with. So let's say self doubt redirect itself is going to be CMS service.
2:49 Don't get not a dictionary. It has the ability to get a redirect. But by your URL not, but I d So that's right by I d and
3:00 how self doubt requested he redirect 80. There we go and we'll go. Let my charm right that perfect.
3:09 This is gonna be an integer and it's gonna return an optional Dix name. Cool. Now, if you look at the data over here the fake data it
3:22 was easy to get stuff by short by the Earl because those were the keys. But we don't have that here.
3:27 We get to real databases that's going to get better. But I gotta write some clumsy code to make this happen.
3:33 It will say for you're a redirect in items like this, we can go and get the redirect and the euro. We don't really care about the girl,
3:49 but the redirect we dio will say if reader act don't get i d Is this redirect idea we're looking for and we returned the redirect.
4:02 Otherwise you return Nothing. Perfect. Now it turns out this is identical to what we're gonna need for pages later. So why we got in our mind?
4:11 Let's just right the pages by ideas Well, and these little re factoring magic page page So they pages. And today either we have the pages one as well.
4:28 Okay, so this is working. We could do a little bit of work here and say this is none.
4:35 Say something like, if self taught redirect i d Then call this function right? That'll mean that in the ad mood,
4:46 we won't be hitting the quote database for nothing. But it's not a huge deal. It's admin section, after all, but it's a little bit better,
4:52 so we'll do it like this. All right, I think we're in pretty good shape. Let's go over here. And now we can show just the data that weaved,
5:07 parsed out, which is a little bit better if we refresh it and resend it you can see we got the data requests and for the things that don't matter
5:20 But here we go. Here's a short your URL the redirect i d. The redirect is none. All of this stuff is coming out of that view model
5:28 so it got it correctly, All right. Last thing to do is we need to know. Did this work? Does it have the right data,
5:38 or should we keep them on the Forum and tell them that there's an air? So let's have a function called VM Don t validate like so if it's not the
5:49 case we're going after, not here. If there's some kind of error, we want to reload the data. 12 Do Nosek and we want to stay on the same page.
5:59 Otherwise, we want you to print would have some like this would have created your own, and then we're gonna take them somewhere.
6:13 This is the redirect. Get post, redirect where we're going to send them. Let's send them back to admin slash redirects.
6:18 They just created this new redirect and then working with a list of redirects. So this will just take them back that list that right at the top,
6:24 they could go edit it, test it, whatever they want. The last thing to do is write this,
6:32 and the idea is we want to make sure that they submitted a name and you are ill and that the redirect i d makes sense and things like that,
6:38 you might say, Well, name is required. Why do we have to test it? They don't have to have their browser obey.
6:45 What are HTML was that might not even be a browser That might be some like script or something. So we need to check them on the server.
6:51 Always both you that. So we'll check to see if the name is there or if it's just empty white space.
7:08 But other than that, it's empty. That's true. Will say you must specify name. We're gonna do the same for your l and short euro.
7:19 Well, those are the three obvious ones, but we also want to check to other cases. If there is a redirect idea,
7:26 better correspond to an existing redirect, and when they're creating a new one,
7:31 we want to make sure that they're not trying to create one where there's already going to be a short girl. That would be a conflict.
7:37 So we don't want that. We want to check those two cases as well. So in this case, there's an I D that makes they passed along.
7:48 But we couldn't find its corresponding redirect. So that's a problem. Okay, and then the other one is the opposite. So there's no I d. And yet,
8:03 so there's no idea that means there's added a new one and yet were able to find a redirect by the short girl.
8:18 So the redirect with the Royal short Girl RV exists. Now all of these needs a indicate and whether or not this was good or bad
8:25 will say false else. It wasn't okay. Not okay. None of these were okay and here will return.
8:31 True. We can be a little more explicit in saying this returns a pool. Okay, I think those are the cases.
8:39 Let's go over here and just check one more time that everything is hanging together. Try to repost this and remember, it's talk is what we're after.
8:52 Yes, So we would have sent talk over to talk by fun. They're so Let's try one more time and get some more validation test.
9:00 This is gonna be bites. Remember, there's already one for bites. So this will be not the bites because it's not
9:05 gonna work at redirect boom. Redirect with your URL bites already exists. Perfect. We could make it high any tiny, bit better if we just put last year.
9:16 So it's clear that that's the euro. All right, well, I think that does it. The one last thing that is annoying is if I repost this all the status here
9:26 is gone so we can fix that super easy Over here, we just say value equals. Pull the data out for name and do that for short your islands on. All right,
9:40 so if we try again, check it out. Test bites, not the bites. Bring and put a bites to this. Should work should redirect us over to the list,
9:51 which is not yet getting populated. But it will soon it will soon. Ready, Set, go. Yes,
9:58 Validation past. So it decided we could go on and you'll see it sent a little message at the bottom device to we're now gonna go here.
10:06 Awesome. So our view model in our data exchange and all of our data validation
10:09 The super simple stuff like name has to exist in the more interesting stuff like you can't duplicate short your ills.
10:16 It's all in place and it's all working.