Part of canonical.launchpad.webapp.notifications View In Hierarchy
Known subclasses: canonical.launchpad.webapp.servers.LaunchpadBrowserResponse
Implements interfaces: canonical.launchpad.webapp.interfaces.INotificationResponse
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 behavior.
>>> 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.addNoticeNotification('Notice') >>> response.addWarningNotification('Warning')
And an odd one to test Bug #54987
>>> from canonical.launchpad import _ >>> response.addErrorNotification(_('Error${value}', mapping={'value':''}))
>>> INotificationList.providedBy(response.notifications) True
>>> for notification in response.notifications: ... print "%d -- %s" % (notification.level, notification.message) 25 -- <b><Fnord></b> 10 -- Whatever 10 -- Debug 20 -- Info 25 -- Notice 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 25 -- <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'] >>> session.has_key('notifications') False >>> len(response.notifications) 0 >>> response.redirect("http://example.com") 302: http://example.com >>> session.has_key('notifications') False
Method | addNotification | See INotificationResponse . |
Method | notifications | Undocumented |
Method | removeAllNotifications | See canonical.launchpad.webapp.interfaces.INotificationResponse |
Method | redirect | See canonical.launchpad.webapp.interfaces.INotificationResponse |
Method | addDebugNotification | See INotificationResponse . |
Method | addInfoNotification | See INotificationResponse . |
Method | addNoticeNotification | See INotificationResponse . |
Method | addWarningNotification | See INotificationResponse . |
Method | addErrorNotification | See INotificationResponse . |