Routes collection mapped controller

5 views
Skip to first unread message

waugust

unread,
Sep 10, 2010, 6:41:14 PM9/10/10
to pylons-discuss
This is driving me nuts..
I have a route mapped as an extra collection on a resource:
maps.resource('post', 'posts', collection={'json':'GET'})

The controller action:
@jsonify
def json(self):
q = sa.query(Post)
count = q.count()
posts = q.all()

items = []
for post in posts:
_post =
{'id':post.id,'caption':post.title,'description':post.content}
items.append(_post)

postsJson = {'posts': count,'items':items}

return postsJson

Returns null... the code in a shell returns the structure fine (the
data set isn't empty).

What makes this even stranger is that if I were to map this as a
member, and pass it an Id - I get a response as expected (even though
i didn't change the code, other then adding the id parameter).

Anybody know what's going on?
When trying app.get(url('json_posts')) in the shell I get "<Response
200 OK ''>" (null).
I also get that same response if I put an invalid url in there
(instead of a 404)...
The url('json_posts') resolves correctly and paster routes reports it
appropriately as well.

Wyatt Baldwin

unread,
Sep 11, 2010, 8:45:46 PM9/11/10
to pylons-discuss
I'm not really sure what's going on there, but when you use
map.resource, you can do this instead of adding a special `json`
action:

GET /post/1.json

Then 'json' will be passed as the `format` arg to the appropriate REST
action. In this example:

def show(self, id, format='html'):
# id is '1' and format is 'json'
# ...
if format == 'json':
# convert member to json and return it

Wojtek Augustynski

unread,
Sep 11, 2010, 8:55:55 PM9/11/10
to pylons-...@googlegroups.com
Thanks, for the response. So doing it that way (other then the decorator way) i just change the mime type and use simplejson.dumps? 


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


Wyatt Baldwin

unread,
Sep 11, 2010, 10:22:59 PM9/11/10
to pylons-discuss
> On Sat, Sep 11, 2010 at 5:45 PM, Wyatt Baldwin
> <wyatt.lee.bald...@gmail.com>wrote:
>
> > I'm not really sure what's going on there, but when you use
> > map.resource, you can do this instead of adding a special `json`
> > action:
>
> >    GET /post/1.json
>
> > Then 'json' will be passed as the `format` arg to the appropriate REST
> > action. In this example:
>
> >    def show(self, id, format='html'):
> >        # id is '1' and format is 'json'
> >        # ...
> >        if format == 'json':
> >            # convert member to json and return it
>
>
On Sep 11, 5:55 pm, Wojtek Augustynski <waugustyn...@gmail.com> wrote:
> Thanks, for the response. So doing it that way (other then the decorator
> way) i just change the mime type and use simplejson.dumps?

Well, there are various approaches you could take, but I'd add another
method that specifically handles returning JSON responses:

def show(self, id, format='html'):
entity = Session.query(Entity).get(id)
if format == 'json':
return self._render_json(entity)

@jsonify
def _render_json(self, entity):
# I'd put this in a controller base class
# convert `entity` to some kind of object that can be
JSONified, probably a dict
return object_that_can_be_jsonified

Note, this just shows the general approach I would take. For example,
this doesn't deal with collections. If you want to see what I
*actually* do, look here and check out the various `_render` methods:

http://code.google.com/p/restler/source/browse/restler/controller.py
Reply all
Reply to author
Forward
0 new messages