pyramid_layout icon indicating copy to clipboard operation
pyramid_layout copied to clipboard

pyramid_layout breaks view lookup

Open tonthon opened this issue 7 years ago • 6 comments

When using pyramid_layout with a non default layout, the request_param view predicates is not matched anymore.

Starting with the demo app, add following lines in views.py

@view_config(                                                                    
    route_name='home.mako',                                                    
    request_param='view=html',                                                   
    renderer='demo:templates/home2.mako',                                      
)                                             
@view_config(                                                                    
    route_name='home.jinja2',                                                    
    request_param='view=html',                                                   
    renderer='demo:templates/home2.jinja2',                                      
)                                                                                
def home2(request):                                                              
    return {}  

Add a home2.mako and a home2.jinja2 in the demo/templates directory

<div>It works</div>

Restart the demo server

If you go to http://0.0.0.0:6543/?view=html you get the expected 'It works' text.

If you go to http://0.0.0.0:6543/home.jinja2?view=html You fill on the home page and not on the expected one.

tonthon avatar Jul 04 '17 10:07 tonthon

This sounds like a route matching issue in your code. There is a usage pattern to register routes, as shown in the demo app that might help.

stevepiercy avatar Jul 04 '17 18:07 stevepiercy

As said here above, the issue is easily reproducible starting with the demo app. It has nothing to do with route registration, but with the fact that the layout predicate should not influence the way a view is matched or not (in fact it shoud not be a predicate).

tonthon avatar Jul 05 '17 06:07 tonthon

Here a test repo including the changes listed here above (more information in the README).

https://github.com/tonthon/test-pyramid-layout-33

tonthon avatar Jul 05 '17 07:07 tonthon

This is due to the way that view matching works in Pyramid, and due to the fact that layout is a predicate.

Pyramid matches views based upon some non-predicate information (IIRC permissions/accept are the only two) after that it matches based on longest predicate match:

@view_config(
    route_name='home.mako',
    renderer='demo:templates/home.mako'
    )

Predicates:

  • route_name
@view_config(
    route_name='home.chameleon',
    renderer='demo:templates/home.pt',
    layout='chameleon'
    )

Predicates:

  • route_name
  • layout
@view_config(
    route_name='home.jinja2',
    renderer='demo:templates/home.jinja2',
    layout='jinja2'
    )

Predicates:

  • route_name
  • layout

Then here comes your home2:

@view_config(
    route_name='home.mako',
    request_param='view=html',
    renderer='demo:templates/home2.mako',
)

Predicates:

  • route_name
  • request_param
@view_config(
    route_name='home.jinja2',
    request_param='view=html',
    renderer='demo:templates/home2.jinja2',
)

Predicates:

  • route_name
  • request_param

Pyramid when given:

http://0.0.0.0:6543/jinja2?view=html

Will match in the order that views were registered, so since there are no permissions or accept that are not predicates, we basically ask for all views and start narrowing things down.

  • route_name matches 'jinja2'
  • layout always returns True and thus always matches

The next view that might match would be:

  • route_name matches 'jinja2'
  • request_param has 'view' equal 'html'

However, since both have a predicate length of 2 (longest match wins), it picks the first view found.

You can see that with pviews:

(env) alexandra:test-pyramid-layout-33 xistence$ pviews development.ini /jinja2?view=html

URL = /jinja2?view=html

    context: <pyramid.traversal.DefaultRootFactory object at 0x105344ef0>
    view name: 

    View:
    -----
    demo.views.home
    view predicates (layout = jinja2)

    View:
    -----
    demo.views.home2
    view predicates (request_param view=html)

If instead if we add the layout predicate explicitly to the last view:

@view_config(
    route_name='home.jinja2',
    request_param='view=html',
    renderer='demo:templates/home2.jinja2',
    layout='jinja2'
)

Then it has the following predicates:

  • route_name
  • request_param
  • layout

And thus it wins since it has the longest predicate chain:

(env) alexandra:test-pyramid-layout-33 xistence$ pviews development.ini /jinja2?view=html

URL = /jinja2?view=html

    context: <pyramid.traversal.DefaultRootFactory object at 0x10b0faba8>
    view name: 

    View:
    -----
    demo.views.home2
    view predicates (request_param view=html, layout = jinja2)

    View:
    -----
    demo.views.home
    view predicates (layout = jinja2)

The reason the mako example works, is because the mako view_config only has a single predicate, and thus with view=html the request_param predicate matches and thus it has a longer predicate chain.


tl;dr: pyramid_layout should make layout a view deriver instead, so that it doesn't affect route matching because the predicate is not actually a predicate.

digitalresistor avatar Jul 06 '17 00:07 digitalresistor

any progress on this topic?

s4msung avatar Apr 01 '19 09:04 s4msung

@s4msung nope. Help is wanted. We'd accept a PR with tests, documentation updates, and a signed CONTRIBUTORS.txt.

stevepiercy avatar Apr 01 '19 09:04 stevepiercy