Jsonify like TG

2 views
Skip to first unread message

Philip Cooper

unread,
Oct 2, 2007, 3:08:34 AM10/2/07
to pylons-...@googlegroups.com
OK,
I wrote up this page http://www.openvest.com/trac/wiki/PylonsJsonify
becuase in moving code from Turbogears to Pylons, I really wanted some
ready made json functionality.

Just thru ti together and haven't given it a through road test yet.

In short: check the link. It only takes a half dozen lines of code and
any pylons controller method can be jsonified, and just by adding a "
json='OK') kwarg to render_response ... no decorator required.

After the fact, I saw Kendall Clarks posting about NOT using the query
string to invoke it. Sorry, practicality overcame me.

Any other red flags out there as to why this won't work. Seems too good
to pass up. To be honest, that feature in Turbogears is the only thing
that has kept it of interest to me. Now it Pylons all the way!

Write once, use twice. I love it. Anyway, Kendalls remarks were about
using something like #json -- using things after the ? throws it into
the query string and off of uri. Anyway the code changes
render_response matching Ben Banget's comments on the thread
http://groups.google.com/group/pylons-discuss/browse_thread/thread/61d9e672032f9156/403f8d380d1beb8d?lnk=gst&q=json&rnum=1#403f8d380d1beb8d
and can look at headers to decide what to do.

Any other thoughts?

--
Phil Cooper

Christoph Haas

unread,
Oct 2, 2007, 5:42:25 AM10/2/07
to pylons-...@googlegroups.com
On Tue, Oct 02, 2007 at 01:08:34AM -0600, Philip Cooper wrote:
> I wrote up this page http://www.openvest.com/trac/wiki/PylonsJsonify

Quoting:

* The reason is that the return from a controller method is a call to
* render_response.

This isn't quite true. You could send a "Response()" object containing
plain text. And with the current Pylons version 0.9.6 you are free to
return just a string object anyway. Perhaps that makes jsonification
simpler? Or do I get your complaint wrong?

Cheers
Christoph

Philip Cooper

unread,
Oct 2, 2007, 8:54:57 AM10/2/07
to pylons-...@googlegroups.com

True, but if I knew I wanted json, the current decorator is ok. You
are right about sending a Response() object but in practice, you spend
time in your method gathering up or creating data, (into "c" in pylons)
and render that information with the template call. It's just cool to
have all of that effort repurposed into json with just a json="OK")
kwarg added to the return line.


--
Philip J. Cooper CFA

Cezary Statkiewicz

unread,
Oct 2, 2007, 2:07:29 PM10/2/07
to pylons-...@googlegroups.com
On wtorek 02 października 2007, Philip Cooper wrote:

> True, but if I knew I wanted json, the current decorator is ok. You
> are right about sending a Response() object but in practice, you
spend
> time in your method gathering up or creating data, (into "c" in
pylons)
> and render that information with the template call. It's just cool to
> have all of that effort repurposed into json with just a json="OK")
> kwarg added to the return line.

You could achieve this in this way: make method which would return only
data and wrap it with jsonifying method or templating method:

v+
from pylons.decorators import jsonify

class MyController(BaseController):
def __get_data(self, *params):
"""
clean data
"""
data = {'a': 1, 'b': 2}
return data

def action(self, param1, param2):
"""
pushing data into template
"""
c.data = self.__get_data(param1, param2)

return render('/mytemplate.mak')

@jsonify
def json_action(self, param1, param2):
"""
retuning jsonified structure
"""
return self.__get_data(param1, param2)

v-

Best regards,

CS


--
Cezary Statkiewicz - http://thelirium.net
rlu#280280 gg#5223219
jabber://koo...@jabber.wp.pl

Philip Cooper

unread,
Oct 2, 2007, 2:25:04 PM10/2/07
to pylons-...@googlegroups.com
Cezary Statkiewicz at about 10/2/07 12:07 PM said:
> You could achieve this in this way: make method which would return
> only data and wrap it with jsonifying method or templating method:
>
> from pylons.decorators import jsonify
>
> class MyController(BaseController): def __get_data(self, *params):
> """ clean data """ data = {'a': 1, 'b': 2} return data
>
> def action(self, param1, param2): """ pushing data into template
> """ c.data = self.__get_data(param1, param2) return
> render('/mytemplate.mak')
>
> @jsonify def json_action(self, param1, param2): """ retuning
> jsonified structure """ return self.__get_data(param1, param2)
>

Not a bad option. But..

1 This is a little in the direction of creating
3 methods rather than one. you have to plan ahead to use it.
2 It requires two different url's (not a biggie since I have to
modify my query string)
3 this seems to require things in your template like c.data.b
rather than c.b
4 Part of the previous is that existing methods can't be re-purposed.
With my method all current methods can become json methods for free.

Again, maybe "like TG" is not what you want. I just thought it looked
... well ... shiny.

--
Phil


Reply all
Reply to author
Forward
0 new messages