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