From 56f709bda2fb92956d751756c1faa1a89bc7c67d Mon Sep 17 00:00:00 2001 From: Deimos Date: Tue, 14 Apr 2020 19:39:20 -0600 Subject: [PATCH] Add a current_theme request method --- tildes/tildes/request_methods.py | 14 ++++++++++++ tildes/tildes/templates/base.jinja2 | 22 +++++++------------ .../tildes/templates/base_no_sidebar.jinja2 | 8 +------ tildes/tildes/templates/settings.jinja2 | 2 +- tildes/tildes/views/settings.py | 3 --- 5 files changed, 24 insertions(+), 25 deletions(-) diff --git a/tildes/tildes/request_methods.py b/tildes/tildes/request_methods.py index 61c6900..2376069 100644 --- a/tildes/tildes/request_methods.py +++ b/tildes/tildes/request_methods.py @@ -179,6 +179,18 @@ def current_listing_normal_url( return request.current_route_url(_query=query_vars) +def current_theme(request: Request) -> str: + """Return the name of the current theme being used by the user.""" + cookie_theme = request.cookies.get("theme", None) + + if request.user: + user_theme = request.user.theme_default + else: + user_theme = None + + return cookie_theme or user_theme or "white" + + def includeme(config: Configurator) -> None: """Attach the request methods to the Pyramid request object.""" config.add_request_method(is_bot, "is_bot", reify=True) @@ -195,6 +207,8 @@ def includeme(config: Configurator) -> None: ) # pylint: enable=unnecessary-lambda + config.add_request_method(current_theme, "current_theme", reify=True) + config.add_request_method(check_rate_limit, "check_rate_limit") config.add_request_method(apply_rate_limit, "apply_rate_limit") diff --git a/tildes/tildes/templates/base.jinja2 b/tildes/tildes/templates/base.jinja2 index cd69ca6..be58838 100644 --- a/tildes/tildes/templates/base.jinja2 +++ b/tildes/tildes/templates/base.jinja2 @@ -20,17 +20,17 @@ {% endblock %} {# Hardcoding each option isn't great, but I don't know a better method #} - {% if request.cookies.get('theme', '') == 'solarized-dark' %} + {% if request.current_theme == "solarized-dark" %} - {% elif request.cookies.get("theme", "") == "dracula" %} + {% elif request.current_theme == "dracula" %} - {% elif request.cookies.get("theme", "") == "atom-one-dark" %} + {% elif request.current_theme == "atom-one-dark" %} - {% elif request.cookies.get('theme', '') == 'black' %} + {% elif request.current_theme == "black" %} - {% elif request.cookies.get('theme', '') == 'zenburn' %} + {% elif request.current_theme == "zenburn" %} - {% elif request.cookies.get('theme', '') == 'gruvbox-dark' %} + {% elif request.current_theme == "gruvbox-dark" %} {% endif %} @@ -57,13 +57,7 @@ {% block body_tag %} - {% if request.cookies.get('theme', '') %} - - {% elif request.user and request.user.theme_default %} - - {% else %} - - {% endif %} + {% endblock %}