Routing requested based on presence of query params

24 views
Skip to first unread message

goya bean

unread,
Sep 2, 2011, 1:58:38 PM9/2/11
to pylons-discuss
Hello, to avoid 'None' checking and duplicate request returning code,
I'm trying to use (or abuse) route requests in pyramid by using
parameters in query Strings using the 'request_param=' argument as
seen here:
http://pastebin.com/G2shq75s
is there a way to do what I'm trying to do using routing or view
config?

Thanks,
Gavin

Jason

unread,
Sep 2, 2011, 3:44:09 PM9/2/11
to pylons-...@googlegroups.com
Could you write a custom route predicate (https://docs.pylonsproject.org/projects/pyramid/dev/narr/urldispatch.html#custom-route-predicates) that checks for your requirements?

-- Jason

Michael Merickel

unread,
Sep 2, 2011, 5:37:11 PM9/2/11
to pylons-...@googlegroups.com
``request_method`` is limited to comparison with a single query parameter. To add more complex behavior you will need to use a custom predicate.

Note that you are not required to use 2 routes here... After the pattern is matched for your route, there is view lookup performed based on the *view* predicates. For example:

    config.add_route('readPage', '/SupportMapAdmin/api')

    config.add_view(sortView, route_name='readPage', request_param='sort')

    config.add_view(startAndLimitView, route_name='readPage', request_param='start')
    config.add_view(startAndLimitView, route_name='readPage', request_param='limit')

OR, if this is too verbose, a custom predicate:

    def custom_param_predicate(*params):
        def predicate(context, request):
            return any([param in request.params for param in params])
        return predicate

    config.add_route('readPage', '/SupportMapAdmin/api')

    config.add_view(sortView, route_name='readPage', request_param='sort')

    config.add_view(startAndLimitView, route_name='readPage', custom_predicates=(custom_params_predicate('start', 'limit'),))

It is also possible to do this with route predicates instead of view predicates, but I'm not sure why you would want to necessarily unless the different groups of views set different traversal paths or root factories.

--

Michael

Chris McDonough

unread,
Sep 2, 2011, 5:46:36 PM9/2/11
to pylons-...@googlegroups.com
On Fri, 2011-09-02 at 16:37 -0500, Michael Merickel wrote:
> ``request_method`` is limited to comparison with a single query
> parameter. To add more complex behavior you will need to use a custom
> predicate.

Actually request_method on the trunk accepts more than one, but
request_param, yeah does not.

- C


goya bean

unread,
Sep 6, 2011, 1:44:55 PM9/6/11
to pylons-discuss
Thank you for your responses Jason, Michael, Chris.
I was able to use the responses here to solve my problem.

As Jason pointed out and Michael Illustrated, I was able to use a
custom route predicate to check to make sure the incoming
request_params that I needed were included in any given mapping from
the route to the view config.

Since I wanted to make sure every param was present before, I needed
to change the any() to all(). This was the only change I needed to
apply on the example provided by Michael Merickel

def custom_param_predicate(*params):
def predicate(context, request):
return all([param in request.params for param in params])
return predicate


Also, on Michael's advice, I consolidated my routes and mapped them to
views according to the custom predicate.

Thank you all again for your responses,
-Gavin
Reply all
Reply to author
Forward
0 new messages