[Zope] ZClasses meet PythonScripts, sample request

Jim Washington jwashin@vt.edu
Sun, 14 Jan 2001 15:35:42 -0500


Hi, Tim

I am not working through that example, but the below is a start on what
you seem to need. Let me know what you think.

good luck!

-- Jim Washington

***************************************

You probably want the work to be done in the context of an instance of
the class, so assure you have "context" bound to the Python Script on
the "bindings" tab.  It's bound by default.

You might want to name the Script in a Zopish way to distinguish it from
other scripts that may do other things.  I would probably call it
manage_changeCDProperties.

You will want to pass REQUEST to it so you can use it like the Zope
builtins:

containerName.CDInfoName.manage_changeCDProperties(REQUEST)

So, put REQUEST in the parameters list.

(BTW, REQUEST is available as context.REQUEST if you choose not to pass
REQUEST.)

Inside the script, you would use Zope's manage_changeProperties method:

context.propertysheets['cd_info'].manage_changeProperties(REQUEST)
-or-
context.propertysheets.cd_info.manage_changeProperties(REQUEST)

You're done, unless you want to get fancy and process the items
separately.

If you want to get fancy, REQUEST.form['title'], REQUEST.form['artist'],
and REQUEST.form['date_purchased'] are available to you.

For example, if you want to assure title case for title and artist (try
this in DTML first, just to get the scope of how much easier it is in a
Python Script):

import string
dontCapitalize = ['a','in','of', 'for']
localPropertiesDict = {}
for aproperty in ['title','artist']:
  theProperty = string.strip(REQUEST.form[aproperty])
  splitWords = string.split(theProperty)
  newlist = []
  for aWord in splitWords:
    if aWord in dontCapitalize and aWord != splitWords[0]:
      newlist.append(string.lower(aWord))
    else:
      newlist.append(string.capitalize(aWord))
  newValue = string.join(newlist,' ')
  localPropertiesDict[aproperty] = newValue
context.propertysheets['cd_info'].manage_changeProperties(localPropertiesDict)  



****************************************

To delete something from the ZODB, pass a list of ids to
manage_delObjects in your container.

To delete them all:

thelist = context.containerName.objectIds('CD_Info')
context.containerName.manage_delObjects(thelist)

Let's say you lent out all your Britney Spears CDs and don't expect to
get them back.  How do you update your collection?

Make a form that allows you to select 'Britney Spears' into a variable
called 'deleteThisArtist'

Make a Python Script that is called by the action for the form.  This
goes into your class for CD collection, whatever you have named it.

import string
#we make this case-insensitive by converting all to uppercase
deleteMe = string.upper(context.REQUEST.form['deleteThisArtist'])
listToDel = []
for CD in context.objectValues('CD_Info'):
  if string.upper(CD.artist) == deleteMe:
    listToDel.append(CD.id)  #in Zope-2.3.0+, you should use .getId()
instead of .id
context.manage_delObjects(listToDel)


Timothy Wilson wrote:
> 
> Hi everyone,
> 
> I'm looking forward to using some PythonScripts as methods of some ZClasses
> because it seems like a much cleaner way to do things. I wonder if one of
> the PythonScript gurus out there would be willing to show an example of two
> presumably common PythonScript/ZClass tasks: processing changes to a
> property sheet using form input and deleting a ZClass instance.
> 
> Let's say we've got a ZClass for keeping track of a CD collection. (I recall
> seeing that example in a ZClass How-To.) Let's say the ZClass has a property
> sheet called 'cd_info' with the following properties:
> 
> title (string)
> artist (string)
> date_purchased (date)
> 
> (for extra credit, add a 'songs' property of type 'lines' :-)
> 
> Is that enough info? Anybody want to take a crack at this? (This would
> surely be good info to put at zope.org somewhere.)
> 
> -Tim