Mercurial > libervia-web
annotate src/server/utils.py @ 1102:8b70c838ed14
login (logged): set guest_session and session_started template variables
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 01 Jun 2018 18:33:14 +0200 |
parents | 63ed5f6bd4eb |
children | cdd389ef97bc |
rev | line source |
---|---|
984
f0fc28b3bd1e
server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/python |
f0fc28b3bd1e
server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
f0fc28b3bd1e
server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
f0fc28b3bd1e
server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 # Libervia: a Salut à Toi frontend |
1054 | 5 # Copyright (C) 2011-2018 Jérôme Poisson <goffi@goffi.org> |
984
f0fc28b3bd1e
server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 |
f0fc28b3bd1e
server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
f0fc28b3bd1e
server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
f0fc28b3bd1e
server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
f0fc28b3bd1e
server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 # (at your option) any later version. |
f0fc28b3bd1e
server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 |
f0fc28b3bd1e
server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
f0fc28b3bd1e
server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
f0fc28b3bd1e
server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
f0fc28b3bd1e
server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 # GNU Affero General Public License for more details. |
f0fc28b3bd1e
server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 |
f0fc28b3bd1e
server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
f0fc28b3bd1e
server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
1063
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
19 from sat.core.i18n import _ |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
20 from twisted.internet import reactor |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
21 from twisted.internet import defer |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
22 from sat.core import exceptions |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
23 from sat.core.log import getLogger |
984
f0fc28b3bd1e
server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 import urllib |
1063
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
25 log = getLogger(__name__) |
984
f0fc28b3bd1e
server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 |
f0fc28b3bd1e
server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 |
1027
46ce20494a1b
server (utils): added "safe" attribute to quote
Goffi <goffi@goffi.org>
parents:
1023
diff
changeset
|
28 def quote(value, safe='@'): |
984
f0fc28b3bd1e
server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 """shortcut to quote an unicode value for URL""" |
1027
46ce20494a1b
server (utils): added "safe" attribute to quote
Goffi <goffi@goffi.org>
parents:
1023
diff
changeset
|
30 return urllib.quote(value.encode('utf-8'), safe=safe) |
1063
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
31 |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
32 |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
33 class ProgressHandler(object): |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
34 """class to help the management of progressions""" |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
35 handlers = {} |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
36 |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
37 def __init__(self, host, progress_id, profile): |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
38 self.host = host |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
39 self.progress_id = progress_id |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
40 self.profile = profile |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
41 |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
42 @classmethod |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
43 def _signal(cls, name, progress_id, data, profile): |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
44 handlers = cls.handlers |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
45 if profile in handlers and progress_id in handlers[profile]: |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
46 handler_data = handlers[profile][progress_id] |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
47 timeout = handler_data[u'timeout'] |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
48 if timeout.active(): |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
49 timeout.cancel() |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
50 cb = handler_data[name] |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
51 if cb is not None: |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
52 cb(data) |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
53 if name == u'started': |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
54 pass |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
55 elif name == u'finished': |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
56 handler_data[u'deferred'].callback(data) |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
57 handler_data[u'instance'].unregister_handler() |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
58 elif name == u'error': |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
59 handler_data[u'deferred'].errback(Exception(data)) |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
60 handler_data[u'instance'].unregister_handler() |
1067
808ec98de8b3
pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents:
1063
diff
changeset
|
61 else: |
808ec98de8b3
pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents:
1063
diff
changeset
|
62 log.error(u'unexpected signal: {name}'.format(name=name)) |
1063
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
63 |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
64 def _timeout(self): |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
65 log.warning(_(u"No progress received, cancelling handler: {progress_id} [{profile}]").format( |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
66 progress_id = self.progress_id, profile = self.profile)) |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
67 |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
68 def unregister_handler(self): |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
69 """remove a previously registered handler""" |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
70 try: |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
71 del self.handlers[self.profile][self.progress_id] |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
72 except KeyError: |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
73 log.warning(_(u"Trying to remove unknown handler: {progress_id} [{profile}]").format( |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
74 progress_id = self.progress_id, profile = self.profile)) |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
75 else: |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
76 if not self.handlers[self.profile]: |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
77 self.handlers[self.profile] |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
78 |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
79 def register(self, started_cb=None, finished_cb=None, error_cb=None, timeout=30): |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
80 """register the signals to handle progression |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
81 |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
82 @param started_cb(callable, None): method to call when progressStarted signal is received |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
83 @param finished_cb(callable, None): method to call when progressFinished signal is received |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
84 @param error_cb(callable, None): method to call when progressError signal is received |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
85 @param timeout(int): progress time out |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
86 if nothing happen in this progression during this delay, |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
87 an exception is raised |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
88 @return (D(dict[unicode,unicode])): a deferred called when progression is finished |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
89 """ |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
90 handler_data = self.handlers.setdefault(self.profile, {}).setdefault(self.progress_id, {}) |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
91 if handler_data: |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
92 raise exceptions.ConflictError(u"There is already one handler for this progression") |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
93 handler_data[u'instance'] = self |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
94 deferred = handler_data[u'deferred'] = defer.Deferred() |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
95 handler_data[u'started'] = started_cb |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
96 handler_data[u'finished'] = finished_cb |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
97 handler_data[u'error'] = error_cb |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
98 handler_data[u'timeout'] = reactor.callLater(timeout, self._timeout) |
4b69f69c6ffd
server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
99 return deferred |
1092
63ed5f6bd4eb
pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents:
1067
diff
changeset
|
100 |
63ed5f6bd4eb
pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents:
1067
diff
changeset
|
101 |
63ed5f6bd4eb
pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents:
1067
diff
changeset
|
102 class SubPage(unicode): |
63ed5f6bd4eb
pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents:
1067
diff
changeset
|
103 """use to mark subpages when generating a page path""" |