Building Data-Driven Web Apps with Pyramid and SQLAlchemy Transcripts
Chapter: Routing and URLs
Lecture: Route constraints

Login or purchase this course to watch this video and the rest of the course contents.
0:00 What we've seen so far is the basic routing but there's a few advanced use cases that are really, really helpful.
0:06 So, let's go over here for an example. If I come over here and look at the episodes that were recorded
0:12 I could click inside the new PyPI launch, awesome! So actually if you listen to this they'll talk about how they use Pyramid and various things
0:19 to build the thing that we're modeling for a course. That was a lucky click. Notice the URL is /episodes/show. Really wish I'd have put details there
0:29 but that's in the past. And then the number. And then just for a little SEO and helping people make sense of the URL, I put the actual
0:36 sort of friendly name of the episode as well. And this is the main one. But what I would like to do is say for social media and stuff
0:45 let people just say, what is this 160, 159 like, let them just type talkpython.fm/159. And notice that redirects over.
0:58 I'll just go here and just paste it. It redirects back to the right site, the right page. I want to add that functionality over here to our PyPI here.
1:07 This doesn't exist in the real one but I want to be able to go like five fifth most popular package. That's pretty easy to do.
1:15 I could come over here and you could say "Alright, well that just goes in the package thing here." So let's go and just call this "popular".
1:26 And let's just say this is going to be {num} as in 1, 2, so on. Actually, I got to go ahead and add it over here, don't I?
1:34 Ah, make it listen somewhere; let's do this. Now, we're going to show details 'cause it's we're going to reuse this template here
1:49 because it's going to use a different route but then it's going to effectively either just show that one or probably what makes more sense
1:56 is what I do on TalkPython to do a redirect. But let's just go over here, like this.
2:17 So we're going to convert this to an integer and then let's do a little test that says "If the number, if it's not, if it's not between 1 and 10
2:25 then we're not going to show the popular." We're going to say "You're not going to be able to ask for the 100th one."
2:30 I'll just say, "That's how it's working, alright?" So then we'll say, "the whatever-eth popular package." So we'll just drop that in here now
2:46 so we can use the template. Really, we'd go to the database and actually get what one that is going to be. Okay, so, this is going to work, I think.
2:54 But not in the way that you are hoping it's going to work. So, let's go over here, and we'll ask for 5. The fifth most popular package. Awesome!
3:05 Well let's try to go home. That works good. Let's try to go and see projects/requests again. That works good. What about, about?
3:17 Ah, so far we're getting lucky with the order. Let's look over here see if I can find something that'll break. Yeah, let's try to go to account.
3:24 'Member, that worked before. Account cannot be converted to 10. Hm. Well, what's going on here? So, there's a couple of things that work in this thing.
3:39 So, remember what I said is we start at the top and you just go down into one of these matches. Well this says, "I want to look just for
3:46 effectively / a single thing." Well, account is a single thing. So we need a way to tell it, "No, no no." "That needs to be a number."
3:58 What we can do is we can actually add a little bit on here to say, "No, we're going to check that and this is going to be constrained in some way."
4:06 Now, this is not as wonderful as it could be but let's go and do it. So we have custom predicates, is going to be an array. But what we put in here
4:14 is we're going to put any function that given some of the information that's in the route whether or not it's matching this route, this popular route.
4:24 So we'll just create a lambda. It's going to take info and some stuff we don't care about. And then we're going to say, "Go to the info
4:30 which has the match data in it." And we'll say, "Get me the num." And if you don't have that, "Give me the match string there."
4:39 And on the string, I'm going to ask, isdigit(). And we're just going to return that. So now if I rerun it, let's see how it works now.
4:47 First of all, account should not match, alright? Account is not isdigit(). Here we go. So, account goes there.
4:56 But let's see if what we had before, like 5, still works? The fifth most popular package. 8. The first most popular package, first, and so on.
5:09 So now our system is working beautifully and we're able to add that constraint right there. Really, really nice.


Talk Python's Mastodon Michael Kennedy's Mastodon