Hello! I'm trying to write a small filesystems based product which I'm calling MovieWorld. The main class (MovieWorld) inherits from ObjectManager and is to be configured to contain a folder with images and a index_html page template (so that it can be edited via ZMI if needed). Setting these things up in a manage_afterAdd works fine as long as I'm just adding new products, but renaming or copying will cause manage_afterAdd to be called again, which fails because the objects to be created already exist. What should I do? The obvious solution is to check for existance of the objects and only add them if they don't exist, which is what I tried. This worked for the folder called "images", but I was using hasattr(self,'images') to check to existance, so it worked by coincidence. What I mean is it works only if there is no object called images aquired, which means it also mus not exist anywhere along the aquire path. For this reason, hasttr(self,'index_html') will return true, and the index_html template will not be added at all. It seems I need one of three things: 1. A better way to add images and a template altogether. 2. A hook which is only called once, when the object is first created. 3. A method to check if the objects exist in the newly added item directly, not aquired. Help! // Philip Jägenstedt
Hej
I'm trying to write a small filesystems based product which I'm calling MovieWorld. The main class (MovieWorld) inherits from ObjectManager and is to be configured to contain a folder with images and a index_html page template (so that it can be edited via ZMI if needed). Setting these things up in a manage_afterAdd works fine as long as I'm just adding new products, but renaming or copying will cause manage_afterAdd to be called again, which fails because the objects to be created already exist. What should I do?
Consider using CheckoutableTemplates instead of templates as Zope objects. Gives you that filesystem-based-power they're all talking about PLUS the ability to edit the template specifically or globally in the ZMI.
The obvious solution is to check for existance of the objects and only add them if they don't exist, which is what I tried. This worked for the folder called "images", but I was using hasattr(self,'images') to check to existance, so it worked by coincidence. What I mean is it works only if there is no object called images aquired, which means it also mus not exist anywhere along the aquire path. For this reason, hasttr(self,'index_html') will return true, and the index_html template will not be added at all.
No, use the 'aq_base'. Like this: def _createImages(self, imageids): here_only = getattr(self, 'aq_base', self) for imageid in imageids: if not hasattr(here_only, imageid): self.manage_addImage(...) -- Peter Bengtsson, work www.fry-it.com home www.peterbe.com hobby www.issuetrackerproduct.com
On Fri, Aug 19, 2005 at 01:25:22AM +0200, Philip Jägenstedt wrote: | Hello! | | I'm trying to write a small filesystems based product which I'm calling | MovieWorld. The main class (MovieWorld) inherits from ObjectManager and | is to be configured to contain a folder with images and a index_html | page template (so that it can be edited via ZMI if needed). Note that your object does not need to contain an index_html object in order to be edited in the ZMI. In your MovieWorld class, put the following: manage_options = OFS.Folder.Folder.manage_options (be sure to 'import OFS' earlier in the module) The manage_options object in the class defines what tabs will be shown in the ZMI and what method to call when the admin clicks on the tab. HTH, -D -- Your beauty should not come from outward adornment, such as braided hair and the wearing of gold jewelry and fine clothes. Instead, it should be that of your inner self, the unfading beauty of a gentle and quiet spirit, which is of GREAT WORTH in God's sight. For this is the way the holy women of the past used to make themselves beautiful. I Peter 3:3-5 www: http://dman13.dyndns.org/~dman/ jabber: dman@dman13.dyndns.org
Philip J?genstedt wrote:
It seems I need one of three things:
1. A better way to add images and a template altogether. 2. A hook which is only called once, when the object is first created. 3. A method to check if the objects exist in the newly added item directly, not aquired.
As Peter mentioned, use aq_base. There are 2 ways to use it. One as an object attribute (self.aq_base) or using the aq_base() function as imported from Acquisition. The latter method won't cause an attribute error if you already are working with the non-acquisition object (in that case it will simply return the object. For what you are doing I recommend one of these methods. To get more control over when manage_afterAdd runs some code, you can set some flags using the copy hooks. Before running manage_afterAdd when copying/moving/renaming ObjectManager calls the method _notifyOfCopyTo() on the object being copied/moved/renamed. It passes one argument (op) giving some context to the call. It is set to 1 for moves and renames, and set to 0 for copies. So you can use this to set a flag on your object to modify manage_afterAdd's behaviour in these circumstances. Eg. class Container(ObjectManager): def _notifyOfCopyTo(self,op): # use a _v_ volitile attribute here to avoid persistence self._v_copy_flag = op def manage_afterAdd(self, item, container): if hasattr(self,'_v_copy_flag'): if self._v_copy_flag == 0: # stuff you want done when copying ... if self._v_copy_flag == 1: # stuff you want done when moving/renaming ... # clear the flag delattr(self,'_v_copy_flag') else: # stuff you want done only on initial install ... # stuff you want done no matter what. .,. Of course you can simplify this if you don't care if its been moved, renamed or copied. Just wanted to show the different possibilities. -- John Eikenberry [jae@kavi.com] ______________________________________________________________ "A society that will trade a little liberty for a little order will deserve neither and lose both." --B. Franklin
participants (4)
-
Derrick Hudson -
John Eikenberry -
Peter Bengtsson -
Philip Jägenstedt