comparison libervia/server/pages.py @ 1506:ce879da7fcf7

server: fix `on_signal` callback
author Goffi <goffi@goffi.org>
date Thu, 23 Mar 2023 17:50:54 +0100
parents 409d10211b20
children 106bae41f5c8
comparison
equal deleted inserted replaced
1505:a169cbc315f0 1506:ce879da7fcf7
540 else: 540 else:
541 log.info(_("{page} reloaded").format(page=resource)) 541 log.info(_("{page} reloaded").format(page=resource))
542 542
543 def checkCSRF(self, request): 543 def checkCSRF(self, request):
544 session = self.host.getSessionData( 544 session = self.host.getSessionData(
545 request, session_iface.ISATSession 545 request, session_iface.IWebSession
546 ) 546 )
547 if session.profile is None: 547 if session.profile is None:
548 # CSRF doesn't make sense when no user is logged 548 # CSRF doesn't make sense when no user is logged
549 log.debug("disabling CSRF check because service profile is used") 549 log.debug("disabling CSRF check because service profile is used")
550 return 550 return
1076 'registered')) 1076 'registered'))
1077 return 1077 return
1078 if profile != C.SERVICE_PROFILE: 1078 if profile != C.SERVICE_PROFILE:
1079 #  only service profile is cached for now 1079 #  only service profile is cached for now
1080 return 1080 return
1081 session_data = self.host.getSessionData(request, session_iface.ISATSession) 1081 session_data = self.host.getSessionData(request, session_iface.IWebSession)
1082 locale = session_data.locale 1082 locale = session_data.locale
1083 if locale == C.DEFAULT_LOCALE: 1083 if locale == C.DEFAULT_LOCALE:
1084 # no need to duplicate cache here 1084 # no need to duplicate cache here
1085 locale = None 1085 locale = None
1086 try: 1086 try:
1157 """ 1157 """
1158 entities = {str(e) for e in entities if e} 1158 entities = {str(e) for e in entities if e}
1159 profile = self.getProfile(request) or C.SERVICE_PROFILE 1159 profile = self.getProfile(request) or C.SERVICE_PROFILE
1160 identities = self.host.getSessionData( 1160 identities = self.host.getSessionData(
1161 request, 1161 request,
1162 session_iface.ISATSession 1162 session_iface.IWebSession
1163 ).identities 1163 ).identities
1164 for e in entities: 1164 for e in entities:
1165 if e not in identities: 1165 if e not in identities:
1166 id_raw = await self.host.bridgeCall( 1166 id_raw = await self.host.bridgeCall(
1167 'identityGet', e, [], True, profile) 1167 'identityGet', e, [], True, profile)
1274 if no_body: 1274 if no_body:
1275 request.finish() 1275 request.finish()
1276 else: 1276 else:
1277 template = "error/" + str(code) + ".html" 1277 template = "error/" + str(code) + ".html"
1278 template_data = request.template_data 1278 template_data = request.template_data
1279 session_data = self.host.getSessionData(request, session_iface.ISATSession) 1279 session_data = self.host.getSessionData(request, session_iface.IWebSession)
1280 if session_data.locale is not None: 1280 if session_data.locale is not None:
1281 template_data['locale'] = session_data.locale 1281 template_data['locale'] = session_data.locale
1282 if self.vhost_root.site_name: 1282 if self.vhost_root.site_name:
1283 template_data['site'] = self.vhost_root.site_name 1283 template_data['site'] = self.vhost_root.site_name
1284 1284
1336 else: 1336 else:
1337 child.render(request) 1337 child.render(request)
1338 raise failure.Failure(exceptions.CancelError("subpage page is used")) 1338 raise failure.Failure(exceptions.CancelError("subpage page is used"))
1339 1339
1340 def _prepare_dynamic(self, request): 1340 def _prepare_dynamic(self, request):
1341 session_data = self.host.getSessionData(request, session_iface.ISATSession) 1341 session_data = self.host.getSessionData(request, session_iface.IWebSession)
1342 # we need to activate dynamic page 1342 # we need to activate dynamic page
1343 # we set data for template, and create/register token 1343 # we set data for template, and create/register token
1344 # socket_token = str(uuid.uuid4()) 1344 # socket_token = str(uuid.uuid4())
1345 socket_url = self.host.get_websocket_url(request) 1345 socket_url = self.host.get_websocket_url(request)
1346 # as for CSRF, it is important to not let the socket token if we use the service 1346 # as for CSRF, it is important to not let the socket token if we use the service
1357 1357
1358 def _render_template(self, request): 1358 def _render_template(self, request):
1359 template_data = request.template_data 1359 template_data = request.template_data
1360 1360
1361 # if confirm variable is set in case of successfuly data post 1361 # if confirm variable is set in case of successfuly data post
1362 session_data = self.host.getSessionData(request, session_iface.ISATSession) 1362 session_data = self.host.getSessionData(request, session_iface.IWebSession)
1363 template_data['identities'] = session_data.identities 1363 template_data['identities'] = session_data.identities
1364 if session_data.popPageFlag(self, C.FLAG_CONFIRM): 1364 if session_data.popPageFlag(self, C.FLAG_CONFIRM):
1365 template_data["confirm"] = True 1365 template_data["confirm"] = True
1366 notifs = session_data.popPageNotifications(self) 1366 notifs = session_data.popPageNotifications(self)
1367 if notifs: 1367 if notifs:
1439 else: 1439 else:
1440 ret = tuple(ret) 1440 ret = tuple(ret)
1441 raise NotImplementedError( 1441 raise NotImplementedError(
1442 _("iterable in on_data_post return value is not used yet") 1442 _("iterable in on_data_post return value is not used yet")
1443 ) 1443 )
1444 session_data = self.host.getSessionData(request, session_iface.ISATSession) 1444 session_data = self.host.getSessionData(request, session_iface.IWebSession)
1445 request_data = self.getRData(request) 1445 request_data = self.getRData(request)
1446 if "post_redirect_page" in request_data: 1446 if "post_redirect_page" in request_data:
1447 redirect_page_data = request_data["post_redirect_page"] 1447 redirect_page_data = request_data["post_redirect_page"]
1448 if isinstance(redirect_page_data, tuple): 1448 if isinstance(redirect_page_data, tuple):
1449 redirect_page = redirect_page_data[0] 1449 redirect_page = redirect_page_data[0]
1468 try: 1468 try:
1469 ret = await asDeferred(self.on_data_post, self, request) 1469 ret = await asDeferred(self.on_data_post, self, request)
1470 except exceptions.DataError as e: 1470 except exceptions.DataError as e:
1471 # something is wrong with the posted data, we re-display the page with a 1471 # something is wrong with the posted data, we re-display the page with a
1472 # warning notification 1472 # warning notification
1473 session_data = self.host.getSessionData(request, session_iface.ISATSession) 1473 session_data = self.host.getSessionData(request, session_iface.IWebSession)
1474 session_data.setPageNotification(self, str(e), C.LVL_WARNING) 1474 session_data.setPageNotification(self, str(e), C.LVL_WARNING)
1475 request.setResponseCode(C.HTTP_SEE_OTHER) 1475 request.setResponseCode(C.HTTP_SEE_OTHER)
1476 request.setHeader("location", request.uri) 1476 request.setHeader("location", request.uri)
1477 request.finish() 1477 request.finish()
1478 raise failure.Failure(exceptions.CancelError("Post/Redirect/Get is used")) 1478 raise failure.Failure(exceptions.CancelError("Post/Redirect/Get is used"))
1559 """Helper method to easily get current profile 1559 """Helper method to easily get current profile
1560 1560
1561 @return (unicode, None): current profile 1561 @return (unicode, None): current profile
1562 None if no profile session is started 1562 None if no profile session is started
1563 """ 1563 """
1564 sat_session = self.host.getSessionData(request, session_iface.ISATSession) 1564 web_session = self.host.getSessionData(request, session_iface.IWebSession)
1565 return sat_session.profile 1565 return web_session.profile
1566 1566
1567 def getJid(self, request): 1567 def getJid(self, request):
1568 """Helper method to easily get current jid 1568 """Helper method to easily get current jid
1569 1569
1570 @return: current jid 1570 @return: current jid
1571 """ 1571 """
1572 sat_session = self.host.getSessionData(request, session_iface.ISATSession) 1572 web_session = self.host.getSessionData(request, session_iface.IWebSession)
1573 return sat_session.jid 1573 return web_session.jid
1574 1574
1575 1575
1576 def getRData(self, request): 1576 def getRData(self, request):
1577 """Helper method to get request data dict 1577 """Helper method to get request data dict
1578 1578
1587 request.data = {} 1587 request.data = {}
1588 return request.data 1588 return request.data
1589 1589
1590 def getPageData(self, request, key): 1590 def getPageData(self, request, key):
1591 """Helper method to retrieve reload resistant data""" 1591 """Helper method to retrieve reload resistant data"""
1592 sat_session = self.host.getSessionData(request, session_iface.ISATSession) 1592 web_session = self.host.getSessionData(request, session_iface.IWebSession)
1593 return sat_session.getPageData(self, key) 1593 return web_session.getPageData(self, key)
1594 1594
1595 def setPageData(self, request, key, value): 1595 def setPageData(self, request, key, value):
1596 """Helper method to set reload resistant data""" 1596 """Helper method to set reload resistant data"""
1597 sat_session = self.host.getSessionData(request, session_iface.ISATSession) 1597 web_session = self.host.getSessionData(request, session_iface.IWebSession)
1598 return sat_session.setPageData(self, key, value) 1598 return web_session.setPageData(self, key, value)
1599 1599
1600 def handleSearch(self, request, extra): 1600 def handleSearch(self, request, extra):
1601 """Manage Full-Text Search 1601 """Manage Full-Text Search
1602 1602
1603 Check if "search" query argument is present, and add MAM filter for it if 1603 Check if "search" query argument is present, and add MAM filter for it if
1647 if not lang: 1647 if not lang:
1648 continue 1648 continue
1649 for a in available: 1649 for a in available:
1650 if a.lower().startswith(lang): 1650 if a.lower().startswith(lang):
1651 session_data = self.host.getSessionData(request, 1651 session_data = self.host.getSessionData(request,
1652 session_iface.ISATSession) 1652 session_iface.IWebSession)
1653 session_data.locale = a 1653 session_data.locale = a
1654 return 1654 return
1655 1655
1656 async def renderPage(self, request, skip_parse_url=False): 1656 async def renderPage(self, request, skip_parse_url=False):
1657 """Main method to handle the workflow of a LiberviaPage""" 1657 """Main method to handle the workflow of a LiberviaPage"""
1658 # template_data are the variables passed to template 1658 # template_data are the variables passed to template
1659 if not hasattr(request, "template_data"): 1659 if not hasattr(request, "template_data"):
1660 # if template_data doesn't exist, it's the beginning of the request workflow 1660 # if template_data doesn't exist, it's the beginning of the request workflow
1661 # so we fill essential data 1661 # so we fill essential data
1662 session_data = self.host.getSessionData(request, session_iface.ISATSession) 1662 session_data = self.host.getSessionData(request, session_iface.IWebSession)
1663 profile = session_data.profile 1663 profile = session_data.profile
1664 request.template_data = { 1664 request.template_data = {
1665 "profile": profile, 1665 "profile": profile,
1666 # it's important to not add CSRF token and session uuid if service profile 1666 # it's important to not add CSRF token and session uuid if service profile
1667 # is used because the page may be cached, and the token then leaked 1667 # is used because the page may be cached, and the token then leaked