Mercurial > libervia-web
view libervia/pages/_browser/aio_bridge.py @ 1417:314bba1ae433
pages: breadcrumbs handling:
a new `breadcrumbs` list of dict is created in `template_data`. By default it is
automatically filled by pages run to reach the requested URI, but a page can customize it.
A breadcrumb data dict must have a `label`, should have an `url` and may have an `icon`
(which is the name of a SàT Media well-known icon).
Pages may now have a `label` attribute, which is used to automatically fill the crumb
(otherwise page name then URI is used).
A new `add_breadcrumb` method can be used to manually breadcrumb data, in which case auto-filling is disabled.
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 29 Apr 2021 20:48:35 +0200 |
parents | 72f9639594b2 |
children | 106bae41f5c8 |
line wrap: on
line source
from browser import window, aio import javascript class BridgeException(Exception): """An exception which has been raised from the backend and arrived to the frontend.""" def __init__(self, name, message="", condition=""): """ @param name (str): full exception class name (with module) @param message (str): error message @param condition (str) : error condition """ Exception.__init__(self) self.fullname = str(name) self.message = str(message) self.condition = str(condition) if condition else "" self.module, __, self.classname = str(self.fullname).rpartition(".") def __str__(self): return f"{self.classname}: {self.message or ''}" def __eq__(self, other): return self.classname == other class Bridge: def __getattr__(self, attr): return lambda *args, **kwargs: self.call(attr, *args, **kwargs) async def call(self, method_name, *args, **kwargs): data = javascript.JSON.stringify({ "args": args, "kwargs": kwargs, }) url = f"/_bridge/{method_name}" r = await aio.post( url, headers={ 'X-Csrf-Token': window.csrf_token, }, data=data, ) if r.status == 200: return javascript.JSON.parse(r.data) elif r.status == 502: ret = javascript.JSON.parse(r.data) raise BridgeException(ret['fullname'], ret['message'], ret['condition']) else: print(f"bridge called failed: code: {r.status}, text: {r.statusText}") raise BridgeException("InternalError", r.statusText)