Mercurial > libervia-web
annotate libervia/pages/_browser/bridge.py @ 1306:c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
`template` module has been update so most SàT template can be run from browser:
- `profile` and `csrf_token` are set as globals
- an implementation of `xmlattr` filter has been added
- `date_fmt` filter has been implemented using `moment.js`
- i18n method `_` has been added to globals, and `{% trans %}` statement has been
implemented using an extension. For now they are not actually translating but just
returning the unmodified string.
- new `get_args` helper method to handle `nunjucks` convention for arguments.
- fixed `get_elt` to only return the first child element (avoiding any text child) + added
a defaut value for `context`
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 16 Jul 2020 09:08:50 +0200 |
parents | 999dccf0093e |
children | 8998f01088ac |
rev | line source |
---|---|
1297
999dccf0093e
browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 from browser import window |
999dccf0093e
browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 import javascript |
999dccf0093e
browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
999dccf0093e
browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 |
999dccf0093e
browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 class Bridge: |
999dccf0093e
browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 |
999dccf0093e
browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 def __getattr__(self, attr): |
999dccf0093e
browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 return lambda *args, **kwargs: self.call(attr, *args, **kwargs) |
999dccf0093e
browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 |
999dccf0093e
browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 def on_load(self, xhr, ev, callback, errback): |
999dccf0093e
browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 if xhr.status == 200: |
999dccf0093e
browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 ret = javascript.JSON.parse(xhr.response) |
999dccf0093e
browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 if ret is None: |
999dccf0093e
browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 callback() |
999dccf0093e
browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 else: |
999dccf0093e
browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 callback(ret) |
999dccf0093e
browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 elif xhr.status == 502: |
999dccf0093e
browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 # PROXY_ERROR is used for bridge error |
999dccf0093e
browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 ret = javascript.JSON.parse(xhr.response) |
999dccf0093e
browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 errback(ret) |
999dccf0093e
browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 else: |
999dccf0093e
browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 print(f"bridge called failed: code: {xhr.response}, text: {xhr.statusText}") |
999dccf0093e
browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 errback({"fullname": "InternalError", "message": xhr.statusText}) |
999dccf0093e
browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 |
999dccf0093e
browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 def call(self, method_name, *args, callback, errback, **kwargs): |
999dccf0093e
browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 xhr = window.XMLHttpRequest.new() |
999dccf0093e
browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 xhr.bind('load', lambda ev: self.on_load(xhr, ev, callback, errback)) |
999dccf0093e
browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 xhr.bind('error', lambda ev: errback( |
999dccf0093e
browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 {"fullname": "ConnectionError", "message": xhr.statusText})) |
999dccf0093e
browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 xhr.open("POST", f"/_bridge/{method_name}", True) |
999dccf0093e
browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 data = javascript.JSON.stringify({ |
999dccf0093e
browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 "args": args, |
999dccf0093e
browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 "kwargs": kwargs, |
999dccf0093e
browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 }) |
999dccf0093e
browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 xhr.setRequestHeader('X-Csrf-Token', window.csrf_token) |
999dccf0093e
browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 xhr.send(data) |