# HG changeset patch # User Goffi # Date 1558901767 -7200 # Node ID 352865f4a26804a9c42440dac44a6e95ab051b2a # Parent 7d6c0e5d5f349da2470cd5f66df9335dcc9cab04 server: added a generic way to have notification messages in pages diff -r 7d6c0e5d5f34 -r 352865f4a268 libervia/server/classes.py --- a/libervia/server/classes.py Sun May 26 22:14:23 2019 +0200 +++ b/libervia/server/classes.py Sun May 26 22:16:07 2019 +0200 @@ -23,3 +23,4 @@ from collections import namedtuple WebsocketMeta = namedtuple("WebsocketMeta", ("url", "token", "debug")) +Notification = namedtuple("Notification", ("message", "level")) diff -r 7d6c0e5d5f34 -r 352865f4a268 libervia/server/pages.py --- a/libervia/server/pages.py Sun May 26 22:14:23 2019 +0200 +++ b/libervia/server/pages.py Sun May 26 22:16:07 2019 +0200 @@ -1220,6 +1220,9 @@ session_data = self.host.getSessionData(request, session_iface.ISATSession) if session_data.popPageFlag(self, C.FLAG_CONFIRM): template_data[u"confirm"] = True + notifs = session_data.popPageNotifications(self) + if notifs: + template_data[u"notifications"] = notifs if session_data.locale is not None: template_data[u'locale'] = session_data.locale if self.vhost_root.site_name: diff -r 7d6c0e5d5f34 -r 352865f4a268 libervia/server/session_iface.py --- a/libervia/server/session_iface.py Sun May 26 22:14:23 2019 +0200 +++ b/libervia/server/session_iface.py Sun May 26 22:16:07 2019 +0200 @@ -19,12 +19,14 @@ from zope.interface import Interface, Attribute, implements from sat.tools.common import data_objects from libervia.server.constants import Const as C +from libervia.server.classes import Notification from collections import OrderedDict import os.path import shortuuid import time FLAGS_KEY = "_flags" +NOTIFICATIONS_KEY = "_notifications" MAX_CACHE_AFFILIATIONS = 100 # number of nodes to keep in cache @@ -142,6 +144,32 @@ else: return False + def setPageNotification(self, page, message, level=C.LVL_INFO): + """set a flag for this page + + @param page(LiberviaPage): instance of the page + @param flag(unicode): flag to set + """ + notif = Notification(message, level) + notifs = self.getPageData(page, NOTIFICATIONS_KEY) + if notifs is None: + notifs = self.setPageData(page, NOTIFICATIONS_KEY, []) + notifs.append(notif) + + def popPageNotifications(self, page): + """Return and remove last page notification + + @param page(LiberviaPage): instance of the page + @return (list[Notification]): notifications if any + """ + page_data = self.pages_data.get(page, {}) + notifs = page_data.get(NOTIFICATIONS_KEY) + if not notifs: + return [] + ret = notifs[:] + del notifs[:] + return ret + def getAffiliation(self, service, node): """retrieve affiliation for a pubsub node