changeset 1186:352865f4a268

server: added a generic way to have notification messages in pages
author Goffi <goffi@goffi.org>
date Sun, 26 May 2019 22:16:07 +0200
parents 7d6c0e5d5f34
children dab7a2b151ea
files libervia/server/classes.py libervia/server/pages.py libervia/server/session_iface.py
diffstat 3 files changed, 32 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- 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"))
--- 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:
--- 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