Browse Source

Add solarized- prefix to theme settings

Since we're adding a prefix to the Solarized theme's "value" and CSS
class, we need to convert old values so that people using a Solarized
theme don't lose their setting. This changes it in two places:

* The value of the "theme" cookie, using the tween we already have for
  dealing with that cookie.
* The user's default theme setting in the database.
merge-requests/85/head
Deimos 5 years ago
parent
commit
6f26a94d42
  1. 34
      tildes/alembic/versions/a195ddbb4be6_add_solarized_prefix_to_default_themes.py
  2. 22
      tildes/tildes/tweens.py

34
tildes/alembic/versions/a195ddbb4be6_add_solarized_prefix_to_default_themes.py

@ -0,0 +1,34 @@
"""Add solarized- prefix to default themes
Revision ID: a195ddbb4be6
Revises: f20ce28b1d5c
Create Date: 2019-09-10 04:13:50.950487
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = "a195ddbb4be6"
down_revision = "f20ce28b1d5c"
branch_labels = None
depends_on = None
def upgrade():
op.execute(
"UPDATE users SET theme_default = 'solarized-dark' WHERE theme_default = 'dark'"
)
op.execute(
"UPDATE users SET theme_default = 'solarized-light' WHERE theme_default = 'light'"
)
def downgrade():
op.execute(
"UPDATE users SET theme_default = 'dark' WHERE theme_default = 'solarized-dark'"
)
op.execute(
"UPDATE users SET theme_default = 'light' WHERE theme_default = 'solarized-light'"
)

22
tildes/tildes/tweens.py

@ -77,6 +77,9 @@ def theme_cookie_tween_factory(handler: Callable, registry: Registry) -> Callabl
but doesn't already have a theme cookie. This is necessary so that their default
theme will apply to the Blog and Docs sites as well, since those sites are
static and can't look up the user's default theme in the database.
Temporarily, this tween is also being used to convert old theme cookies with
"light" or "dark" values to the new "solarized-light" and "solarized-dark" ones.
"""
response = handler(request)
@ -84,18 +87,25 @@ def theme_cookie_tween_factory(handler: Callable, registry: Registry) -> Callabl
if request.method.upper() != "GET":
return response
# if they already have a theme cookie, we don't need to do anything
if request.cookies.get("theme", ""):
current_theme = request.cookies.get("theme", "")
# if they already have a valid theme cookie, we don't need to do anything
if current_theme and current_theme not in ("light", "dark"):
return response
# if the user doesn't have a default theme, we don't need to do anything
if not request.user or not request.user.theme_default:
if current_theme in ("light", "dark"):
# add the "solarized-" prefix to "light" / "dark" values
new_theme = "solarized-" + current_theme
elif request.user and request.user.theme_default:
# otherwise (no theme cookie), set as the user's default
new_theme = request.user.theme_default
else:
# if the user isn't logged in or doesn't have a default, do nothing
return response
# set a cookie with the user's default theme
response.set_cookie(
"theme",
request.user.theme_default,
new_theme,
max_age=315360000,
secure=True,
domain="." + request.domain,

Loading…
Cancel
Save