comparison libervia/server/pages.py @ 1275:334d044f2713

server: default theme can now be specified in site section of `sat.conf` with `theme` key
author Goffi <goffi@goffi.org>
date Fri, 19 Jun 2020 16:47:50 +0200
parents b0b9218c5982
children cad8f24e23d4
comparison
equal deleted inserted replaced
1274:eb4f03da0d7d 1275:334d044f2713
258 258
259 @property 259 @property
260 def main_menu(self): 260 def main_menu(self):
261 return self.vhost_root.main_menu 261 return self.vhost_root.main_menu
262 262
263 @property
264 def default_theme(self):
265 return self.vhost_root.default_theme
266
267
268 @property
269 def site_themes(self):
270 return self.vhost_root.site_themes
271
263 @staticmethod 272 @staticmethod
264 def createPage(host, meta_path, vhost_root, url_elts, replace_on_conflict=False): 273 def createPage(host, meta_path, vhost_root, url_elts, replace_on_conflict=False):
265 """Create a LiberviaPage instance 274 """Create a LiberviaPage instance
266 275
267 @param meta_path(Path): path to the page_meta.py file 276 @param meta_path(Path): path to the page_meta.py file
1236 if self.vhost_root.site_name: 1245 if self.vhost_root.site_name:
1237 template_data['site'] = self.vhost_root.site_name 1246 template_data['site'] = self.vhost_root.site_name
1238 1247
1239 rendered = self.host.renderer.render( 1248 rendered = self.host.renderer.render(
1240 template, 1249 template,
1250 theme=session_data.theme or self.default_theme,
1251 site_themes=self.site_themes,
1241 error_code=code, 1252 error_code=code,
1242 **template_data 1253 **template_data
1243 ) 1254 )
1244 1255
1245 self.writeData(rendered, request) 1256 self.writeData(rendered, request)
1343 if key not in template_data: 1354 if key not in template_data:
1344 template_data[key] = value 1355 template_data[key] = value
1345 1356
1346 return self.host.renderer.render( 1357 return self.host.renderer.render(
1347 self.template, 1358 self.template,
1348 theme=session_data.theme, 1359 theme=session_data.theme or self.default_theme,
1349 site_themes=self.vhost_root.site_themes, 1360 site_themes=self.site_themes,
1350 page_url=self.getURL(), 1361 page_url=self.getURL(),
1351 media_path=f"/{C.MEDIA_DIR}", 1362 media_path=f"/{C.MEDIA_DIR}",
1352 cache_path=session_data.cache_dir, 1363 cache_path=session_data.cache_dir,
1353 build_path=f"/{C.BUILD_DIR}/", 1364 build_path=f"/{C.BUILD_DIR}/",
1354 main_menu=self.main_menu, 1365 main_menu=self.main_menu,
1593 if self.vhost_root.site_name: 1604 if self.vhost_root.site_name:
1594 template_data['site'] = self.vhost_root.site_name 1605 template_data['site'] = self.vhost_root.site_name
1595 1606
1596 return self.host.renderer.render( 1607 return self.host.renderer.render(
1597 template, 1608 template,
1609 theme=session_data.theme or self.default_theme,
1610 site_themes=self.site_themes,
1598 page_url=self.getURL(), 1611 page_url=self.getURL(),
1599 media_path="/" + C.MEDIA_DIR, 1612 media_path="/" + C.MEDIA_DIR,
1600 cache_path=session_data.cache_dir, 1613 cache_path=session_data.cache_dir,
1601 build_path="/" + C.BUILD_DIR + "/", 1614 build_path="/" + C.BUILD_DIR + "/",
1602 main_menu=self.main_menu, 1615 main_menu=self.main_menu,
1632 def renderPage(self, request, skip_parse_url=False): 1645 def renderPage(self, request, skip_parse_url=False):
1633 """Main method to handle the workflow of a LiberviaPage""" 1646 """Main method to handle the workflow of a LiberviaPage"""
1634 1647
1635 # template_data are the variables passed to template 1648 # template_data are the variables passed to template
1636 if not hasattr(request, "template_data"): 1649 if not hasattr(request, "template_data"):
1650 # if template_data doesn't exist, it's the beginning of the request workflow
1651 # so we fill essential data
1637 session_data = self.host.getSessionData(request, session_iface.ISATSession) 1652 session_data = self.host.getSessionData(request, session_iface.ISATSession)
1638 csrf_token = session_data.csrf_token 1653 csrf_token = session_data.csrf_token
1639 request.template_data = { 1654 request.template_data = {
1640 "profile": session_data.profile, 1655 "profile": session_data.profile,
1641 "csrf_token": csrf_token, 1656 "csrf_token": csrf_token,
1668 self.setBestLocale(request) 1683 self.setBestLocale(request)
1669 1684
1670 # theme 1685 # theme
1671 key_theme = C.KEY_THEME.encode() 1686 key_theme = C.KEY_THEME.encode()
1672 if key_theme in request.args: 1687 if key_theme in request.args:
1673 try: 1688 theme = request.args.pop(key_theme)[0].decode()
1674 theme = request.args.pop(key_theme)[0].decode() 1689 if key_theme != session_data.theme:
1675 except IndexError: 1690 if theme not in self.site_themes:
1676 theme = C.TEMPLATE_THEME_DEFAULT 1691 log.warning(_(
1677 else: 1692 "Theme {theme!r} doesn't exist for {vhost}"
1678 if key_theme != session_data.theme: 1693 .format(theme=theme, vhost=self.vhost_root)))
1679 if theme not in self.vhost_root.site_themes: 1694 else:
1680 log.warning(_( 1695 session_data.theme = theme
1681 "Theme {theme!r} doesn't exist for {vhost}, using "
1682 "default one")
1683 .format(theme=theme, vhost=self.vhost_root))
1684 else:
1685 session_data.theme = theme
1686 1696
1687 1697
1688 d = defer.Deferred() 1698 d = defer.Deferred()
1689 d.addCallback(self._checkAccess, request) 1699 d.addCallback(self._checkAccess, request)
1690 1700