annotate libervia/server/session_iface.py @ 1169:97e850e6fae9

pages (i18n): if locale is not specified, "accept-language" header is used to try to determine the best one
author Goffi <goffi@goffi.org>
date Fri, 12 Apr 2019 14:08:02 +0200
parents 6424d3684d1e
children 352865f4a268
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
1144
2af117bfe6cc dates update
Goffi <goffi@goffi.org>
parents: 1124
diff changeset
5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org)
919
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
995
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 964
diff changeset
21 from libervia.server.constants import Const as C
1093
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
22 from collections import OrderedDict
995
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 964
diff changeset
23 import os.path
919
7b267496da1d server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff changeset
24 import shortuuid
1090
9c41b7e91172 server: set jid and started time in session on log-in:
Goffi <goffi@goffi.org>
parents: 1055
diff changeset
25 import time
919
7b267496da1d server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff changeset
26
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1101
diff changeset
27 FLAGS_KEY = "_flags"
1093
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
28 MAX_CACHE_AFFILIATIONS = 100 # number of nodes to keep in cache
960
e59edcae4c18 pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents: 957
diff changeset
29
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1101
diff changeset
30
919
7b267496da1d server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff changeset
31 class ISATSession(Interface):
7b267496da1d server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff changeset
32 profile = Attribute("Sat profile")
7b267496da1d server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff changeset
33 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
34 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
35 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
36
7b267496da1d server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff changeset
37
7b267496da1d server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff changeset
38 class SATSession(object):
7b267496da1d server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff changeset
39 implements(ISATSession)
7b267496da1d server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff changeset
40
7b267496da1d server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff changeset
41 def __init__(self, session):
7b267496da1d server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff changeset
42 self.profile = None
7b267496da1d server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff changeset
43 self.jid = None
1090
9c41b7e91172 server: set jid and started time in session on log-in:
Goffi <goffi@goffi.org>
parents: 1055
diff changeset
44 self.started = time.time()
9c41b7e91172 server: set jid and started time in session on log-in:
Goffi <goffi@goffi.org>
parents: 1055
diff changeset
45 # time when the backend session was started
9c41b7e91172 server: set jid and started time in session on log-in:
Goffi <goffi@goffi.org>
parents: 1055
diff changeset
46 self.backend_started = None
919
7b267496da1d server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff changeset
47 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
48 self.identities = data_objects.Identities()
956
dabecab10faa server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents: 947
diff changeset
49 self.csrf_token = unicode(shortuuid.uuid())
1165
6424d3684d1e pages: locale handling:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
50 self.locale = None # i18n of the pages
960
e59edcae4c18 pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents: 957
diff changeset
51 self.pages_data = {} # used to keep data accross reloads (key is page instance)
1093
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
52 self.affiliations = OrderedDict() # cache for node affiliations
960
e59edcae4c18 pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents: 957
diff changeset
53
995
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 964
diff changeset
54 @property
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 964
diff changeset
55 def cache_dir(self):
1055
c2037b44f84e server: fixed cache_dir for service profile in session interface
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
56 if self.profile is None:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1101
diff changeset
57 return self.service_cache_url + u"/"
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1101
diff changeset
58 return os.path.join(u"/", C.CACHE_DIR, self.uuid) + u"/"
995
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 964
diff changeset
59
1094
8a270f32de81 server (session): new "connected" property, which is True when a user is logged
Goffi <goffi@goffi.org>
parents: 1093
diff changeset
60 @property
8a270f32de81 server (session): new "connected" property, which is True when a user is logged
Goffi <goffi@goffi.org>
parents: 1093
diff changeset
61 def connected(self):
8a270f32de81 server (session): new "connected" property, which is True when a user is logged
Goffi <goffi@goffi.org>
parents: 1093
diff changeset
62 return self.profile is not None
8a270f32de81 server (session): new "connected" property, which is True when a user is logged
Goffi <goffi@goffi.org>
parents: 1093
diff changeset
63
1101
9f7a628ce893 server (session): new "guest" property, True when connected user is a guest
Goffi <goffi@goffi.org>
parents: 1094
diff changeset
64 @property
9f7a628ce893 server (session): new "guest" property, True when connected user is a guest
Goffi <goffi@goffi.org>
parents: 1094
diff changeset
65 def guest(self):
9f7a628ce893 server (session): new "guest" property, True when connected user is a guest
Goffi <goffi@goffi.org>
parents: 1094
diff changeset
66 """True if this is a guest session"""
9f7a628ce893 server (session): new "guest" property, True when connected user is a guest
Goffi <goffi@goffi.org>
parents: 1094
diff changeset
67 if self.profile is None:
9f7a628ce893 server (session): new "guest" property, True when connected user is a guest
Goffi <goffi@goffi.org>
parents: 1094
diff changeset
68 return False
9f7a628ce893 server (session): new "guest" property, True when connected user is a guest
Goffi <goffi@goffi.org>
parents: 1094
diff changeset
69 else:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1101
diff changeset
70 return self.profile.startswith("guest@@")
1101
9f7a628ce893 server (session): new "guest" property, True when connected user is a guest
Goffi <goffi@goffi.org>
parents: 1094
diff changeset
71
960
e59edcae4c18 pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents: 957
diff changeset
72 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
73 """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
74
e59edcae4c18 pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents: 957
diff changeset
75 @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
76 @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
77 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
78 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
79 """
e59edcae4c18 pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents: 957
diff changeset
80 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
81
e59edcae4c18 pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents: 957
diff changeset
82 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
83 """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
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 key(object): data key
e59edcae4c18 pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents: 957
diff changeset
87 @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
88 @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
89 """
e59edcae4c18 pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents: 957
diff changeset
90 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
91 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
92 return default
e59edcae4c18 pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents: 957
diff changeset
93 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
94 if not page_data:
e59edcae4c18 pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents: 957
diff changeset
95 # 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
96 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
97 return value
e59edcae4c18 pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents: 957
diff changeset
98
e59edcae4c18 pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents: 957
diff changeset
99 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
100 """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
101
e59edcae4c18 pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents: 957
diff changeset
102 @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
103 @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
104 @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
105 @return (object): set value
e59edcae4c18 pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents: 957
diff changeset
106 """
e59edcae4c18 pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents: 957
diff changeset
107 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
108 page_data[key] = value
e59edcae4c18 pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents: 957
diff changeset
109 return value
e59edcae4c18 pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents: 957
diff changeset
110
e59edcae4c18 pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents: 957
diff changeset
111 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
112 """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
113
e59edcae4c18 pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents: 957
diff changeset
114 @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
115 @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
116 """
e59edcae4c18 pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents: 957
diff changeset
117 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
118 if flags is None:
e59edcae4c18 pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents: 957
diff changeset
119 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
120 flags.add(flag)
e59edcae4c18 pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents: 957
diff changeset
121
e59edcae4c18 pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents: 957
diff changeset
122 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
123 """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
124
e59edcae4c18 pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents: 957
diff changeset
125 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
126 @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
127 @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
128 @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
129 """
e59edcae4c18 pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents: 957
diff changeset
130 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
131 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
132 if flags is None:
e59edcae4c18 pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents: 957
diff changeset
133 return False
e59edcae4c18 pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents: 957
diff changeset
134 if flag in flags:
e59edcae4c18 pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents: 957
diff changeset
135 flags.remove(flag)
e59edcae4c18 pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents: 957
diff changeset
136 # 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
137 if not flags:
e59edcae4c18 pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents: 957
diff changeset
138 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
139 if not page_data:
e59edcae4c18 pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents: 957
diff changeset
140 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
141 return True
e59edcae4c18 pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents: 957
diff changeset
142 else:
e59edcae4c18 pages(session): added method to handle reload resistant page specific data:
Goffi <goffi@goffi.org>
parents: 957
diff changeset
143 return False
919
7b267496da1d server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff changeset
144
1093
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
145 def getAffiliation(self, service, node):
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
146 """retrieve affiliation for a pubsub node
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
147
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
148 @param service(jid.JID): pubsub service
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
149 @param node(unicode): pubsub node
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
150 @return (unicode, None): affiliation, or None if it is not in cache
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
151 """
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
152 if service.resource:
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
153 raise ValueError(u"Service must not have a resource")
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
154 if not node:
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
155 raise ValueError(u"node must be set")
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
156 try:
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
157 affiliation = self.affiliations.pop((service, node))
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
158 except KeyError:
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
159 return None
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
160 else:
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
161 # we replace at the top to get the most recently used on top
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
162 # so less recently used will be removed if cache is full
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
163 self.affiliations[(service, node)] = affiliation
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
164 return affiliation
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
165
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
166 def setAffiliation(self, service, node, affiliation):
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
167 """cache affiliation for a node
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
168
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
169 will empty cache when it become too big
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
170 @param service(jid.JID): pubsub service
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
171 @param node(unicode): pubsub node
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
172 @param affiliation(unicode): affiliation to this node
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
173 """
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
174 if service.resource:
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
175 raise ValueError(u"Service must not have a resource")
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
176 if not node:
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
177 raise ValueError(u"node must be set")
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
178 self.affiliations[(service, node)] = affiliation
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
179 while len(self.affiliations) > MAX_CACHE_AFFILIATIONS:
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
180 self.affiliations.popitem(last=False)
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
181
919
7b267496da1d server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff changeset
182
7b267496da1d server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff changeset
183 class ISATGuestSession(Interface):
7b267496da1d server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff changeset
184 id = Attribute("UUID of the guest")
7b267496da1d server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff changeset
185 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
186
7b267496da1d server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff changeset
187
7b267496da1d server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff changeset
188 class SATGuestSession(object):
7b267496da1d server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff changeset
189 implements(ISATGuestSession)
7b267496da1d server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff changeset
190
7b267496da1d server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff changeset
191 def __init__(self, session):
7b267496da1d server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff changeset
192 self.id = None
7b267496da1d server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents:
diff changeset
193 self.data = None