Plone.api.portal.show_message [RESOLVED]

Hello Plone Community. First off, I would like to say that I am new to Plone Development, so please bear with me if this is an impossible task (or a trivial one).

I am trying to call the plone.api.portal.show_message method inside a classless Python file in my add-on in order to display a status message to the user. However, as specified here in the documentation, there is a required "request" parameter. From a significant amount of Googling, I have determined that this refers to a collection of HTTP Request information stored in an object (such as BaseRequest). Am I incorrect in understanding this?

Anyways, when I try to do

from ZPublisher.BaseRequest import BaseRequest
api.portal.show_message('success!', BaseRequest())

in my classless file, I get an error in my Plone site:

TypeError: ('Could not adapt', , <InterfaceClass Products.statusmessages.interfaces.IStatusMessage>)

Does anyone know how I can use plone.api.portal.show_message? I've tried to find example use cases on the internet and I haven't seen anything. My goal by using this method is to display notifications to my users; as a follow-up question, are there alternative ways of doing that in Plone 5 that I should be aware of? Again, I'm sorry if this is an idiotic/obvious question.

Not sure what you mean by classless Python file... are you saying that if you use request=request as in the docs you get an error?

Might help to post the entirety of your code...?

I'm sorry, what I meant by "classless Python file" was that it has no "class" declaration in it. Yes, using request=request throws an error because "request" is not a defined variable, hence my seeking out BaseRequest.

The method that I am trying to use show_message in is a handler method that is fired on IObjectAddedEvent. Here is a sample of the code (I have scrapped everything else in content_created temporarily while I troubleshoot this issue, and this is the only method in the whole Python file). content_created is the handler method.

from plone import api
from ZPublisher.BaseRequest import BaseRequest

def content_created(object, event):
    api.portal.show_message('success!', BaseRequest())

Does this information help?

Try:

from plone import api
from zope.globalrequest import getRequest

def content_created(object, event):
    api.portal.show_message('success!', getRequest())
3 Likes

Thank you! That worked!

1 Like