changeset 1270:6dfcdbeb0d33

pages: handle themes using `theme` request argument
author Goffi <goffi@goffi.org>
date Tue, 19 May 2020 11:05:50 +0200
parents 470c6cfdf4ce
children b0b9218c5982
files libervia/pages/forums/topics/page_meta.py libervia/server/pages.py libervia/server/session_iface.py
diffstat 3 files changed, 22 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/libervia/pages/forums/topics/page_meta.py	Tue May 19 11:05:47 2020 +0200
+++ b/libervia/pages/forums/topics/page_meta.py	Tue May 19 11:05:50 2020 +0200
@@ -7,6 +7,7 @@
 from sat.core.log import getLogger
 from sat.tools.common import uri as xmpp_uri
 from sat.tools.common import data_format
+from libervia.server import session_iface
 
 log = getLogger(__name__)
 
--- a/libervia/server/pages.py	Tue May 19 11:05:47 2020 +0200
+++ b/libervia/server/pages.py	Tue May 19 11:05:50 2020 +0200
@@ -1345,6 +1345,7 @@
 
         return self.host.renderer.render(
             self.template,
+            theme=session_data.theme,
             site_themes=self.vhost_root.site_themes,
             page_url=self.getURL(),
             media_path=f"/{C.MEDIA_DIR}",
@@ -1666,6 +1667,24 @@
             if session_data.locale is None:
                 self.setBestLocale(request)
 
+            # 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
+
+
         d = defer.Deferred()
         d.addCallback(self._checkAccess, request)
 
--- a/libervia/server/session_iface.py	Tue May 19 11:05:47 2020 +0200
+++ b/libervia/server/session_iface.py	Tue May 19 11:05: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
@@ -51,6 +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.pages_data = {}  # used to keep data accross reloads (key is page instance)
         self.affiliations = OrderedDict()  # cache for node affiliations