xmlrpc and json decorators

11 views
Skip to first unread message

Tito Garrido

unread,
Feb 7, 2009, 5:43:21 PM2/7/09
to web...@googlegroups.com
Hi Folks,

t3 have some useful decorators like @xmrpc and @json do you know if we can use it without t3?

Best Regards,

Tito

--
Linux User #387870
.........____
.... _/_õ|__|
..º[ .-.___.-._| . . . .
.__( o)__( o).:_______

mdipierro

unread,
Feb 7, 2009, 5:59:12 PM2/7/09
to web2py Web Framework
Yes. Just make a model that contains

from gluon.storage import Storage
settings=Storage()
settings.rss_procedures=[]
settings.exposed_procedures=[]
settings.xmlrpc_procedures=[]
settings.json_procedures=[]
def rss(f):
settings.rss_procedures.append(f.__name__)
return f
def expose(f):
settings.exposed_procedures.append(f.__name__)
return f
def json(f):
settings.json_procedures.append(f.__name__)
return f
def xmlrpc(f):
settings.xmlrpc_procedures.append(f.__name__)

and a controller with the following actions:

def rss():
if request.args and request.args[0] in settings.rss_procedures:
feed=eval('%s(*request.args[1:],**dict
(request.vars))'%request.args[0])
else:
t2._error()
import gluon.contrib.rss2 as rss2
rss = rss2.RSS2(
title=feed['title'],
link = feed['link'],
description = feed['description'],
lastBuildDate = feed['created_on'],
items = [
rss2.RSSItem(
title = entry['title'],
link = entry['link'],
description = entry['description'],
pubDate = entry['created_on']) for entry in feed
['entries']]
)
response.headers['Content-Type']='application/rss+xml'
return rss2.dumps(rss)

def run():
if request.args and request.args[0] in
settings.exposed_procedures:
return eval('%s(*request.args[1:],**dict
(request.vars))'%request.args[0])
return 'Not Authorized'

def xmlrpc():
methods=[eval(x) for x in settings.xmlrpc_procedures]
return response.xmlrpc(request,methods)

def json():
if request.args and request.args[0] in settings.json_procedures:
import gluon.contrib.simplejson as sj
return sj.dumps(eval('%s(*request.args[1:],**dict
(request.vars))'%request.args[0]))
return 'Not Authorized'

Massimo

Tito Garrido

unread,
Feb 7, 2009, 6:08:25 PM2/7/09
to web...@googlegroups.com
Thank you Massimo!

It works even without t2?

mdipierro

unread,
Feb 7, 2009, 7:00:20 PM2/7/09
to web2py Web Framework
Yes. No need of T2 or T3.
Reply all
Reply to author
Forward
0 new messages