[Zope] Python product with Catalog

Sin Hang Kin iekentsin@infoez.com.mo
Thu, 17 Feb 2000 15:34:43 +0800


I try to make a simple searchable python product. I could not find the
proper document for it so I copy from photo product and mixed with the
boring product as below. I really do not understand what I am doing, but it
works. However, I do not know how to make it catalog itself automatically as
catalogaware says. Where I tell which catalog the newly add object to
catalog? How?

Code follows:

# Documentation string.

__doc__ = """Term product module.

"""

# Version string. Usually updated automatically by CVS.

__version__ = '0.1' # I used to wonder whether Zope checked these. Nope.

# Now, you can compress these imports down to three lines, but that makes it

# harder to document it as you go. Here's the long-winded version:

from Globals import HTMLFile # fakes a method from a DTML file

from Globals import MessageDialog # provid

from Globals import Persistent # makes an object stick in the ZODB

import OFS.SimpleItem

import Acquisition

import AccessControl.Role

from Products.ZCatalog import ZCatalog

from Products.ZCatalog.CatalogAwareness import CatalogAware

"""Step #1: Provide "constructors" for Zope's management interface.

__init__ is all very well and good for Python, but doesn't provide

a nice HTML interface or accept arguments from an HTTP request, so

you need to provide some functions to act as a middle-man between

Zope and your class.

Use HTMLFile to produce a [pseudo-]function which hands back an

appropriate DTML document based on a file sitting in your product

directory.

"""

manage_addTermForm = HTMLFile('termAdd', globals())

def manage_addTerm(self, id, title='', definition='', REQUEST=None):

"""Add a term to a folder."""

self._setObject(id, Term(id, title, definition))

if REQUEST is not None:

return self.manage_main(self, REQUEST)

"""Step #2: Provide the class for your Term object."""

class Term(

OFS.SimpleItem.Item, # A simple Principia object. Not Folderish.

Persistent, # Make us persistent. Yaah!

Acquisition.Implicit, # Uh, whatever.

AccessControl.Role.RoleManager, # Security manager.

ZCatalog.ZCatalog,

CatalogAware

):

"""Term object.


This is an attempt at writing a product from scratch using mostly

Python instead of ZClasses. We'll see how it all goes.

"""


meta_type = 'Term' # what do people think they're adding?

manage_options = ( # what management options are there?

{'label': 'Edit', 'action': 'manage_main'},

{'label': 'View', 'action': ''}, # defaults to index_html

{'label': 'Security', 'action': 'manage_access'},

)

__ac_permissions__=( # what permissions make sense for us?

('View management screens', ('manage_tabs','manage_main')),

('Change permissions', ('manage_access',) ),

('Change Term' , ('manage_edit',) ),

('View Terms', ('',) ),

)

def __init__(self, id, title='', definition=''):

"""initialise a new instance of Term"""

ZCatalog.ZCatalog.__init__(self, id, title)

self.id = id

self.title = title

self.definition = definition

self.boringmembers = []

# self._catalog.addIndex('title', 'TextIndex')

self._catalog.addIndex('definition', 'TextIndex')

# self._catalog.addColumn('title')

self._catalog.addColumn('definition')

index_html = HTMLFile('index', globals()) # View Interface


manage_main = HTMLFile('termEdit', globals()) # Management Interface


def manage_edit(self, title, definition, REQUEST=None):

"""does this really need a doc string?"""

ZCatalog.Z

self.title = title

self.definition = definition

if REQUEST is not None:

return MessageDialog(

title = 'Edited',

definition = '',

message = "Properties for %s changed." % self.id,

action = './manage_main',

)