Related Entries

Interview with Ploners
Formulator makes life easy
Server side python
Resetting Zope's session timeout
Getting undo history

« Tuning Oracle backed ASP application
» Command line editing in Bash in VI mode

Updating properties of objects

The divisions in our HR job posting site had the usual spelling error for "Northeast plus Carribean" instead of "Northeast...

The divisions in our HR job posting site had the usual spelling error for "Northeast plus Carribean" instead of "Northeast plus Caribbean". Since there were already around 35 jobs for this division, it made sense to script it to update all of these.


#
# gets the job object (zclass) in the current folder and fix a
# simple spelling mistake - sbabu 03/13/02
#
# copy this to the folder where all your jobs are and run it
#
for job in context.objectValues(["Job"]):
  if job.division == 'Northeast plus Carribean':
    print job.title #just so that we know
    job.propertysheets.job_info.manage_changeProperties({'division':'Northeast plus Caribbean'})
    #had this been a python product, it would’ve been simpler -
    #job.manage_changeProperties({'division':'Northeast plus Caribbean'})
return printed

As you can see, it is not that more difficult than a standard SQL update statement. Interesting situation is when you’ve objects in multiple folders. In that case, you can use a catalog to catalog these, then search and find the objects through the catalog - this is lot more faster than looping through every object in the folder and then recursively going through all folders.

Script using the catalog (named Catalog) will be

for job in context.Catalog(division="Northeast plus Carribean"):
  #get the actual object
  #oJob = getattr(context.aq_explicit, job.id)
  #the next line will get the "real" object regardless of the path
  oJob = context.restrictedTraverse(context.Catalog.getpath(job.data_record_id_))
  oJob.propertysheets.job_info.manage_changeProperties ({'division':'Northeast plus Caribbean'})
  #Now, don’t forget to update that catalog!

//-->