Monday, February 13, 2012

Calling remote program on DB2 from Python/Web2py

I never realized the power of stored procedure on DB2 and now I can execute the CL/RPG/QRY program from python/web2py !


1. Create Stored Procedure

Let's say, I have a CL program called MYCLPGM in MYLIB. In CL program, I added libraries, clear files and call RPG program..etc.

Then, I can create stored procedures called MYPRCD as follows.
STRSQL 
CREATE PROCEDURE MYLIB/MYPRCD LANGUAGE CL NOT DETERMINISTIC
CONTAINS SQL EXTERNAL NAME MYLIB/MYCLPGM PARAMETER STYLE  
GENERAL



You can also set up parameters or select different language if you need. I recommend using CL and call whatever you need from there because most of case, your objects are in different libraries and you can add them in CL.


After it's created, you can check the stored procedures information in here
STRSQL 
VIEW ROUTINE
SELECT * FROM SYSROUTINES WHERE SPECIFIC_NAME ='MYPRCD'

VIEW PARAMETERS
SELECT * FROM SYSPARMS


From DB2, you can call the stored procedure like
STRSQL 
CALL MYLIB/MYPRCD

2. Calling from Python
* I assume you already finished my previous post.


>>> import pyodbc
>>> conn = pyodbc.connect('DSN=MYDSN;UID=xxxxx;PWD=xxxxx')
>>> cursor = conn.cursor()
>>> cursor.execute('CALL MYLIB.MYPRCD')
<pyodbc.Cursor object at 0x01E7DA30>
>>> conn.close
>>> conn.close() 
3. Calling from web2py (Tested with Ver 1.99.4)
* I assume you already finished my previous post.

Yes, you can do it using executesql.

Model
-------------------------------------------------------------------
db = DAL('db2://DSN=MYDSN;UID=xxxxx;PWD=xxxxx', migrate_enabled=False)
-------------------------------------------------------------------

Controller
-------------------------------------------------------------------
def index():
    form=SQLFORM.factory()
    if form.accepts(request):
        db.executesql('CALL MYLIB.MYPRCD')
        response.flash = 'Stored Executed !!'
    return dict(form=form)
-------------------------------------------------------------------

View
-------------------------------------------------------------------
{{extend 'layout.html'}}

<h3>Run stored procedure</h3>
<hr>
{{=form}}
-------------------------------------------------------------------





Friday, January 13, 2012

web2py: plugin rating widget

I was looking for a plugin for rating with starts and found this one but it doesn't give me much information how to do it so I gave up. (It's sad, I couldn't plug the plugin.)

Plugin Rating
http://www.web2py.com/plugins/default/rating

Now, kenji (s-cubism) just introduced the new plugin and it works like a charm ! It's easy to use and I strongly recommend if you're looking for the plugin.

Rating Widget
http://dev.s-cubism.com/plugin_rating_widget 

Here's how I did.

1. Create new app called "rating"

2. Create models/rating.py
# coding: utf8
from plugin_rating_widget import RatingWidget
db.define_table('product',
    Field('rating', 'integer',
          requires=IS_IN_SET(range(1,6)), # "requires" is necessary for the rating widget
))
################################ The core ######################################
# Inject the horizontal radio widget
db.product.rating.widget = RatingWidget()
################################################################################

3.  Edit controllers/default.py for def index

def index():
    form = SQLFORM(db.product)
    if form.accepts(request.vars, session):
        session.flash = 'submitted %s' % form.vars
        redirect(URL('index'))
    return dict(form=form)
4. Edit views/default.html

{{left_sidebar_enabled,right_sidebar_enabled=False,True}}
{{extend 'layout.html'}}
{{=form}}
5. Result

Of course, you can create another table and store the submitted value to it, calculate the average, whatever you like. This plugin is good enough for me to build some kind of user review app !




Tuesday, December 27, 2011

web2py: new appliances web site

Oh, my app I developed for final project for Massimo's class is now available on new appliances site.

Restaurants Listing

Tuesday, December 20, 2011

web2py: social gathering in Chicago


We had a social gathering at Exchequer Restaurant & Pub which is very close to DePaul Unitversity.


http://www.exchequerpub.com/

226 S. Wabash Avenue
Chicago, IL 60604
Phone: (312) 939-5633

It was fun meeting with the small local group and let's do it again !
I missed the train at 9:40, waited until 10:40 then got home almost 12:00... feel so sleepy today.






Monday, November 21, 2011

web2py: hide register link on page

The following code will hide the link but also disable the function to register user completely even you create the record from appadmin menu.

auth.settings.actions_disabled=['register']

So here's how I do...

# all we need is login
auth.settings.actions_disabled=['change_password','request_reset_password','retrieve_username','profile']
if request.controller != 'appadmin':
    auth.settings.actions_disabled +=['register']

In this way, it's disabled for users but not admin.

web2py: add additonal field in auth_user

It's actually very easy. Just add the following code in your model before auth.define_tables().



## additonal fields
auth.settings.extra_fields['auth_user']= [
  Field('office')
  ]

So it will be like this...

from gluon.tools import Auth, Crud, Service, PluginManager, prettydate
auth = Auth(db, hmac_key=Auth.get_or_create_key())
crud, service, plugins = Crud(db), Service(), PluginManager()

## create all tables needed by auth if not custom tables
## additonal fields
auth.settings.extra_fields['auth_user']= [
  Field('office')
  ]
auth.define_tables()