Re: FR: Option to terminate page rendering from an Action nicely

Ralf Schlatterbeck <[email protected]>
Newsgroups gmane.comp.bug-tracking.roundup.user
Message-ID <[email protected]>
Hi Nagy, see below for my fix, it probably is a good idea to put that
into some lib shipped with roundup and document the behaviour.

On Fri, Aug 06, 2021 at 09:12:50PM +0200, Nagy Gabor wrote:
> 
> Based on ExportCSVAction, I wrote an ExportXLSAction using xslxwriter.
> Everything worked fine on Linux systems, but MS Office always reported
> corrupted xlsx file, which seemed OK after applying Office's repair.
> Mystery...
> 
> After hours of debugging, I found the bug:
> 
> First I realized that the problem is that my generated .xlsx file is
> correct, but it is served with a '\n' character added to the end of the
> file. Analogously to cgi.actions.ExportCSVAction.handle(), the last line of
> my handle method was also   return '\n'.
> We are getting closer: Yes, the returned string is appended to the
> served content, this is verified for different strings. At this point I
> believed that I am done, but neither return '' nor return None worked.
> In these cases the issue.index template file is rendered into the xlsx
> binary. Hmmm. So I have to looked into Roundup's code deeper, and I
> found the relevant code in cgi/client.py (lines 685-696, inner_main()
> method of Client class):
> 
>                 if csrf_ok:
>                     # csrf checks pass. Run actions etc.
>                     # possibly handle a form submit action (may change
>                     # self.classname and self.template, and may also
>                     # append error/ok_messages)
>                     html = self.handle_action()
>                 else:
>                     html = None
> 
>                 if html:
>                     self.write_html(html)
>                     return
>                 # now render the page
>                 # we don't want clients caching our dynamic pages
> 
> This means that if the action's handle() returns anything which
> evaluates to True in the "if html" condition, then it is added to the
> page, and the rendering is done; otherwise the rendering goes on.
> 
> My feature request is to add a nice possibility for the Action to
> terminate the page rendering. (Maybe by raising an exception, which is
> handled by the except part of inner_main.) I've just added an
> "if html == True:
>      return"
> test before the "if html:" test, and my Action returns True. (This
> is an ugly solution, but it should work, because self.write_html(True)
> induces an error, so probably no other Action returns True, because
> with the old code, that would lead to an exception.)

I'm using the following class in my actions:

class True_Value (type ("")):
    """ A class that evaluates to True but can return a zero-length string.
        We use this as return value from a handle routine where the
        output happend to the file descriptor
    """
    def __bool__ (self):
        return True

Note that for Python2 you want to add
    __nonzero__ = __bool__

I'm returning an instance of that class which appends an empty string.

Ralf
-- 
Dr. Ralf Schlatterbeck                  Tel:   +43/2243/26465-16
Open Source Consulting                  www:   www.runtux.com
Reichergasse 131, A-3411 Weidling       email: [email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.