Status Messages show() not working as expected in Senaite

Hello,

I’ve been trying to use Plone’s status messages (from Products.statusmessages) to show pop-ups for users on a few screens like the Batch - Batchbook to show messages about that screen.

By default, status messages display on the NEXT screen after being added which was undesirable since the message is based on the current screen (not the workflow leading into the screen). However, there is a show() function that supposed can be called to display the messages on the current screen. My plan was to use this feature to show a few niche conditional notifications on the Batchbook that would help users on that screen.

My issue is that when I try to use the show() function during rendering on a Senaite site, the status messages end up not showing up at all (even on the next screen). my guess is that I am calling the function in the wrong location, but I’ve tried calling it during init(), update(), folderitems(), and before_render, but all gave the same result - no messages show on the screen nor the next screen.

example:

def folderitems(self):
    items = super(BatchBookView, self).folderitems()

    message = IStatusMessage(self.request)

    # Using Products.statusmessages directly
    from Products.statusmessages.interfaces import IStatusMessage

    # This will show the message on the NEXT screen if `show` is never called.
    message.add("Test Message", type="info")

    # I would expect this to show the message on the CURRENT screen when called like in Plone
    # but it makes nothing show instead. Message cookie expires before render
    message.show()

    #Same thing happens with the API
    from plone import api as plapi
    
    papi.portal.show_message("Test message", request = self.request, type="info")

Anyone have any thoughts, solutions, or known issues? It looks like the statusmessage cookie in the session expires before the screen is rendered when show() is called directly, so I’m curious if it’s a compatibility issue with the JS in senaite.app.listing.

I know I could create a custom template macro with the errors callable that way (similar to the Invalidated Sample warning at the top of the Sample view screen), but I’m curious if there’s a way to leverage the show() function of this core feature to work in Senaite :thinking: it’s a very convenient and useful API tool for user alerts.

Hi @faytrow,

the folderitems method is called asynchronously by senaite.app.listing. Thus, it will never show your status message.

You need to do this before the template renders, which is usually in the __call__ method of a browser view.

If it is a listing view, you could also try to put it in the before_render hook:

You could also consider showing a message via JavaScript using either this code:

which is hooked automatically for these selectors:

or this still present artifact:

Hope this helps,
Ramon

1 Like

Thanks @ramonski !

On a Listing View, executing the show() command for the StatusMessages within the before_render hook does not appear to work, nor does adding it to the __call__() of a Browser View.

It definitely appears to be expiring the session cookie before the screen appears (I get a JS error in the console about it), so it might have to do with some sneaky Plone code protecting the site from XSS… :thinking: I’ll post the question to the Plone forums in case someone there has some insight as to why it would not render in an Add-On.

In the meantime I’ll see if I can get JS to handle it as you suggested. I typically try to avoid messing with JS when I can because my npm and webpack skills are amateur at best :melting_face: but this might give me a good excuse to break a screen and learn a thing or two :rofl: :hear_no_evil: :brain: