changeset 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 eb4f03da0d7d
children cad8f24e23d4
files libervia/server/constants.py libervia/server/pages.py libervia/server/server.py libervia/server/session_iface.py twisted/plugins/libervia_server.py
diffstat 5 files changed, 46 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/libervia/server/constants.py	Fri May 29 21:56:42 2020 +0200
+++ b/libervia/server/constants.py	Fri Jun 19 16:47:50 2020 +0200
@@ -1,7 +1,6 @@
 #!/usr/bin/env python3
 
-
-# Libervia: a SAT frontend
+# Libervia: a SàT frontend
 # Copyright (C) 2009-2020 Jérôme Poisson (goffi@goffi.org)
 
 # This program is free software: you can redistribute it and/or modify
@@ -24,6 +23,7 @@
 
     APP_NAME = "Libervia"
     APP_NAME_FILE = "libervia"
+    CONFIG_SECTION = APP_NAME.lower()
     SERVICE_PROFILE = "libervia"  # the SàT profile that is used for exporting the service
 
     SESSION_TIMEOUT = 7200  # Session's timeout, after that the user will be disconnected
--- 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()
--- a/libervia/server/server.py	Fri May 29 21:56:42 2020 +0200
+++ b/libervia/server/server.py	Fri Jun 19 16:47:50 2020 +0200
@@ -181,6 +181,7 @@
         self.host_name = host_name
         self.site_name = site_name
         self.site_path = Path(site_path)
+        self.default_theme = self.getConfig('theme', C.TEMPLATE_THEME_DEFAULT)
         self.site_themes = set()
         self.named_pages = {}
         self.browser_modules = {}
@@ -227,10 +228,19 @@
         )
 
     def __str__(self):
-        return ("Root resource for {host_name} using {site_name} at {site_path} and "
-                "deserving files at {path}".format(
-                host_name=self.host_name, site_name=self.site_name,
-                site_path=self.site_path, path=self.path))
+        return (
+            f"Root resource for {self.host_name or 'default host'} using "
+            f"{self.site_name or 'default site'} at {self.site_path} and deserving "
+            f"files at {self.path}"
+        )
+
+    def getConfig(self, key, default=None, value_type=None):
+        """Retrieve configuration for this site
+
+        params are the same as for [Libervia.getConfig]
+        """
+        return self.host.getConfig(self, key, default, value_type)
+
 
     def _initRedirections(self, options):
         url_redirections = options["url_redirections_dict"]
@@ -740,7 +750,7 @@
                 - "path": a path is expected, will be normalized and expanded
 
         """
-        section = site_root_res.site_name.lower().strip()
+        section = site_root_res.site_name.lower().strip() or C.CONFIG_SECTION
         value = config.getConfig(self.main_conf, section, key, default=default)
         if value_type is not None:
             if value_type == 'path':
--- a/libervia/server/session_iface.py	Fri May 29 21:56:42 2020 +0200
+++ b/libervia/server/session_iface.py	Fri Jun 19 16:47:50 2020 +0200
@@ -50,7 +50,7 @@
         self.identities = data_objects.Identities()
         self.csrf_token = str(shortuuid.uuid())
         self.locale = None  # i18n of the pages
-        self.theme = C.TEMPLATE_THEME_DEFAULT
+        self.theme = None
         self.pages_data = {}  # used to keep data accross reloads (key is page instance)
         self.affiliations = OrderedDict()  # cache for node affiliations
 
--- a/twisted/plugins/libervia_server.py	Fri May 29 21:56:42 2020 +0200
+++ b/twisted/plugins/libervia_server.py	Fri Jun 19 16:47:50 2020 +0200
@@ -49,7 +49,6 @@
 import configparser
 
 
-CONFIG_SECTION = C.APP_NAME.lower()
 if libervia.__version__ != sat.__version__:
     import sys
 
@@ -185,7 +184,7 @@
         for param in self.optParameters + OPT_PARAMETERS_CFG:
             name = param[0]
             try:
-                value = config.getConfig(config_parser, CONFIG_SECTION, name, Exception)
+                value = config.getConfig(config_parser, C.CONFIG_SECTION, name, Exception)
                 # if isinstance(value, str):
                 #     value = value.encode("utf-8")
                 try:
@@ -206,13 +205,13 @@
         replacements = (("ssl_certificate", "tls_certificate"),)
         for old, new in replacements:
             try:
-                value = config.getConfig(config_parser, CONFIG_SECTION, old, Exception)
+                value = config.getConfig(config_parser, C.CONFIG_SECTION, old, Exception)
             except (configparser.NoSectionError, configparser.NoOptionError):
                 pass
             else:
                 print(("\n/!\\ Use of {old} is deprecated, please use {new} instead\n"
                       .format(old=old, new=new)))
-                config_parser.set(CONFIG_SECTION, new, value)
+                config_parser.set(C.CONFIG_SECTION, new, value)
 
 
 @implementer(IServiceMaker, IPlugin)