[Zope] Making products that contain multiple classes

Max M maxmcorp@worldonline.dk
Tue, 15 Jan 2002 12:00:58 +0100


> From: zope-admin@zope.org [mailto:zope-admin@zope.org]On Behalf Of Jeff
> Kowalczyk

> I became interested in trying out the new mxm product which offers a
> reusable base class for products, mxmSimpleItem. I thought I'd try to
> create the Student/Teacher/Course classes, with which I could later try
> out the mxmRelation product. (Objects with manageable
> relationships/references are the thing I've been waiting on to really
> get started with Zope)

Great ... so have I :-)

> I tried to create my three classed in one product. I finally have all
> classes, but all objectmanager adds call the third classes constructor.
> If I add a Course, I still get a Teacher in its place, because that's
> the final definition of the manage_addForm and manage_addAction.

The his is not a problem with the mxm package, but something that will bite
you every time you try to have mutltiple classes in one file.

The problem is, as you correctly have pointed out, that your manage_addForm
and manage_addAction is defined in the package, and as such are global
names. What you need to do in that case is to rename the methods, and create
a new dtml file for each class. Like::

def manage_addCourseAction(self, id=None, REQUEST=None):
    "Add instance to parent ObjectManager"
    mxmSimpleItem.addClass(self, id, Course, REQUEST)
courseConstructors = (manage_addCourseForm, manage_addCourseAction)

def manage_addClassAction(self, id=None, REQUEST=None):
    "Add instance to parent ObjectManager"
    mxmSimpleItem.addClass(self, id, Class, REQUEST)
classConstructors = (manage_addClassForm, manage_addClassAction)

etc. You must also remember to add the right security for the new methods.

and then in you __init__

    context.registerClass(
        Course.Course,
        constructors = Course.courseConstructors,
        icon='images/Course.gif')
etc ...

 Are multiple classes per product something that just isn't done, isn't
> done with mxm, or is easily done once I know the trick?

It is done just like with every product. But the mxm package is meant to be
one product one folder. If you want to keep it simple that is.

Else you can do exactly the same with the mxm based products as with the
normal products. There is no difference. It is just a normal product with a
little extra stuff added to make it easy to get started.

regards Max M

> "__init__.py" -------(some linebreaks removed)-------
> import Course
> import Student
> import Teacher
> def initialize(context):
>     "This makes the object apear in the product list"
>     context.registerClass(
>         Course.Course,
>         constructors = Course.constructors,
>         icon='images/Course.gif')
>     context.registerClass(
>         Student.Student,
>         constructors = Student.constructors,
>         icon='images/Student.gif')
>     context.registerClass(
>         Teacher.Teacher,
>         constructors = Teacher.constructors,
>         icon='images/Teacher.gif')
>
> Course.py -------------------------------------------
> from mxm import mxmSimpleItem
> class Course(mxmSimpleItem.mxmSimpleItem):
>     meta_type = 'Course'
>     _properties = (
>         {'id':'number', 'type':'int', 'mode':'w'},
>         {'id':'title', 'type':'string', 'mode':'w'},
>         {'id':'summary', 'type':'text', 'mode':'w'},
>         {'id':'building', 'type':'string', 'mode':'w'},)
> def manage_addAction(self, id=None, REQUEST=None):
>     "Add instance to parent ObjectManager"
>     mxmSimpleItem.addClass(self, id, Course, REQUEST)
> constructors = (mxmSimpleItem.manage_addForm, manage_addAction)
>
> Student.py and Teacher.py are essentially identical, save for the
> _properties. Can I do anything to the constructors to allow this
> multi-class/single product package to work?
>
>
>
> _______________________________________________
> Zope maillist  -  Zope@zope.org
> http://lists.zope.org/mailman/listinfo/zope
> **   No cross posts or HTML encoding!  **
> (Related lists -
>  http://lists.zope.org/mailman/listinfo/zope-announce
>  http://lists.zope.org/mailman/listinfo/zope-dev )
>