Mailing List Archive

Using psycopg directly in Zope Python script
I need to be able to access a Postgresql database directly from Zope
Python scripts (Z SQL Methods is not an option as the SQL code occurs in
modules that must be re-usable in a non-Zope context).

After several attempts at getting the DB-API connection from a Psycopg
connection object created in Zope, I tried this in a Zope Python script:

import psycopg
dbcon = psycopg.connect('host=%s dbname=%s user=%s' % ('dbhost',
'dbname', 'username'))

When run outside Zope, it opens a connection as expected. When run in
Zope, however, I get "thrown out" - the browser asks me to enter my user
name and password again. I get no error message!

Does Zope somehow create a restricted environment where opening a
database connection is impossible? Is there a way to bypass that
restriction?

Ideally I would like to use the DB-API object from a Zope Psycopg
connection object so that it worls within Zope's transactions, but any
help at just getting a working connection in any way will be greatly
appreciated!

--
med venlig hilsen / regards

Jørgen Frøjk Kjærsgaard
Systemudvikler

Metation ApS
systemudvikling * hosting * IT rådgivning

_______________________________________________
Zope-DB mailing list
Zope-DB@zope.org
http://mail.zope.org/mailman/listinfo/zope-db
Re: Using psycopg directly in Zope Python script [ In reply to ]
--On 1. Juli 2005 23:28:51 +0200 Jørgen Frøjk Kjærsgaard
<jfk@metation.com> wrote:

>
>
> Does Zope somehow create a restricted environment where opening a
> database connection is impossible? Is there a way to bypass that
> restriction?


That's a misuse of PythonScripts. They are not designed to handle such
tasks. Use an external method or write a Zope product instead.

-aj
Re: Using psycopg directly in Zope Python script [ In reply to ]
On 2005-07-01 at 23:28:51 [+0200], Jørgen Frøjk Kjærsgaard
<jfk@metation.com> wrote:
> I need to be able to access a Postgresql database directly from Zope
> Python scripts (Z SQL Methods is not an option as the SQL code occurs in
> modules that must be re-usable in a non-Zope context).
>
> After several attempts at getting the DB-API connection from a Psycopg
> connection object created in Zope, I tried this in a Zope Python script:
>
> import psycopg
> dbcon = psycopg.connect('host=%s dbname=%s user=%s' % ('dbhost',
> 'dbname', 'username'))
>
> When run outside Zope, it opens a connection as expected. When run in
> Zope, however, I get "thrown out" - the browser asks me to enter my user
> name and password again. I get no error message!
>
> Does Zope somehow create a restricted environment where opening a
> database connection is impossible? Is there a way to bypass that
> restriction?

Yes, of course it does: it's an application server and the whole point of
database adapters is to make your life easier! You should read the
documentation about PythonScripts

If you need to do some stuff with the database that isn't directly possible
in a ZSQL method then you might want to call the DA from within a
PythonScript or ExternalMethod as Andreas has suggested. Some DA's such as
our mxODBCZopeDA expose additional functionality for use in
ExternalMethods. If you want to work directly with the database outside of
Zope then you will have to use an ExternalMethod but note that this will
put you outside of Zope's transaction mechanism which is *generally* very
useful.

Charlie
--
Charlie Clark
eGenix.com

Professional Python Services directly from the Source
>>> Python/Zope Consulting and Support ... http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
________________________________________________________________________

::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,FreeBSD for free ! ::::
_______________________________________________
Zope-DB mailing list
Zope-DB@zope.org
http://mail.zope.org/mailman/listinfo/zope-db
Re: Using psycopg directly in Zope Python script [ In reply to ]
Jørgen Frøjk Kjærsgaard wrote at 2005-7-1 23:28 +0200:
>I need to be able to access a Postgresql database directly from Zope
>Python scripts (Z SQL Methods is not an option as the SQL code occurs in
>modules that must be re-usable in a non-Zope context).
> ...
>import psycopg
>dbcon = psycopg.connect('host=%s dbname=%s user=%s' % ('dbhost',
>'dbname', 'username'))

You should *NOT* do this because you move outside of Zope's
transaction handling.

Fetch the connection from the DA. It looks somehow like:

db = DA() # low level connection object
db._register() # register with Zope's transaction system
conn = db.db # this is the "psycopg" connection

Pass the "conn" into your code.

> ...
>When run outside Zope, it opens a connection as expected. When run in
>Zope, however, I get "thrown out" - the browser asks me to enter my user
>name and password again. I get no error message!

Cancel the login dialog and you will get the error message :-)

>Does Zope somehow create a restricted environment

PythonScripts are a restricted environment where "import" is
restricted as well as access to attributes.

--
Dieter
_______________________________________________
Zope-DB mailing list
Zope-DB@zope.org
http://mail.zope.org/mailman/listinfo/zope-db
Re: Using psycopg directly in Zope Python script [ In reply to ]
Dieter Maurer wrote:

>Jørgen Frøjk Kjærsgaard wrote at 2005-7-1 23:28 +0200:
>
>
>>I need to be able to access a Postgresql database directly from Zope
>>Python scripts (Z SQL Methods is not an option as the SQL code occurs in
>>modules that must be re-usable in a non-Zope context).
>>...
>>import psycopg
>>dbcon = psycopg.connect('host=%s dbname=%s user=%s' % ('dbhost',
>>'dbname', 'username'))
>>
>>
>
>You should *NOT* do this because you move outside of Zope's
>transaction handling.
>
>Fetch the connection from the DA. It looks somehow like:
>
> db = DA() # low level connection object
> db._register() # register with Zope's transaction system
> conn = db.db # this is the "psycopg" connection
>
>Pass the "conn" into your code.
>
>
Thanks! This is exactly what I would like to do in the end. The code
above was just a first attempt to get started.

>>...
>>When run outside Zope, it opens a connection as expected. When run in
>>Zope, however, I get "thrown out" - the browser asks me to enter my user
>>name and password again. I get no error message!
>>
>>
>
>Cancel the login dialog and you will get the error message :-)
>
>
Good to know :-)

>>Does Zope somehow create a restricted environment
>>
>>
>
>PythonScripts are a restricted environment where "import" is
>restricted as well as access to attributes.
>
>
I didn't read the Zope book very carefully on that point - I have been
using Python mod_python for years but am new to "advanced" Zope programming.

Another question: When I define an external method to process form data,
I have to do it like this:

def process_form_data(self, name, address, zip, ...):
...

What I would like to do is:

def process_form_data(self, **args):
...

so that I don't have to alter the argument list if I add or remove
fields. However, args always comes out empty, presumeably because Zope
inspects the argument list and passes only those arguments that have a
matching name in the list. Is there any way to tell Zope to pass "the
rest" in **args?

--
med venlig hilsen / regards

Jørgen Frøjk Kjærsgaard
Systemudvikler

Metation ApS
systemudvikling * hosting * IT rådgivning