Hi! I have some problems creating my first product. I've read the Developers Guide and the boring product, but I could not solve 2 problems: 1.) I can select my product from the add list, but whenever I try to add it to the folder (after submitting the add form) I get an error message: KeyError: manage_page_header When I access manage_main via the 'edit' tab of an already created product, this error does not appear. 2.) I want to add some properties to my product. So I defined the _properties variable, but my property 'color' does not appear on the property manage interface. Here is my source code: from Acquisition import Implicit from Globals import Persistent from AccessControl.Role import RoleManager from OFS.SimpleItem import Item from Globals import DTMLFile from OFS import PropertyManager from AccessControl import ClassSecurityInfo from Globals import MessageDialog def addMethod(self, id, REQUEST=None): """ Create a new article """ a = Article(id) self._setObject(id, a) if REQUEST is not None: return a.edit(REQUEST) addForm = DTMLFile('dtml/addForm', globals()) class Article(Implicit, Persistent, RoleManager, PropertyManager, Item): """ Article product class """ meta_type = 'article' title = 'Sample article' security=ClassSecurityInfo() manage_options=( {'label' : 'Edit', 'action' : 'manage_main'}, {'label':'Properties', 'action':'manage_propertiesForm',} ) + RoleManager.manage_options + Item.manage_options _properties=({'id':'color', 'type': 'selection', 'mode': 'w', 'select_variable': 'getColors'}, ) manage_main=DTMLFile('dtml/edit', globals()) def __init__(self,id): self.id = id self.color = 'green' security.declareProtected('Edit article', 'edit') def edit(self, REQUEST=None): """ Updates the content of an article. """ if REQUEST is not None: return manage_main(REQUEST, manage_tabs_message='Saved.') def getColors(self): return ['blue','green','red'] from Globals import InitializeClass InitializeClass(Article) Sorry for posting so much code, but I think it's better than forgeting something important. Thank you! Bye Stefan -- Stefan Heimann | fon: +49 (0761) 3196897 Brandensteinstr. 5 | fax: +49 (01212) 510339818 D-79110 Freiburg | e-mail: stefan.heimann@web.de
Zope product development tip-o-the-day :)
I have some problems creating my first product. I've read the Developers Guide and the boring product, but I could not solve 2 problems:
1.) I can select my product from the add list, but whenever I try to add it to the folder (after submitting the add form) I get an error message: KeyError: manage_page_header When I access manage_main via the 'edit' tab of an already created product, this error does not appear.
This one I can help with :) The problem is in the "constructor" (or factory) method for the Article object:
def addMethod(self, id, REQUEST=None): """ Create a new article """ a = Article(id) self._setObject(id, a) if REQUEST is not None: return a.edit(REQUEST)
What is happening here is that 'a' is a new instance of Article, which you add to 'self' (the container). The reason why you get an error when trying to render the manage_main dtml (a.edit) is that 'a' is not wrapped in any Acquisition context. It works for you later via the 'edit' tab because then you are accessing it in such a way that it is wrapped. It needs to be wrapped because the ZMI needs to be able to get certain things via acquisition. The solution here is to arrange to get a wrapped version of the new Article instance before trying to render the dtml: def addMethod(self, id, REQUEST=None): """ Create a new article """ a = Article(id) self._setObject(id, a) # get a wrapped version of the new instance... wrapped = self._getOb(id) if REQUEST is not None: return wrapped.edit(REQUEST) Note that a way many people avoid having to remember this is to do a redirect at the end of the factory method rather than rendering the result dtml directly. A common paradigm in the Zope source is: def addMethod(self, id, REQUEST=None): """ Create a new article """ # In your case, you want to redirect to the manage_main of the # newly created object. Since you don't need to use anything # from the new instance directly when you do a redirect, you # wouldn't have to worry about wrapping it. a = Article(id) self._setObject(id, a) if REQUEST is not None: redirect_to = '%s/%s/manage_main' % (REQUEST['URL1'], id) REQUEST['RESPONSE'].redirect(redirect_to)
2.) I want to add some properties to my product. So I defined the _properties variable, but my property 'color' does not appear on the property manage interface.
_properties=({'id':'color', 'type': 'selection', 'mode': 'w', 'select_variable': 'getColors'}, )
I _suspect_ (but I can't verify, since I can't get to the docs at the moment and I haven't used the selection / select_variable stuff very much myself) that 'getColors' also has to be a property. The Zope book (online) should be able to clarify that. Hope this helps! Brian Lloyd brian@digicool.com Software Engineer 540.371.6909 Digital Creations http://www.digicool.com
Stefan Heimann writes:
1.) I can select my product from the add list, but whenever I try to add it to the folder (after submitting the add form) I get an error message: KeyError: manage_page_header When I access manage_main via the 'edit' tab of an already created product, this error does not appear.
2.) I want to add some properties to my product. So I defined the _properties variable, but my property 'color' does not appear on the property manage interface. .... def addMethod(self, id, REQUEST=None): """ Create a new article """ a = Article(id) self._setObject(id, a) if REQUEST is not None: return a.edit(REQUEST) Here, the "a" is not yet "wrapped" into the site hierarchy. Therefore, acquisition can not yet help to find "manage_page_header".
Add (before the return): a= a.__of__(self) I do not understand why you property handling does not work. You code seems good. Dieter
participants (3)
-
Brian Lloyd -
Dieter Maurer -
Stefan Heimann