Hi,

I've been tinkering with Pyramid code as I learn the framework and I'd
appreciate some feedback on a possible enhancement, even if it's just
to tell me it's a bad idea.

One nit that bothered me was the way rendered views require you to set
attributes in the request object in order to change the response's
attributes like status, headers, etc. This is described in "Varying
Attributes of Rendered Responses" [1].

I have experimental code in github [2] that provides alternative ways
to set response attributes in a view. I've included some example usage
fragments below to demonstrate. The code is backwards-compatible so
there shouldn't be any need to change the API or implementations of
existing renderers.

If people think some or all of these approaches are worth pursuing I
can continue refining the code/tests/docs based on feedback.

Cheers,
James


= Alternatives for setting response attributes in a view =

1) Return the new class `pyramid.response.ValueResult` from a view.
This class stores the view's result value along with response
attributes, which can be set on init or later.

value = {'URL': request.URL}
value_result = ValueResult(value, status='403 Forbidden')
value_result.headers.add('X-Forbidden-Reason', 'On-A-Whim')
return value_result

2) Use an updated version of the
`pyramid.renderers.render_to_response` function that accepts response
attribute parameters.

value = {'a': 1}
return render_to_response('abc/def.pt', value,
    request=request,  # request param is *required* for this to work
properly
    content_type='text/plain', cache_for=100)

3) Use a combination of the two (might be useful if you're composing
results from multiple views?)

value_result = ValueResult({'a': 1}, content_type='text/plain')
return render_to_response('abc/def.pt', value_result,
    request=request,  # request param is *required* for this to work
properly
    cache_for=100)


[1]: 
http://docs.pylonsproject.org/projects/pyramid/dev/narr/renderers.html#varying-attributes-of-rendered-responses
[2]: 
https://github.com/jmurty/pyramid/commit/77c04d7171d2e9ae5dbbf8e8a5c5b85e796cc31a

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

Reply via email to