RESTful and HTTP APIs in Pyramid Transcripts
Chapter: Validation with view models
Lecture: Improved car object creation
Login or
purchase this course
to watch this video and the rest of the course contents.
0:01
Okay we got our view model working well, let's go back and look at one more thing though that was kind of annoying to me;
0:08
if I put something wrong here, watch this— 500 internal server error, it got an unexpected keyword argument,
0:15
hm, that's not great, where did that come from? It turns out if we switch back to PyCharm, that's right here,
0:22
remember I told you this little trick that we did, this */* dict, this is not great, if the arguments and the dictionary values
0:31
don't line up precisely, this doesn't work well, now you could say Michael, just drop this and do a ** kwargs
0:38
I personally don't like that very much, the reason is if I come over here ant type car like this, it will tell me exactly what I need to pass,
0:45
brand, name, price I can even put type annotations, or type hints on there
0:49
if I put ** kwargs all of a sudden it's entirely opaque, what I need to pass, that always makes me crazy, I'm not a fan of that
0:56
so I don't want to do that, but I also don't want to do this, ok. So let's actually move this car initialization bit out, right
1:05
so for example if I just go and let's just take this out this value instead of x damage, we're taking it damage out,
1:13
if I try it'll say missing positional argument damage, right well you're supposed to pass a damage and you didn't,
1:21
do we really want a 500 error, or would we much rather, I can tend to get a hey you're supposed to pass the damage
1:27
that's a required field, you didn't, right so here you have to like know that the initializer
1:32
missing an argument damage meant you probably didn't send it correctly, so let's go work on that a little bit,
1:37
so we're going to take this, and say you know what, forget this, this is out, an we're going to go put that creation instead of doing this,
1:46
we're just going to put the creation here, say car like that and let's actually switch this, say self.car=this,
1:57
and we could actually push this, we could do this validation here a little bit above, so we could do things like this
2:07
I'll go and write this out and then go back over, so here you can see that we've pulled these values out and then we'll go and put them into the car
2:15
we'll say things like brand, name, price, year, damage last seen image and this one does not take an id but we could go and pass the id, if it's here,
2:32
so we could say something like car id and it's going to come back none like the default value anyway, so we'll pass the car id.
2:42
Now, the problem was, we were trying to call this before we did the validation, so let's do this and put it at the bottom,
2:50
so now we've got the values, we're going to validate it, and we'll say if not self.errors then we'll go ahead and do this,
3:00
maybe we wrap this around so it's not huge, I don't know but only you set this and try to call this function
3:07
if it's all looking as valid as we can tell there is probably more validation, in fact his last seen here
3:14
notice right now it's just coming back like this but we could do something like this if last seen I want to use another module here
3:22
so we're going to say pip install Python dateutil, this has a really nice date parsing function, so let's take this and remember
3:34
any time you use something external it has to go over here, ok, great, so let's go over here, now it should know
3:42
that we could import dateutil.parser.parse of last seen. And that will convert the data, it takes a bunch of different formats and so on.
3:51
Okay, so we could do validation on this if we want to make sure that it's not in the future or reasonable somehow
3:58
so Ii think we've done a lot of work here to make this much nicer function let's go and run it one more time,
4:05
what do we do, we had sent it without the damage now we're not actually checking for damage this time
4:11
we're getting it, but we're using that right here, which returns none if it's not there and for damage that's okay.
4:18
Try this again, datetime, okay, now we've got a new problem this is an interesting problem just with straight serialization
4:28
because we converted this to the correct type of daytime that oddly, is not possible, in json, that's a Python thing
4:38
not anything to do with what we're doing here, so let's go and we could just say well we ’ ll have to do some work here
4:45
but we actually don't, we just need a different adapter, so this will be nice, if we go over here and say look if you see a datetime.datetime
4:53
what I want you to do is go over here and just return the string representation I don't think you actually need that, let's put a d for a date,
5:07
isoformat that's what I want, so we're going to get the isoformat here, let's just try that again;
5:21
Oh, this is a problem here, the problem is that well that's great, this normally would work if we just return the car
5:31
and yeah, we would need to do that here, this two dictionary thing, this one we need to go ahead
5:38
and say the year, last seen, we need to go over here and say isoformat like that there we go, because we weren't actually,
5:49
because we're returning a response object we're not going through the renderer, all right, so here you see now we've got our last seen,
5:55
but we're going to parse it back into a real object when we submit it. Okay and damage came back as null, let's go over here and say
6:02
what if we don't have the price, remember this was crashing and saying actually that may make it worse,
6:08
when we had the x, what that did before was to say oh this is a problem, right, you're trying to pass that to a constructor,
6:17
now I realize I made a bit of a validation mistake, over here, let's look at it again, as I said, if nothing put a zero,
6:24
because I didn't want the int to crash, but let's put negative number so that we have something there,
6:30
alright, let's run it again, now it says, you can't submit your car, the price must be non-negative, so if you don't have a price,
6:36
it's just going to tell you it can't be negative, right, we could check to see if that exists, create a special error message, and so on,
6:42
we can get the same thing for the year, if we take the name out it's going to tell us the name is required, right,
6:48
so let's put all these back the way it was, so now you can see we have a much better validation,
6:55
and we did that by moving it all this extra work down into the view model, let's grab all of these things, and then we're going to validate them,
7:02
and then we're going to use them to create this stuff, if we had left it up here, again, this would like balloon up into a big problem.