Hello, im just learning zope and python and find it difficult to get the details of aquisition and inheritance. I have the following problem: (FS product) My base class has an attribute manage_editSettingsForm In its __init__ method I instantiate a contoller class. In the methods of the controller class can access self.manage_edtiSettingsForm (fine) Now I instantiate a importSingle class in a method of the controller class. In the methods of the importSingle class I cannot access self.manage_edtiSettingsForm (why?) Controller class: from Acquisition import Implicit from Globals import InitializeClass, Persistent from Products.PythonScripts.Utility import allow_class, allow_module from AccessControl import ClassSecurityInfo from OFS.SimpleItem import SimpleItem from OFS.ObjectManager import ObjectManager from Products.BTreeFolder2.BTreeFolder2 import BTreeFolder2 from Lasttest import Lasttest import sys, os, urllib, commands, re, types import LTimport allow_module=('Controller') class Controller(Implicit,ObjectManager, SimpleItem): --------------------- importSingle Class: from Globals import InitializeClass from Acquisition import Implicit import os,re class importSingle(Implicit): I tried subclassing form SimpleItem and ObjectManager but it didn't work. What is required that inheritance will work? Greetings Roman
+-------[ Roman Klesel ]---------------------- | | Hello, | | im just learning zope and python and find it difficult to get the details of aquisition and inheritance. | I have the following problem: | | (FS product) | | My base class has an attribute manage_editSettingsForm | In its __init__ method I instantiate a contoller class. | In the methods of the controller class can access self.manage_edtiSettingsForm (fine) | Now I instantiate a importSingle class in a method of the controller class. | In the methods of the importSingle class I cannot access self.manage_edtiSettingsForm (why?) One (or more) of the following two; Your class isn't yet fully instantiated and Acquisition wrapped, this normally doesn't occur until your class is inside the ZODB. You generally need to use a 'post init' method to do a 2-stage init so that subitems can acquire items. There are Zope hooks you can use (manage_afterAdd), or you can call your own methods in the "manage_add" Factory method of your product. If this is what you are doing then it's probably; The importSingle class must be an attribute of the Controller class for Acquisition to work in this way. -- Andrew Milton akm@theinternet.com.au
Hello Andrew, thanks for your reply. Andrew Milton schrieb:
One (or more) of the following two;
Your class isn't yet fully instantiated and Acquisition wrapped, this normally doesn't occur until your class is inside the ZODB.
That's true! And it's nor supposed to be. It's only a temporary Object that's being destroyed after the method in which it's created has finished processing.
You generally need to use a 'post init' method to do a 2-stage init so that subitems can acquire items. There are Zope hooks you can use (manage_afterAdd), or you can call your own methods in the "manage_add" Factory method of your product.
If this is what you are doing then it's probably;
The importSingle class must be an attribute of the Controller class for Acquisition to work in this way.
I wouldn't like to do that either, because I don't want that object to persist. Probably I just do things in the wrong way. What I wnat to do is: In the temporary object: try: h=open('file',r) except: em = 'File %s does not exist' % file return REQUEST.RESPONSE.redirect(self.manage_editSettings(REQUEST), em) the calling Object then would pass it to the browser ... ... in order to do some exception handling. is there an other way to redirect to the settigs Form when an error occurs in this nonpersistent object? Greetings Roman
+-------[ Roman Klesel ]---------------------- | Hello Andrew, | | thanks for your reply. | | Andrew Milton schrieb: | > | > One (or more) of the following two; | > | > Your class isn't yet fully instantiated and Acquisition wrapped, this normally | > doesn't occur until your class is inside the ZODB. | | That's true! And it's nor supposed to be. It's only a temporary Object that's being destroyed | after the method in which it's created has finished processing. [snip] | Probably I just do things in the wrong way. What I wnat to do is: | | In the temporary object: | | try: | h=open('file',r) | except: | em = 'File %s does not exist' % file | return REQUEST.RESPONSE.redirect(self.manage_editSettings(REQUEST), em) redirect should take a URL (i.e. a string), not a rendered page... | the calling Object then would pass it to the browser ... | | ... in order to do some exception handling. | | is there an other way to redirect to the settigs Form when an error occurs in this nonpersistent object? Since your class is already assuming things (manage_editSettings e.g.), a) pass in a parent object that is wrapped and call parentObj.manage_editSettings.. b) or more cleanly call parentObj.errorOccurred(message) which would do the redirect, c) or even more cleanly you could raise an exception, and catch it in the parent class, then redirect. -- Andrew Milton akm@theinternet.com.au
On Fri, Feb 03, 2006 at 02:04:49PM +0100, Roman Klesel wrote:
Hello Andrew,
thanks for your reply.
Andrew Milton schrieb:
One (or more) of the following two;
Your class isn't yet fully instantiated and Acquisition wrapped, this normally doesn't occur until your class is inside the ZODB.
That's true! And it's nor supposed to be. It's only a temporary Object that's being destroyed after the method in which it's created has finished processing.
You generally need to use a 'post init' method to do a 2-stage init so that subitems can acquire items. There are Zope hooks you can use (manage_afterAdd), or you can call your own methods in the "manage_add" Factory method of your product.
If this is what you are doing then it's probably;
The importSingle class must be an attribute of the Controller class for Acquisition to work in this way.
I wouldn't like to do that either, because I don't want that object to persist.
Probably I just do things in the wrong way. What I wnat to do is:
In the temporary object:
try: h=open('file',r) except: em = 'File %s does not exist' % file return REQUEST.RESPONSE.redirect(self.manage_editSettings(REQUEST), em)
... and you want manage_editSettings to be acquired, ok. As has been suggested, you probably don't have an acquisition wrapper set up. You can create one without persisting your wrapped object like so: class Controller(...): def some_method(self, ...): ... temp = importSingle(...) temp = temp.__of__(self) # this creates the wrapper temp.someMethod() ... Once you have a wrapper, you can call methods on it and those methods can successfully acquire things from the acquisition context (in this case the Controller instance). This example assumes that your Controller is responsible for instantiating the importSingle instances. But having said that, I too wonder why use acquisition for this. Why not just pass in a reference to the Controller instance and call methods on it explicitly? -- Paul Winkler http://www.slinkp.com
participants (3)
-
Andrew Milton -
Paul Winkler -
Roman Klesel