[Zope-CMF] Problem adding Portal Images programatically
Josef Albert Meile
jmeile@hotmail.com
Sun, 07 Oct 2001 18:28:51 -0000
For the people interested, I solved the problem without using the
invokeFactory method. I had just forgot some steps. This is the code:
from Products.CMFDefault import Image
from string import rfind,lower
def createImage(location,fileName,fileContent):
#gets the fileName without its path
pos=rfind(fileName,'\\')
shortName=fileName[pos+1:len(fileName)]
#gets its extension and determines its kind of file
#I'm not sure if this is necessary, but it works
pos=rfind(fileName,'.')
extension=lower(fileName[pos+1:len(fileName)])
if extension=='gif' or extension=='jpg' or extension=='jpeg' or
extension=='bmp':
fileFormat='image/'
if extension=='jpg':
fileFormat=fileFormat+'jpeg'
else:
fileFormat=fileFormat+extension
else:
fileFormat='text/html'
#calls the method addImage from the class CMFDefault.Image
Image.addImage(self=location,id=shortName,file=fileContent,
title=fileName,content_type='Image',format=fileFormat)
#This is the part I had forgot
#gets the created object and changes its PortalTypeName attribute
#This ensures that you will be able to see it through the portal
ob=location._getOb(shortName)
if hasattr(ob, '_setPortalTypeName'):
ob._setPortalTypeName('Image')
#You have to do this if you want that this image appears on the
#catalog
ob.reindexObject()
Notes:
- On my personal opinion the last step is useless because I think most
people looks for information rather than images, and, if I am not
wrong, as many objects as you have on your catalog, the search will
be slower.
- location is a folder object, which on my case is the folder user.
I got it by calling the following lines:
<dtml-let
user="portal_membership.getAuthenticatedMember().getUserName()"
folderLocation="portal_membership.getHomeFolder(user)">
- fileName and fileContent were acquired from the following input on
my editForm:
<input type="file" name="attachedImage">
I put the following on my editDone form:
<dtml-call
"REQUEST.set('fileContent',REQUEST.form['attachedImage'].read())">
<dtml-call
"REQUEST.set('fileName',REQUEST.form['attachedImage'].filename)">
I hope that you find this useful.
Once more, thank you to Gregoire Weber for its valuable howTo, from
which I took most of the code.