Hi!

Sometimes, I'd like to "forward" a page to present the results
of some processing. Usually, this new page does only show some
message in the spirit of "Following data have been saved: 
[table]".

One could write another page which contains just the necessary
code, but I prefer not to clutter the directory with lots of
servlets that have almost no functionality. Thus, I've written 
a method which allows to integrate the presentation of the
results into the servlet.

class MyPage (SidebarPage):
    # [...]

    def forwardPage(self, func):
        self.wfc.save()
        oldContent = self.writeContent
        self.writeContent = func
        self.writeBody()
        self.writeContent = oldContent

This method takes as argument the name of a method of MyPage
(or a subclass of MyPage). self.writeContent is temporarily
replaced with the new method "func". When self.writeBody() is
called, it calls "func" instead of self.writeContent. After
this, self.writeContent is replaced with the original method.

You would use this method as follows:

class SomePage (MyPage):
    def writeContent(self):
        """ this routine is called when SomePage is requested 
            by the user"""

        self.write("<form>...</form>")  # with a button that links 
                                        #to self.processData()

    def processData(self):
        """ this is the routine which is called when the
            user presses the submit-button"""

        self.result = self.doSomeProcessing()
        self.forwardPage(self.showResult) 

    def doSomeProcessing(self):
        """ proceess the form data. Usually, this method manipulates
            some attributes of ShowPage which can be used by showResult,
            e.g. lists of successful / unsuccessful manipulations"""
        return 1

    def showResult(self):
        """ this routine is called at the end of 
            self.processData()"""

        if self.result:
            self.write("The changes have been saved")
        else:
            self.write("Something has gone wrong")
        self.write('<a href="MainPage">Back to the main page</a>')


This approach seems to work quite well. There is one caveat,
though: self.forwardPage implicitly calls self.awake(). This
might be a problem if you use self.awake() for the manipulation
of the underlying model (e.g. add a row to a database table).

What do you think about this construction?

        Yours,

                Albert Brandl

_______________________________________________
Webware-discuss mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/webware-discuss

Reply via email to