Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Fallback "accept" in @view_config
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Mattias  
View profile  
 More options Aug 14 2012, 9:35 am
From: Mattias <mattias.gyllsdo...@gmail.com>
Date: Tue, 14 Aug 2012 06:35:19 -0700 (PDT)
Local: Tues, Aug 14 2012 9:35 am
Subject: Fallback "accept" in @view_config

I have a page that is supposed to answer with json if the caller sends
"Accept: application/json" and with a html page if the accept header is
anything else.

It sound like I should be able to do something like below according to the
documentation<http://docs.pylonsproject.org/projects/pyramid/en/1.3-branch/api/conf...> but
I can't get it to work.
    @view_config(route_name='overview',
                 accept='*/*',
                 renderer='overview.mak')
    @view_config(route_name='overview',
                 accept='application/json',
                 renderer='grid-data')
    def overview(self):
        return dict(grid=OverviewGrid(self.request))

If I do
     @view_config(route_name='overview',
                 accept='*/*',
                 renderer='overview.mak')
    def overview(self):
        return dict(grid=OverviewGrid(self.request))
Pyramid/Webob, acceptparse:322 throws a exception with the message "The
application should offer specific types, got */*" so it sound like the
documentation is incorrect.

I also tried
    @view_config(route_name='overview',
                 renderer='overview.mak')
    @view_config(route_name='overview',
                 accept='application/json',
                 renderer='grid-data')
    def overview(self):
        return dict(grid=OverviewGrid(self.request))
But then the only match I get is application/json.

This almost does what I want.
    @view_config(route_name='overview',
                 accept="text/html",
                 renderer='overview.mak')
    @view_config(route_name='overview',
                 accept='application/json',
                 renderer='grid-data')
    def overview(self):
        return dict(grid=OverviewGrid(self.request))
This hits every normal webbrowser except for IE8+ which sends "Accept:
image/gif, image/jpeg, image/pjpeg, image/pjpeg,
application/x-shockwave-flash, */*".

I have tried with both Chrome
("Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
and IE8 ("Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg,
application/x-shockwave-flash, */*") and I can't get it to work.

I took a peek at the Pyramid code and at line 556 in config/views.py
anything with accept == None or with a '*' in it gets added to the
self.view list while anything with something else in the accept parameter
gets added to the self.accepts list.

Later on when get_views gets called it creates a list using the
self.accepts list in the order of the best match. Afterwards it adds the
self.views list to the end of the list and returns that, the code then
tries to call each view in the list until it succeeds.

This means that as long as there is any other view_config with a accept
parameter with the same route_name and if the webbrowser send '*/*' in the
accept list then there is no way to have a general accept='*/*' view since
'*/*' gets filtered by the code and best_match always returns the other
view_config with a accept parameter.

If I change pyramid/config/views.py:559 from "if accept is None or '*' in
accept:" to "if accept is None or '*' == accept:" and add a "return"
in WebOb/acceptparse.py:320 then everything works as I expected it to.

Did I miss something or is there currently no way to do what I want without
changing both Pyramid and WebOb?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gael Pasgrimaud  
View profile  
 More options Aug 14 2012, 9:49 am
From: Gael Pasgrimaud <g...@gawel.org>
Date: Tue, 14 Aug 2012 15:49:07 +0200
Local: Tues, Aug 14 2012 9:49 am
Subject: Re: Fallback "accept" in @view_config
On 14/08/2012 15:35, Mattias wrote:

What about:

     @view_config(route_name='overview',
                  accept='application/json',
                  renderer='grid-data')
     @view_config(route_name='overview',
                  renderer='overview.mak')
     def overview(self):
         return dict(grid=OverviewGrid(self.request))

I guess that if the first one doesn't match then the second is used.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mattias  
View profile  
 More options Aug 14 2012, 10:00 am
From: Mattias <mattias.gyllsdo...@gmail.com>
Date: Tue, 14 Aug 2012 07:00:38 -0700 (PDT)
Local: Tues, Aug 14 2012 10:00 am
Subject: Re: Fallback "accept" in @view_config

Unfortunately WebOb matches "application/json" with the last "*/*" of the
browsers accept string. I wonder if I could use
@view_config(route_name="foo" accept="a") or something that always would
end up as the best match of '*/*'. I will have to try that.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mattias  
View profile  
 More options Aug 14 2012, 10:04 am
From: Mattias <mattias.gyllsdo...@gmail.com>
Date: Tue, 14 Aug 2012 07:04:52 -0700 (PDT)
Local: Tues, Aug 14 2012 10:04 am
Subject: Re: Fallback "accept" in @view_config

Thanks, it worked. It looks ugly but at least it works. I will have to run
some test on it to see if the sort order is stable...

     @view_config(route_name='overview',
                  accept='application/json',
                  renderer='grid-data')
     @view_config(route_name='overview',
                  accept="a",
                  renderer='overview.mak')
     def overview(self):
         return dict(grid=OverviewGrid(self.request))


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »