.cgi file example for Python and Cognos

In this post we show an example of a Store Procedure call and an execution of a Python sentence from Cognos.

The three parameters are read from Cognos components and the AJAX call is done to a file named file.cgi located in CGIlocation.

The following javascript is executed from the Cognos report:

function funcionJS() {

var parameter1 = Number(getFormWarpRequest()._textEditBoxCOMPONENTE_COGNOS1.value); ;

var parameter2 = getFormWarpRequest()._oLstChoicesCOMPONENTE_COGNOS2.value;

var parameter3 = getFormWarpRequest()._oLstChoicesCOMPONENTE_COGNOS3.value;

$j.ajax({

url: "http://server/CGIlocation/file.cgi",

data: {

parameter1: parameter1,

parameter2: parameter2,

parameter3: parameter3

},

success: function(data){

var data = $j.parseJSON(data);

print data.valor;},

error: onError

});

setTimeout("promptButtonFinish();",500);

}


The .cgi file must contain the following code:

import cx_Oracle,cgitb, cgi, json
cgitb.enable()

print "Status: 200 OK"
print "Content-Type: text/plain;charset=utf-8"
print

arguments = cgi.FieldStorage()

con = cx_Oracle.connect('user/pass@database')
cur = con.cursor();

par1= cur.var(cx_Oracle.STRING)
par1.setvalue(0,(arguments["parameter1"].value))
# Gets the parameter value parameter1 from the AJAX call

par2= cur.var(cx_Oracle.STRING)
par2.setvalue(0,(arguments["parameter2"].value))
# Gets the parameter value parameter2 from the AJAX call

par3= cur.var(cx_Oracle.STRING)
par3.setvalue(0,(arguments["parameter3"].value))
# Gets the parameter value parameter3 from the AJAX call

cur.execute("call PROCEDURE(:p1, :p2)", p1= par1, p2=par2)
cur.execute("SELECT A,B,C FROM TABLE where column1=:p1 and column2=:p3", p1=par1, p3=par3)

response = {"success": True, "message":"Successfully run", "data": cur.fetchall()}

cur.close()
con.close()

print json.dumps(response)