comparison libervia/server/pages.py @ 1286:63328c793a9e

pages: new method `exposeToScripts` to make variables accessible to scripts in browser
author Goffi <goffi@goffi.org>
date Fri, 19 Jun 2020 16:47:51 +0200
parents 37a582b0fe53
children de3b15d68bb6
comparison
equal deleted inserted replaced
1285:37a582b0fe53 1286:63328c793a9e
43 from sat.tools.common import date_utils 43 from sat.tools.common import date_utils
44 from sat.tools.common import utils 44 from sat.tools.common import utils
45 from sat.core.log import getLogger 45 from sat.core.log import getLogger
46 from sat_frontends.bridge.bridge_frontend import BridgeException 46 from sat_frontends.bridge.bridge_frontend import BridgeException
47 47
48 from libervia.server.constants import Const as C 48 from .constants import Const as C
49 from libervia.server import session_iface 49 from . import session_iface
50 from libervia.server.utils import quote, SubPage 50 from .utils import quote, SubPage
51 from libervia.server.classes import WebsocketMeta 51 from .classes import WebsocketMeta
52 from .classes import Script
52 53
53 log = getLogger(__name__) 54 log = getLogger(__name__)
54 55
55 56
56 class CacheBase(object): 57 class CacheBase(object):
539 url=request.uri, ip=request.getClientIP() 540 url=request.uri, ip=request.getClientIP()
540 ) 541 )
541 ) 542 )
542 self.pageError(request, C.HTTP_FORBIDDEN) 543 self.pageError(request, C.HTTP_FORBIDDEN)
543 544
545 def exposeToScripts(
546 self,
547 request: server.Request,
548 **kwargs: str
549 ) -> None:
550 """Make a local variable available to page script as a global variable
551
552 No check is done for conflicting name, use this carefully
553 """
554 template_data = request.template_data
555 scripts = template_data.setdefault("scripts", utils.OrderedSet())
556 for name, value in kwargs.items():
557 scripts.add(Script(content=f"var {name}={value!r};"))
558
544 def registerURI(self, uri_tuple, get_uri_cb): 559 def registerURI(self, uri_tuple, get_uri_cb):
545 """Register a URI handler 560 """Register a URI handler
546 561
547 @param uri_tuple(tuple[unicode, unicode]): type or URIs handler 562 @param uri_tuple(tuple[unicode, unicode]): type or URIs handler
548 type/subtype as returned by tools/common/parseXMPPUri 563 type/subtype as returned by tools/common/parseXMPPUri