[Zope] Enable and Use Custom Classes in Zope

Istvan Albert ialbert at mailblocks.com
Fri Jul 23 10:48:52 EDT 2004


I posted this in the python group but then I thought it would
be quite suited to this group too. So here is an opinion on how
to enable custom classes in Zope.

---------------------

mir nazim wrote:

 > Seems cool. Please eleborate it a bit. possibly with some examples.


It is one of those things that are not well explained in the
Zope docs ... adn this post too seems like it should belong
to a Zope group.

Yet I heard python people say that the Zope model does not allow one
to approach a problem in a more rigorous, 'programatic'  way.
This is only partly true. Here is how one could  go about it
in what I think is the so called MVC, Model-View-Controller
pattern.

Let's say that we have a situation where one would need
to make use of an instance of a custom User.User class
located in a custom package named foo. (Note that I'm typing
this in so it might have typos in it.)

---

step 0. write and test the foo package and User.User class

---

step 1. you need to deploy your package into the
zope python. I use distutils:

<zope_python> setup.py install

the only thing here is that it is easy to forget and
use the "other" python ... then you are trying to track
probelms that don't actually exists, can be very annoying

-- 

step 2. enable the classes in Zope. Create a folder in the
Products directory called <whatever you want> Place the
following  __init__.py file in this folder:

from AccessControl import allow_module, allow_class, allow_type
from foo import User
allow_module('foo.User')
allow_class(User.User)
allow_type(type(User))

-- 

step 3. to use your class here is the hello.py python script that
read the user instance from a session.

from foo import User

hello_zpt = container['hello.zpt']
error_zpt = container['error.zpt']

user = container.REQUEST.SESSION.get(key='user', None)

if user:
    # I can do here whatever I want to with the user instance
    if user.user_type == 'Troll':
       msg= 'Go away!'
    else:
       msg ='Howdy!'
    return home_zpt(user=user, msg=msg)
else:
    return error_zpt(msg='Please log in!')

---

step 4. in hello.zpt then I have something like this:

A message for user <b tal:content="options/user/user_name"/> of type
<b tal:content="options/user/user_type"/> :
<b tal:content="options/msg"/>

---

I believe that The User.User class -> hello.py -> hello.zpt chain
forms what is called the MVC (Model-View-Controller) pattern.

The model is the class instance itself, the controller is hello.py and the view
is in hello.zpt. I found that it takes a lot of discipline to stay outside
the zpt files without computing all kinds of intermediate results in them.

When making small 'adjustments' it is very tempting to do them by defining
some behavior inside the zpt. But then, invariably, after a few days I
can't remember anymore how things work...

So for me putting everything in the .py controllers is paying off.

best,

Istvan.




More information about the Zope mailing list