FR: Option to terminate page rendering from an Action nicely
Nagy Gabor <[email protected]>
| Newsgroups | gmane.comp.bug-tracking.roundup.user |
|---|---|
| Message-ID | <20210806211250.40cb9d0a@Dell> |
Dear Roundup Users,
[For the impatient: The actual feature request is in the last
paragraph.] I am far from completely understanding Roundup's Action
handling, so I might be wrong in the problem proposal.
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.)
Regards,
Gábor
_______________________________________________
Roundup-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/roundup-users