[ZDP] Partial Zope introduction

A.M. Kuchling akuchlin@mems-exchange.org
Sun, 16 Jan 2000 17:30:53 -0500


For various reasons, I'm going to be dropping plans to add a Zope
chapter to the Python Grimoire.  It's too large a topic for a single
chapter, so I'll leave it to a separate document.

So the partial text doesn't go to waste, here's the introductory
prologue that I have.  Feel free to ignore it, incorporate it into
other documents, and revise it in any way you like.  Credit isn't
required.

-- 
A.M. Kuchling			http://starship.python.net/crew/amk/
I have no skills with machines. I fear them, and because I cannot help
attributing human qualities to them, I suspect that they hate me and will kill
me if they can.
    -- Robertson Davies, "Reading"


\chapter{Zope}

\newcommand{\object}[1]{\code{#1}}

\section{Introduction}

Zope is one of the most exciting new Python-related development
efforts to emerge in some time.  

It's difficult to explain Zope in a sentence, because it's a large
system containing many different components.  It's often considered a Web
application server, because it provides a platform for developing
applications that can be accessed over the Web.  

While Zope was released as free software relatively recently, it's a
mature product whose history can be traced back to 1996.  It started
as a relatively simple module for publishing objects on the Web, and
more features were added over time: the DTML templating language, a
transactional object database, and Web-based management screens.  For
a while it was sold as a commercial product named Principia, until the
decision was made to release the source code.  During its commercial
phase, one important application area for Zope was implementing Web
sites for newspapers.  Newspapers need to have many people adding and
editing their content, but these users aren't usually sophisticated.

The most important components of Zope are:

\begin{itemize}

\item At the highest level, Zope lets you store HTML documents,
images, and other data files in its object database, and lets you add,
delete, and edit items using your Web browser to access its management
interface.  Zope supports some sophisticated features; for example,
you can create several user accounts and grant them different
permissions.  Every time you add, edit, or delete an object, you can
undo the change; the object database stores the different versions of
each object.  You can also open a new Version of your own while you
make extensive changes to a site; ordinary users can't see the changes
you make until you close the Version, so you don't have to worry about
breaking the site if a change goes wrong.  

With Zope a URL doesn't point to a filename or a CGI script, but to an
object within the database.  XXX continue this explanation

\item The objects in a database aren't limited to static chunks of
data.  Zope provides a Document Template Markup Language (DTML) which
adds templating features to text files; this is most often used for
HTML files, but could also be used to generate plain text files.  DTML
uses special HTML tags and entities beginning with \samp{dtml-}.  For
example, this sample code XXX come up with a simple but interesting
example case.

\begin{verbatim}
<dtml-if "title=='index_html'">
  Welcome to 
</dtml-if>
\end{verbatim}

\item DTML can be used for simple to moderately complex tasks, but
more elaborate tasks can be cumbersome; the \code{<dtml-...>} markup 
makes the code hard to follow, and it's not possible to include
several lines of code.  \object{ExternalMethod} objects are another
class of object that can be added to the database, containing an
arbitrary Python function that you've written.  In this way you 
use all the expressive power of Python to extend a Zope-based Web site.

\item Zope can also talk to relational databases.  This is a matter of
installing a Database Adapter for your database -- Oracle, PostgreSQL,
MySQL, and several other databases are supported -- and then creating
Z SQL Methods to represent individual SQL query.  For example, a Z SQL
query to retrieve a user's record from a database might contain the
following SQL code:

\begin{verbatim}
select userid, password, first_name, last_name,
       city, state, country,
       fav_colour
from registered_users 
where userid = <dtml-sqlvar userid type=string>
\end{verbatim}

This Z SQL Method takes one input parameter named \code{userid}; the 
\code{<dtml-sqlvar>} tag substitutes the value of the input parameter
into the text of the SQL query, adding single quotes around the string
and escaping any single quotes within the string, following SQL
syntax.  Assuming the above query is named \code{get_user}, you can
call it from DTML like this:

\begin{verbatim}
<dtml-in "get_user('amk')">
  <font color="&dtml-colour;">
  Hi, <dtml-var first_name>!  
  Here's the weather in <dtml-var city>, &dtml-state; :
    <!--More DTML markup would go here-->
  </font>
</dtml-in>
\end{verbatim}

\code{\&dtml-colour;} uses an HTML entity-style notation to substitute
  the 'colour' column returned by the query into the HTML.  
\code{<dtml-var first_name>} does the same thing, a bit more wordily;
the code would work equally well with \code{\&dtml-first_name;} 
and \code{<dtml-var colour>}.

\item Objects in the database are really instances of Python classes.
Advanced users can add new DTML tags, and can also write new objects
that take advantage of Zope's machinery, and the objects can be tied
together using DTML and \object{ExternalMethod} functions.  A
programmer can package the code as a Zope product for others to use;
installing a new product is simple, simply requiring you to unpack a
\code{.tgz} or ZIP file in your Zope installation, and restarting Zope
to make it notice the newly installed product.

For example, Ty Sarna has written a CalendarTag product which displays
a month, a week, or a single day.  Once it's installed, using 
the new calendar tag looks like this:

\begin{verbatim}
XXX put example here
\end{verbatim}

\end{itemize}

This sprawling variety of features makes Zope very powerful, because
many common Web-site jobs are reduced to gluing together the right
objects with a bit of DTML, but it also makes the learning curve
steep, because you have to learn about all these components.  It's
worth noting that users on the Zope mailing lists often refer to
``Zope Zen'', and in fact the process of understanding Zope 
is often punctuated by Zen-like flashes of insight, when you suddenly
see how several pieces fit together.  I hope that this chapter will
help give you a few such moments of insight.

Questions:

Writing a simple CGI script

Writing a fancy CGI script with ExternalMethods

Pulling data out of a database

Inserting data into a database


Glossary:

Database Adapter

ExternalMethod

Product

Z SQL Method