diff 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
line wrap: on
line diff
--- a/libervia/server/pages.py	Fri May 29 21:56:42 2020 +0200
+++ b/libervia/server/pages.py	Fri Jun 19 16:47:50 2020 +0200
@@ -260,6 +260,15 @@
     def main_menu(self):
         return self.vhost_root.main_menu
 
+    @property
+    def default_theme(self):
+        return self.vhost_root.default_theme
+
+
+    @property
+    def site_themes(self):
+        return self.vhost_root.site_themes
+
     @staticmethod
     def createPage(host, meta_path, vhost_root, url_elts, replace_on_conflict=False):
         """Create a LiberviaPage instance
@@ -1238,6 +1247,8 @@
 
             rendered = self.host.renderer.render(
                 template,
+                theme=session_data.theme or self.default_theme,
+                site_themes=self.site_themes,
                 error_code=code,
                 **template_data
             )
@@ -1345,8 +1356,8 @@
 
         return self.host.renderer.render(
             self.template,
-            theme=session_data.theme,
-            site_themes=self.vhost_root.site_themes,
+            theme=session_data.theme or self.default_theme,
+            site_themes=self.site_themes,
             page_url=self.getURL(),
             media_path=f"/{C.MEDIA_DIR}",
             cache_path=session_data.cache_dir,
@@ -1595,6 +1606,8 @@
 
         return self.host.renderer.render(
             template,
+            theme=session_data.theme or self.default_theme,
+            site_themes=self.site_themes,
             page_url=self.getURL(),
             media_path="/" + C.MEDIA_DIR,
             cache_path=session_data.cache_dir,
@@ -1634,6 +1647,8 @@
 
         # template_data are the variables passed to template
         if not hasattr(request, "template_data"):
+            # if template_data doesn't exist, it's the beginning of the request workflow
+            # so we fill essential data
             session_data = self.host.getSessionData(request, session_iface.ISATSession)
             csrf_token = session_data.csrf_token
             request.template_data = {
@@ -1670,19 +1685,14 @@
             # theme
             key_theme = C.KEY_THEME.encode()
             if key_theme in request.args:
-                try:
-                    theme = request.args.pop(key_theme)[0].decode()
-                except IndexError:
-                    theme = C.TEMPLATE_THEME_DEFAULT
-                else:
-                    if key_theme != session_data.theme:
-                        if theme not in self.vhost_root.site_themes:
-                            log.warning(_(
-                                "Theme {theme!r} doesn't exist for {vhost}, using "
-                                "default one")
-                                .format(theme=theme, vhost=self.vhost_root))
-                        else:
-                            session_data.theme = theme
+                theme = request.args.pop(key_theme)[0].decode()
+                if key_theme != session_data.theme:
+                    if theme not in self.site_themes:
+                        log.warning(_(
+                            "Theme {theme!r} doesn't exist for {vhost}"
+                            .format(theme=theme, vhost=self.vhost_root)))
+                    else:
+                        session_data.theme = theme
 
 
         d = defer.Deferred()