Re: Pyramid, traversal, and file-extensions -- not possible?

2011-06-11 Thread Michael Merickel
Using traversal you'll need to define your own __getitem__ on your
context objects that is capable of discriminating your leaf nodes.

With regards to the SO question, the pyramid way of dealing with this
is much more flexible than the pylons mechanism of an if within the
view code. For example you can add a custom view predicate which will
point the view lookup at the same (or different) view with a different
renderer.

def json_fmt(context, request):
if request.matchdict['ext'] == 'json': return True
return False

config.add_route('test', '/test')
config.add_route('test_fmt', '/test.{ext}')

config.add_view(route_name='test', view='ms.views.test',
renderer='test.mako')
config.add_view(route_name='test_fmt', view='ms.views.test',
custom_predicates=(json_fmt,), renderer='json')

# ms/views.py
def test(request):
return {
'key': 'value',
}

The returned dictionary will either be passed to the mako template, or
rendered as json depending on which view matched leaving you to focus
on views, letting pyramid handle rendering it for you.

Michael

-- 
You received this message because you are subscribed to the Google Groups 
pylons-discuss group.
To post to this group, send email to pylons-discuss@googlegroups.com.
To unsubscribe from this group, send email to 
pylons-discuss+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en.



Re: Pyramid, traversal, and file-extensions -- not possible?

2011-06-11 Thread Michael Merickel
On Jun 11, 3:02 pm, Matt Feifarek matt.feifa...@gmail.com wrote:
 Sure, but we're not using traversal anymore now... we're hand-poking all of
 this stuff. I like your solution, in a non-traversal situation.

Sorry, I gave this example because it's a better solution to the SO
solution that you linked to, so I figured you were interested in how
to do it with dispatch as well.

 Can I keep traversal, keep using simple @view_config decorators, and have
 file-extensions? Seems like no.

How you use views with traversal is completely dependent on your
resource tree. So assuming you have a tree for /users/matt and you
want to handle a view named status off of the User('matt'), you could
simply add a view for 'status' and a view for 'status.json':

@view_config(name='status', context=User, renderer='status.mako')
@view_config(name='status.json', context=User, renderer='json')
def status_view(request):
user = request.context
return {
'key': 'value', # passed to the matched renderer
}

At least in the brief moment I've pondered this, you will have a
problem matching a view in the case where there is no name. For
example matching '/users/matt.json', as the User.__getitem__ will have
to take care of that. You can probably argue that handling this url
with that format wouldn't make sense anyway, however.

Michael

-- 
You received this message because you are subscribed to the Google Groups 
pylons-discuss group.
To post to this group, send email to pylons-discuss@googlegroups.com.
To unsubscribe from this group, send email to 
pylons-discuss+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en.