Mercurial > libervia-web
annotate libervia/pages/_browser/aio_bridge.py @ 1406:cffa3ae4d0aa
pages (blog/view): move URL friendly code to backend tools:
- the code to render an URL friendly is now in `sat.tools.common.regex`
- user friendly extra text is now only displayed when no `-` is found in ID. This is a
temporary transition behaviour because new blog items IDs are now user friendly by
default, and thus extra text is not wanted anymore.
For older IDs it is still needed though, and the presence of `-` is used to guess when
an ID is user friendly or not.
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 16 Apr 2021 18:44:49 +0200 |
parents | 72f9639594b2 |
children | 106bae41f5c8 |
rev | line source |
---|---|
1347 | 1 from browser import window, aio |
2 import javascript | |
3 | |
4 | |
5 class BridgeException(Exception): | |
6 """An exception which has been raised from the backend and arrived to the frontend.""" | |
7 | |
8 def __init__(self, name, message="", condition=""): | |
9 """ | |
10 | |
11 @param name (str): full exception class name (with module) | |
12 @param message (str): error message | |
13 @param condition (str) : error condition | |
14 """ | |
15 Exception.__init__(self) | |
16 self.fullname = str(name) | |
17 self.message = str(message) | |
18 self.condition = str(condition) if condition else "" | |
19 self.module, __, self.classname = str(self.fullname).rpartition(".") | |
20 | |
21 def __str__(self): | |
1394
72f9639594b2
browser (lists): code is now async + item removal for non creator:
Goffi <goffi@goffi.org>
parents:
1347
diff
changeset
|
22 return f"{self.classname}: {self.message or ''}" |
1347 | 23 |
24 def __eq__(self, other): | |
25 return self.classname == other | |
26 | |
27 | |
28 class Bridge: | |
29 | |
30 def __getattr__(self, attr): | |
31 return lambda *args, **kwargs: self.call(attr, *args, **kwargs) | |
32 | |
33 async def call(self, method_name, *args, **kwargs): | |
34 data = javascript.JSON.stringify({ | |
35 "args": args, | |
36 "kwargs": kwargs, | |
37 }) | |
38 url = f"/_bridge/{method_name}" | |
39 r = await aio.post( | |
40 url, | |
41 headers={ | |
42 'X-Csrf-Token': window.csrf_token, | |
43 }, | |
44 data=data, | |
45 ) | |
46 | |
47 if r.status == 200: | |
48 return javascript.JSON.parse(r.data) | |
49 elif r.status == 502: | |
50 ret = javascript.JSON.parse(r.data) | |
51 raise BridgeException(ret['fullname'], ret['message'], ret['condition']) | |
52 else: | |
53 print(f"bridge called failed: code: {r.status}, text: {r.statusText}") | |
54 raise BridgeException("InternalError", r.statusText) |