Browse Source

Add a current_theme request method

merge-requests/110/head
Deimos 5 years ago
parent
commit
56f709bda2
  1. 14
      tildes/tildes/request_methods.py
  2. 22
      tildes/tildes/templates/base.jinja2
  3. 8
      tildes/tildes/templates/base_no_sidebar.jinja2
  4. 2
      tildes/tildes/templates/settings.jinja2
  5. 3
      tildes/tildes/views/settings.py

14
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")

22
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" %}
<meta name="theme-color" content="#073642">
{% elif request.cookies.get("theme", "") == "dracula" %}
{% elif request.current_theme == "dracula" %}
<meta name="theme-color" content="#282a36">
{% elif request.cookies.get("theme", "") == "atom-one-dark" %}
{% elif request.current_theme == "atom-one-dark" %}
<meta name="theme-color" content="#282c34">
{% elif request.cookies.get('theme', '') == 'black' %}
{% elif request.current_theme == "black" %}
<meta name="theme-color" content="#222">
{% elif request.cookies.get('theme', '') == 'zenburn' %}
{% elif request.current_theme == "zenburn" %}
<meta name="theme-color" content="#3f3f3f">
{% elif request.cookies.get('theme', '') == 'gruvbox-dark' %}
{% elif request.current_theme == "gruvbox-dark" %}
<meta name="theme-color" content="#282828">
{% endif %}
@ -57,13 +57,7 @@
</head>
{% block body_tag %}
{% if request.cookies.get('theme', '') %}
<body class="theme-{{ request.cookies.get('theme', '') }}">
{% elif request.user and request.user.theme_default %}
<body class="theme-{{ request.user.theme_default }}">
{% else %}
<body>
{% endif %}
<body class="theme-{{ request.current_theme }}">
{% endblock %}
<header id="site-header" data-js-hide-sidebar-if-open>
@ -123,7 +117,7 @@
("gruvbox-light", "Gruvbox Light"),
("gruvbox-dark", "Gruvbox Dark")) %}
<option value="{{ theme }}"
{{ 'selected' if theme == request.cookies.get("theme", "white") else '' }}
{{ 'selected' if theme == request.current_theme else '' }}
>
{{ description }}
</option>

8
tildes/tildes/templates/base_no_sidebar.jinja2

@ -4,11 +4,5 @@
{% extends 'base.jinja2' %}
{% block body_tag %}
{% if request.cookies.get('theme', '') %}
<body class="l-no-sidebar theme-{{ request.cookies.get('theme', '') }}">
{% elif request.user and request.user.theme_default %}
<body class="l-no-sidebar theme-{{ request.user.theme_default }}">
{% else %}
<body class="l-no-sidebar">
{% endif %}
<body class="l-no-sidebar theme-{{ request.current_theme }}">
{% endblock %}

2
tildes/tildes/templates/settings.jinja2

@ -27,7 +27,7 @@
{% for theme, description in theme_options.items() %}
<option
value="{{ theme }}"
{{ 'selected' if theme == current_theme else '' }}
{{ 'selected' if theme == request.current_theme else '' }}
>
{{ description }}
</option>

3
tildes/tildes/views/settings.py

@ -51,8 +51,6 @@ def get_settings(request: Request) -> dict:
site_default_theme = "white"
user_default_theme = request.user.theme_default or site_default_theme
current_theme = request.cookies.get("theme", "") or user_default_theme
# Make a copy of the theme options dict so we can add info to the names
theme_options = THEME_OPTIONS.copy()
@ -70,7 +68,6 @@ def get_settings(request: Request) -> dict:
return {
"current_comment_sort_order": current_comment_sort_order,
"comment_sort_order_options": CommentTreeSortOption,
"current_theme": current_theme,
"theme_options": theme_options,
}

Loading…
Cancel
Save