Mercurial > libervia-web
annotate 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 |
rev | line source |
---|---|
919
7b267496da1d
server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/python |
7b267496da1d
server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
7b267496da1d
server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
7b267496da1d
server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 # Libervia: a SAT frontend |
7b267496da1d
server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org) |
7b267496da1d
server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 |
7b267496da1d
server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
7b267496da1d
server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
7b267496da1d
server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
7b267496da1d
server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 # (at your option) any later version. |
7b267496da1d
server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 |
7b267496da1d
server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
7b267496da1d
server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
7b267496da1d
server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
7b267496da1d
server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 # GNU Affero General Public License for more details. |
7b267496da1d
server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 |
7b267496da1d
server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
7b267496da1d
server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
7b267496da1d
server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 from zope.interface import Interface, Attribute, implements |
947
92f0eeb6dc72
pages: cache identities identities in session + get identities for comments in blog
Goffi <goffi@goffi.org>
parents:
919
diff
changeset
|
20 from sat.tools.common import data_objects |
919
7b267496da1d
server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 import shortuuid |
7b267496da1d
server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 |
960
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
23 FLAGS_KEY = '_flags' |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
24 |
919
7b267496da1d
server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 class ISATSession(Interface): |
7b267496da1d
server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 profile = Attribute("Sat profile") |
7b267496da1d
server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 jid = Attribute("JID associated with the profile") |
7b267496da1d
server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 uuid = Attribute("uuid associated with the profile session") |
947
92f0eeb6dc72
pages: cache identities identities in session + get identities for comments in blog
Goffi <goffi@goffi.org>
parents:
919
diff
changeset
|
29 identities = Attribute("Identities of XMPP entities") |
919
7b267496da1d
server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 |
7b267496da1d
server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 |
7b267496da1d
server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 class SATSession(object): |
7b267496da1d
server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 implements(ISATSession) |
7b267496da1d
server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 |
7b267496da1d
server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 def __init__(self, session): |
7b267496da1d
server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 self.profile = None |
7b267496da1d
server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 self.jid = None |
7b267496da1d
server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 self.uuid = unicode(shortuuid.uuid()) |
947
92f0eeb6dc72
pages: cache identities identities in session + get identities for comments in blog
Goffi <goffi@goffi.org>
parents:
919
diff
changeset
|
39 self.identities = data_objects.Identities() |
956
dabecab10faa
server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents:
947
diff
changeset
|
40 self.csrf_token = unicode(shortuuid.uuid()) |
960
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
41 self.pages_data = {} # used to keep data accross reloads (key is page instance) |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
42 |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
43 def getPageData(self, page, key): |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
44 """get session data for a page |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
45 |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
46 @param page(LiberviaPage): instance of the page |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
47 @param key(object): data key |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
48 return (None, object): value of the key |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
49 None if not found or page_data doesn't exist |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
50 """ |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
51 return self.pages_data.get(page, {}).get(key) |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
52 |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
53 def popPageData(self, page, key, default=None): |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
54 """like getPageData, but remove key once value is gotten |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
55 |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
56 @param page(LiberviaPage): instance of the page |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
57 @param key(object): data key |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
58 @param default(object): value to return if key is not found |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
59 @return (object): found value or default |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
60 """ |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
61 page_data = self.pages_data.get(page) |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
62 if page_data is None: |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
63 return default |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
64 value = page_data.pop(key, default) |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
65 if not page_data: |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
66 # no need to keep unused page_data |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
67 del self.pages_data[page] |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
68 return value |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
69 |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
70 def setPageData(self, page, key, value): |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
71 """set data to persist on reload |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
72 |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
73 @param page(LiberviaPage): instance of the page |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
74 @param key(object): data key |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
75 @param value(object): value to set |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
76 @return (object): set value |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
77 """ |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
78 page_data = self.pages_data.setdefault(page, {}) |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
79 page_data[key] = value |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
80 return value |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
81 |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
82 def setPageFlag(self, page, flag): |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
83 """set a flag for this page |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
84 |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
85 @param page(LiberviaPage): instance of the page |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
86 @param flag(unicode): flag to set |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
87 """ |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
88 flags = self.getPageData(page, FLAGS_KEY) |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
89 if flags is None: |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
90 flags = self.setPageData(page, FLAGS_KEY, set()) |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
91 flags.add(flag) |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
92 |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
93 def popPageFlag(self, page, flag): |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
94 """return True if flag is set |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
95 |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
96 flag is removed if it was set |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
97 @param page(LiberviaPage): instance of the page |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
98 @param flag(unicode): flag to set |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
99 @return (bool): True if flaag was set |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
100 """ |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
101 page_data = self.pages_data.get(page, {}) |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
102 flags = page_data.get(FLAGS_KEY) |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
103 if flags is None: |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
104 return False |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
105 if flag in flags: |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
106 flags.remove(flag) |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
107 # we remove data if they are not used anymore |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
108 if not flags: |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
109 del page_data[FLAGS_KEY] |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
110 if not page_data: |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
111 del self.pages_data[page] |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
112 return True |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
113 else: |
e59edcae4c18
pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents:
957
diff
changeset
|
114 return False |
919
7b267496da1d
server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
115 |
7b267496da1d
server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
116 |
7b267496da1d
server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
117 class ISATGuestSession(Interface): |
7b267496da1d
server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
118 id = Attribute("UUID of the guest") |
7b267496da1d
server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
119 data = Attribute("data associated with the guest") |
7b267496da1d
server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
120 |
7b267496da1d
server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
121 |
7b267496da1d
server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
122 class SATGuestSession(object): |
7b267496da1d
server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
123 implements(ISATGuestSession) |
7b267496da1d
server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
124 |
7b267496da1d
server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
125 def __init__(self, session): |
7b267496da1d
server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
126 self.id = None |
7b267496da1d
server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
127 self.data = None |