Part of lp.services.webapp.notifications View In Hierarchy
Known subclasses: lp.services.webapp.servers.LaunchpadBrowserResponse
The NotificationResponse collects notifications to propogate to the next page loaded. Notifications are stored in the session, with a key propogated via the URL to load the correct messages in the next loaded page.
It needs to be mixed in with an IHTTPApplicationResponse so its redirect method intercepts the default behaviour.
>>> class MyNotificationResponse(NotificationResponse, MockResponse): ... pass >>> response = MyNotificationResponse() >>> INotificationResponse.providedBy(response) True >>> request = NotificationRequest() >>> request.response = response >>> response._request = request >>> request.principal = None # full IRequests are zope.security ... # participations, and NotificationResponse.redirect expects a ... # principal, as in the full IRequest interface.
>>> len(response.notifications) 0
>>> response.addNotification("something") >>> len(response.notifications) 1
>>> response.removeAllNotifications() >>> len(response.notifications) 0
>>> msg = structured("<b>%(escaped)s</b>", escaped="<Fnord>") >>> response.addNotification(msg)
>>> response.addNotification("Whatever", BrowserNotificationLevel.DEBUG) >>> response.addDebugNotification('Debug') >>> response.addInfoNotification('Info') >>> response.addWarningNotification('Warning')
And an odd one to test Bug #54987
>>> from lp import _ >>> response.addErrorNotification(_('Error${value}', mapping={'value':''}))
>>> INotificationList.providedBy(response.notifications) True
>>> for notification in response.notifications: ... print "%d -- %s" % (notification.level, notification.message) 20 -- <b><Fnord></b> 10 -- Whatever 10 -- Debug 20 -- Info 30 -- Warning 40 -- Error
>>> response.redirect("http://example.com?foo=bar") 302: http://example.com?foo=bar
Once redirect has been called, any notifications that have been set are stored in the session
>>> for notification in ISession(request)[SESSION_KEY]['notifications']: ... print "%d -- %s" % (notification.level, notification.message) ... break 20 -- <b><Fnord></b>
If there are no notifications, the session is not touched. This ensures that we don't needlessly burden the session storage.
>>> response = MyNotificationResponse() >>> request = NotificationRequest() >>> request.response = response >>> response._request = request
>>> session = ISession(request)[SESSION_KEY] >>> del ISession(request)[SESSION_KEY]['notifications'] >>> 'notifications' in session False >>> len(response.notifications) 0 >>> response.redirect("http://example.com") 302: http://example.com >>> 'notifications' in session False
Method | addNotification | See INotificationResponse . |
Method | notifications | Undocumented |
Method | removeAllNotifications | See lp.services.webapp.interfaces.INotificationResponse |
Method | redirect | See lp.services.webapp.interfaces.INotificationResponse |
Method | addDebugNotification | See INotificationResponse . |
Method | addInfoNotification | See INotificationResponse . |
Method | addWarningNotification | See INotificationResponse . |
Method | addErrorNotification | See INotificationResponse . |