Ir al contenido principal

Ralsina.Me — El sitio web de Roberto Alsina

Pyjamas y Juno

Así que, que miérco­le­s, apren­da­mos al­go mien­tras tan­to. Las víc­ti­ma­s: Py­ja­mas y Juno.

Py­ja­mas es una ver­sión Py­thon de GWT y es una ben­di­ció­n: fi­nal­men­te pue­do es­cri­bir HT­ML y JS en py­thon ;-)

Y Juno es re­fres­cante­men­te sim­ple. Ya que to­do el fron­tend es­tá he­cho en Py­ja­ma­s, lo úni­co que ne­ce­si­to es una ma­ne­ra de rou­tear lla­ma­das JSONRPC a có­di­go py­thon y ope­rar en el ba­cken­d.

Así que acá hay dos frag­men­tos úti­le­s:

# This decorator decodes JSONRPC arguments as sent by Pyjamas to
# Juno, and calls the target function with the decoded version.
def JSONRemote(target):
    def wrapper(web,*args,**kwargs):
        postdata=web.input().keys()[0]
        data = json.loads(postdata)
        id, method, params = data["id"],data["method"],[web,]+data["params"][1]
      kwargs['method']=method
        return target(*params,**kwargs)
    return wrapper

usan­do es­to, cual­quier me­to­do Juno fun­cio­na co­mo mé­to­do JSONRP­C!

Ejem­plo:

@route('/user')
@JSONRemote
def list(web,startwith='*',method=None):
    try:
        result=search(filter='(&(uid=*@bigclient.ar)(sn=%s*))'%startwith)
        resp=JSONResponse(result)
    except ldap.LDAPError, e:
        resp=JSONResponse(None,e.desc)
    return resp

¿Ven? Na­da de de­co­di­fi­car JSO­N. Y no co­di­fi­car tam­po­co, por­que creo la res­pues­ta usan­do es­to:

# This class creates a correct JSON response from Juno as required by Pyjamas
class JSONResponse(JunoResponse):
    def __init__(self,result=None,error=None,id=None,code=200):
        JunoResponse.__init__(self)
        self.headers['Content-Type']='text/javascript'
        self.append(json.dumps({'result':result,'error':error,'id':id,'code':code}))

Pro­ba­ble­men­te no sea una gran im­ple­men­ta­ción pe­ro me sir­ve por aho­ra.

Ibn Saeed / 2009-04-26 22:39:

Hello

Just wanted to highlight the meaning of Pyjama

=========================
Pajamas, also spelled pyjamas can refer to several related types of clothing. The original paijama are loose, lightweight trousers fitted with drawstring waistbands and worn in South and West Asia by both sexes.[1] In many English-speaking nations, pajamas are loose-fitting, two-piece garments derived from the original garment and worn chiefly for sleeping,[2] but sometimes also for lounging,[3] also by both sexes.[4] More generally, pajamas may refer to several garments, for both daywear and nightwear, derived from traditional pyjamas and involving variations of style and material.
========================

http://en.wikipedia.org/wik...


Contents © 2000-2020 Roberto Alsina