diff libervia/server/session_iface.py @ 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 6424d3684d1e
children b2d067339de3
line wrap: on
line diff
--- 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