Bizarre changes in the meaning of "self" (mixed DTML/python scripts)
Greetings: I am developing a calendar product, which allows for the addition of event objects to the main calendar object. The problem I am having is that after I use DTML scripts to perform calls to python code, the meaning of the "self" variable in the python code undergoes a strange change. I'll do my best to describe the problem below (Note that all goes well until step 5, where things get weird) 1) For instance, assume I have added (via ZMI) an instance of my ZCalendar product (call it MyCalendar). ZCalendar is coded as a Python package. 2) The default method (index_html) provides a listing of all the currently registered events, as well as provides a button to add a new event. 3) That button calls a DTML method (called add_event) on the MyCalendar instance. add_event.dtml has a form whose action is to collect information about the new event, and then (when the Submit button is clicked) call a method called "addEvent" on the MyCalendar object. 4) addEvent takes the information collected by the add_event DTML Method, and creates a new ZCalEvent object, and inserts it into a Folder called 'Events'. No surprises there, and everything works just fine until then. 5) Once addEvent is finished adding the new event, it performs the command "return self.index_html(REQUEST)". The idea here is that I want the user taken back to the main Calendar view after adding an event. I do *NOT* want them taken to any part of the management interface. 6) By the time we get here, the URL has become "http://localhost:8080/MyCalendar/addEvent", and thus Zope interprets "self" as referring to the method instance of "addEvent". Zope still manages (via acquisition) to find the index_html method, but the resulting call is something like "MyCalendar.addEvent.index_html(REQUEST)", rather than "MyCalendar.index_html(REQUEST)" 7) Needless to say, entities which are visible to MyCalendar are not visible to addEvent, and so when Zope gets to index_html, it reports an AttributeError. There are a variety of refrences I make in index_html which only make sense when it is being called on MyCalendar, and not when it is being called on the addEvent method instance. The way I eventually got around this is to change addEvent to perform a redirect instead of a return: "REQUEST.REPONSE.redirect(REQUEST['URL1'] + '/index_html')". This works, but is an egregious hack, since it depends upon the (potentially unpredictable) value of REQUEST['URL1'], which might or might not be correct in all instances. How can I do things the "correct" way, and make sure that in the context of MyCalendar.addEvent, "self" refers to "MyCalendar" and not its "addEvent" method (as is normal in all OOP languages I have ever met so far). Attached is a copy of the code I have so far (with the ugly hack included). If anyone can take a look and tell me how to avoid this situation, I would appreciate it greatly! Direct replies to me at norvelle@ag.arizona.edu would be most welcome, since this is such a high-traffic list. TKA, Erik Norvelle Support Systems Analyst, Sr. The University of Arizona
participants (1)
-
Erik M Norvelle