[Zope] Some basic questions about Zope development

Robert Carey carey@gsi.org
Fri, 23 Apr 1999 14:52:11 -0400


I am new to both Python and Zope. I have been playing with
Zope for the past few weeks and my company is seriously
considering using it in a project.  So far I am enjoying
both the Python language and the Zope framework, but I am
not sure I am really "getting" the Zope way of doing
things.  In particular, there is a problem I have solved in
two pieces, and I would like to know if the solutions are
legitimate ways of doing these things.

1.  In the first case I wanted to make a couple of queries
to a database and then merge the results using Python.  I
am using gadfly, so I needed to get to the same
in-core representation of the data that Zope is using,
otherwise I would simply have opened a new connection to
the database server.

So I have a gadfly database connection, named "conn", open
in Zope.  I pass it into an external method using the
following DTML:

  <!--#in expr="dump_event_user(conn)"-->

Then, in the external method (with the same name as its
Zope identifier) I extract the gadfly connection from the
Zope DB using the following code:

    def dump_event_user(conn):
        connection = conn().db
        c = connection.cursor()
        c.execute("select * from event_user")
        ...

I know this works, but since db is a data member, not a
method, I don't know if this interface is meant to be
accessible to external methods.  Is it public?  And in
general how can I find this kind of thing out for myself?

2.  My second question is much more general. One of the
things I found really useful early on was the ability to
apply the DTML "in" tag to database queries:

    <!--#in Z_SQL_METHOD_NAME-->
       <!--#var SOME_COLUMN_NAME-->
    <!--#/in-->

However, when I decided to do a database query from within
an external method as described above, I found that I no
longer had as easy access to the metadata - the names of
the columns.  It would be really useful to be able to refer
to the data from DTML as though it had been returned by a Z
SQL Method.  I checked the DTML documentation for the #in
tag, and found that it only directly discussed this feature
in terms of Z SQL Methods and Confera Topics.  It does say
that it can be used on sequences, but there is no example.
So I played around with the Python code and, emulating the
generic example, came up with something like this:

    class myobj:
        def __init__(self, n, p):
            self.n = n
            self.p = p

        def name(self):
            return self.n

        def phone(self):
            return self.p

    def test_sequence():
        ret = []
        ret.append(myobj("Tom", "898-8376"))
        ret.append(myobj("Jon",   "616-0349"))
        return ret

Which I found I could call using DTML like this:

  <!--#in test_sequence-->
      <br>name  <!--#var name-->
      <br>phone <!--#var phone-->
  <!--#/in-->

Similarly I found could package up data returned from the
DB as objects and pass them back into Zope this way.

Again I just wanted to confirm that this is the (or a) correct
way of going about this kind of thing.

I hope all this is not covered in some obvious place
that I missed, such as a faq - I have tried to read
as much of the docs as I can, but there is a lot to
plough through, particularly when you are not sure
exactly what you are looking for.

Thanks,

Rob