Random ...
December 2017
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Tags ...

python+zope++:: ZClasses: Am I Evil?
Posted at 28.Mar,2005 14:11  Comments 0 / Trackbacks 0 / Like this post!
Technorati tag(s):

Here's how to make ZClasses less evil. Use a Python Base Class. "How?", one might ask. Here's how I did it.

First of, make ourselves a Zope Python product. Don't know how? Try reading up on Zope Developers Guide. Or get Maxm's Easy product.

My sample Python Base Class is at the end of this blog item. To subclass our Python Product, we need to make Zope aware that this product is available for subclassing. To do that, add this extra lines in our python product __init__.py

from ZClasses import ZClass
ZClass.createZClassForBase(NewsArticle, globals())

Now, let's go to Control_Panel/Products, and add a Product. Let's call it News. Click News, and add a ZClass. We can now subclass our python base product; NewsArticle.

How much does this help? IMHO, now if we need to change our base class, we can do it on our python base class. Our ZClasses will still work, and actually inherit everything from our python base class.

I've minimally tested this. Initially, my python base class only derives from SimpleItem.Item

After instantiating a News instance, I changed my python base class to derive from Folder.Folder

Now, I added a new ZClass "Views" named "Contents" that calls manage_main (which is inherited from Folder.Folder.manage_main).

Clicking the News instance, we can see the new view "Contents" since our instance is now Folderish.

What we gain from this exercise is ZClasses TTW "pointy clicky" stuff that people loves; well at least a lot of people I know.

This is not the be all end all solution, but may be workable for stuff that needs done immediately.

The Main Class; NewsArticle.py

 Author: Bakhtiar AHamid 

from Acquisition import Implicit
from Globals import Persistent
from AccessControl.Role import RoleManager
from AccessControl import ClassSecurityInfo
from Globals import InitializeClass
from Globals import DTMLFile, HTMLFile
from Globals import MessageDialog
from OFS.SimpleItem import Item
#from OFS.Folder import Folder

addForm = DTMLFile('dtml/addForm',globals())

def manage_addNewsArticle(self, id, title='', REQUEST=None):
    """the add method"""
    ob = NewsArticle(id, title)
    if REQUEST is not None:
        return REQUEST.RESPONSE.redirect('manage_workspace')

class NewsArticle(Implicit, Persistent, RoleManager, Item):
    """My Base Product."""

    meta_type = 'News Article'


              {'label' : 'Edit', 'action' : 'editNewsArticleForm',
 'help': ('NewsArticle','edit.txt')},
              {'label' : 'View', 'action' : '', 'help': ('NewsArticle','index.txt')},
              {'label': 'Properties' , 'action':'main'},
              {'label': 'Test' , 'action':'manage_test'},
            ) + \
RoleManager.manage_options + Item.manage_options

    security.declareProtected('View management screens','main')
    main= DTMLFile('dtml/main',globals())
    security.declareProtected('View management screens','manage_test')
    manage_test= DTMLFile('dtml/test',globals())
    security.declareProtected('View management screens','editNewsArticleForm')
    editNewsArticleForm = DTMLFile('dtml/editNewsArticleForm',globals())
    #index_html = HTMLFile('index_html',globals())

    def __init__(self, id, title=''):
        """my init"""
        self.id = id
        self.title = title

the __init__.py
from NewsArticle import NewsArticle, addForm, manage_addNewsArticle
from ZClasses import ZClass

ZClass.createZClassForBase(NewsArticle, globals())

def initialize(registrar):
        constructors = (addForm, manage_addNewsArticle),
        icon = 'www/nart.jpg'
#this is the same as the above, but deprecated
#    registrar.registerBaseClass(NewsArticle)

Bookmark and Share

Is this entry helpful? Comments/Donate/Click some google ads.  
Trackback is http://myzope.kedai.com.my/blogs/kedai/20/tbping 

Post a comment