comparison src/server/session_iface.py @ 960:e59edcae4c18

pages(session): added method to handle reload resistant page specific data: Liberiva Page can now register session data specific to a page using ISatSession methods (see docstrings for details). Those data can resist a page reload, are specific to a session and a page.
author Goffi <goffi@goffi.org>
date Fri, 27 Oct 2017 18:31:42 +0200
parents 67bf14c91d5c
children fd4eae654182
comparison
equal deleted inserted replaced
959:968eda9e982a 960:e59edcae4c18
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 from zope.interface import Interface, Attribute, implements 19 from zope.interface import Interface, Attribute, implements
20 from sat.tools.common import data_objects 20 from sat.tools.common import data_objects
21 import shortuuid 21 import shortuuid
22 22
23 FLAGS_KEY = '_flags'
24
23 class ISATSession(Interface): 25 class ISATSession(Interface):
24 profile = Attribute("Sat profile") 26 profile = Attribute("Sat profile")
25 jid = Attribute("JID associated with the profile") 27 jid = Attribute("JID associated with the profile")
26 uuid = Attribute("uuid associated with the profile session") 28 uuid = Attribute("uuid associated with the profile session")
27 identities = Attribute("Identities of XMPP entities") 29 identities = Attribute("Identities of XMPP entities")
34 self.profile = None 36 self.profile = None
35 self.jid = None 37 self.jid = None
36 self.uuid = unicode(shortuuid.uuid()) 38 self.uuid = unicode(shortuuid.uuid())
37 self.identities = data_objects.Identities() 39 self.identities = data_objects.Identities()
38 self.csrf_token = unicode(shortuuid.uuid()) 40 self.csrf_token = unicode(shortuuid.uuid())
39 self.flags = set() 41 self.pages_data = {} # used to keep data accross reloads (key is page instance)
42
43 def getPageData(self, page, key):
44 """get session data for a page
45
46 @param page(LiberviaPage): instance of the page
47 @param key(object): data key
48 return (None, object): value of the key
49 None if not found or page_data doesn't exist
50 """
51 return self.pages_data.get(page, {}).get(key)
52
53 def popPageData(self, page, key, default=None):
54 """like getPageData, but remove key once value is gotten
55
56 @param page(LiberviaPage): instance of the page
57 @param key(object): data key
58 @param default(object): value to return if key is not found
59 @return (object): found value or default
60 """
61 page_data = self.pages_data.get(page)
62 if page_data is None:
63 return default
64 value = page_data.pop(key, default)
65 if not page_data:
66 # no need to keep unused page_data
67 del self.pages_data[page]
68 return value
69
70 def setPageData(self, page, key, value):
71 """set data to persist on reload
72
73 @param page(LiberviaPage): instance of the page
74 @param key(object): data key
75 @param value(object): value to set
76 @return (object): set value
77 """
78 page_data = self.pages_data.setdefault(page, {})
79 page_data[key] = value
80 return value
81
82 def setPageFlag(self, page, flag):
83 """set a flag for this page
84
85 @param page(LiberviaPage): instance of the page
86 @param flag(unicode): flag to set
87 """
88 flags = self.getPageData(page, FLAGS_KEY)
89 if flags is None:
90 flags = self.setPageData(page, FLAGS_KEY, set())
91 flags.add(flag)
92
93 def popPageFlag(self, page, flag):
94 """return True if flag is set
95
96 flag is removed if it was set
97 @param page(LiberviaPage): instance of the page
98 @param flag(unicode): flag to set
99 @return (bool): True if flaag was set
100 """
101 page_data = self.pages_data.get(page, {})
102 flags = page_data.get(FLAGS_KEY)
103 if flags is None:
104 return False
105 if flag in flags:
106 flags.remove(flag)
107 # we remove data if they are not used anymore
108 if not flags:
109 del page_data[FLAGS_KEY]
110 if not page_data:
111 del self.pages_data[page]
112 return True
113 else:
114 return False
40 115
41 116
42 class ISATGuestSession(Interface): 117 class ISATGuestSession(Interface):
43 id = Attribute("UUID of the guest") 118 id = Attribute("UUID of the guest")
44 data = Attribute("data associated with the guest") 119 data = Attribute("data associated with the guest")