Excellent example, thanks.

- I.

-----Original Message-----
From: Aaron Held [mailto:aaron@;MetroNY.com]
Sent: Wednesday, October 30, 2002 10:48 AM
To: Ian Sparks
Cc: Webware-Discuss (E-mail)
Subject: Re: [Webware-discuss] dbPool usage


in configuration.py :

from MiscUtils.DBPool import DBPool
from pyPgSQL import PgSQL
DEBUG=1
if DEBUG:
     print 'before datapool'
# set up the database connection pool
datapool = DBPool(PgSQL, 5, 'localhost::comsci:user:passwd)
clientpool['testing']=DBPool(PgSQL,2,localhost::testing:user:passwd')
clientpool['metrony']=DBPool(PgSQL,2,'localhost::metrony:user:passwd')
if DEBUG:
     print 'after datapool'

---
in servlet:

from configuration import datapool
.....
        def login(self , username=None, passwd=None):
                conn = datapool.getConnection()
                c = conn.cursor()
                c.execute('''SELECT "eMail" FROM "users" where "eMail"=%(username)s 
AND passwd=%(passwd)s ''',{'username':username,'passwd':passwd})
                r = c.fetchone()
                c.close()
                conn.close()
                if (r):
                        user= self.getUser(username)
                        print 'validated user in CSUserManager'
                        return user
                else:
                        print 'User not logged in'
                        return None

Although I would not call the database from the servlet directly.  Just 
put webware in PYTHONPATH and write a module like:
UserManager.py
#
#  CSUser factory
#

from CSUser import CSUser
from configuration import datapool

class CSUserManager:
.....
        def login(self , username=None, passwd=None):
                conn = datapool.getConnection()
                c = conn.cursor()
                c.execute('''SELECT "eMail" FROM "users" where "eMail"=%(username)s 
AND passwd=%(passwd)s ''',{'username':username,'passwd':passwd})
                r = c.fetchone()
                c.close()
                conn.close()
                if (r):
                        user= self.getUser(username)
                        print 'validated user in CSUserManager'
                        return user
                else:
                        print 'User not logged in'
                        return None
....
if (__name__=="__main__"):
        print 'Start Main'
        UM = CSUserManager()
        user=UM.login('[EMAIL PROTECTED]','vector')
        aaron=UM.login('[EMAIL PROTECTED]','vector')
        print 'Logged in :%s: ' % user.name
        user2=UM.getUser('[EMAIL PROTECTED]')
        print 'UserName= %s' % user2.name
        print 'UserRole= %s' % user2.role
        user=UM.requestUserByName(user,user2.name)
        print '''requested '[EMAIL PROTECTED]' got: ''', user
        user.passwd='adasdasas'
        print user.company
        print UM.requestUserList(aaron,0,'a')
        userList= UM.requestUserList(aaron)
        print 'Total', userList.pop(0)[0]
        print userList
        print 'groups' , user.groups
        print 'notes', user.notes
        user.groups="newnaksdjkasja"
        user.notes='notes'
        user.phone='5551212'
        UM.updateUser(user,user)
        
else:
        UserManager=CSUserManager()

The idea is that if I call the module directly from a python command 
line it will import configuration.py and do some basic unit testing.  If 
I call it as a module (from a servlet) it will put an instance of itself 
in memory.

Then from the servlet you can use the database as follows:

from CSUserManager import UserManager #<-- pull in the data class

class loginUser(FormValidator):
  def validate(self,fieldDict):          
user=UserManager.login(fieldDict['userName'],fieldDict['password'])

-Aaron


Ian Sparks wrote:
> Where would I set up my dbPool or other "application wide" objects in webware?
> 
> Guess : In contextInitialize of __init__.py? in the MyContext directory?
> 
> Do I need to do anything to attach the pool to the application :
> 
> application.dbpool = DBPool(....) ?
> 
> If someone could post a simple example of setup/usage I'd appreciate it.
> 
> Thanks!
> 
> - Ian Sparks.
> 
> 
> -------------------------------------------------------
> This sf.net email is sponsored by:ThinkGeek
> Welcome to geek heaven.
> http://thinkgeek.com/sf
> _______________________________________________
> Webware-discuss mailing list
> [EMAIL PROTECTED]
> https://lists.sourceforge.net/lists/listinfo/webware-discuss




-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
_______________________________________________
Webware-discuss mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/webware-discuss

Reply via email to