From 7133d6b44184f5023642c7bb672089f6a17e0fe4 Mon Sep 17 00:00:00 2001 From: extome <74880719+extome9000@users.noreply.github.com> Date: Wed, 8 Nov 2023 21:33:03 -0500 Subject: [PATCH] Better CSS support (#583) * Remove banner hardcoding * Refactor "colors" to "stylesheet" * Remove logo hardcoding * Remove stylesheet hardcoding * Add very basic static CSS scanning and a new style * Respect environment settings * Check if selected stylesheet still exists * New theme and title formatting * Revert migration change * Code linting * More outlines for Matrix style * Change wording in settings * Forgot this wording * Add suggested changes --- .../config/management/commands/ta_startup.py | 10 +-- tubearchivist/home/src/frontend/forms.py | 19 +++-- tubearchivist/home/src/ta/helper.py | 19 +++++ tubearchivist/home/src/ta/users.py | 9 ++- tubearchivist/home/templates/home/base.html | 13 +--- tubearchivist/home/templates/home/login.html | 13 +--- .../home/templates/home/settings_user.html | 8 +- tubearchivist/home/views.py | 14 ++-- tubearchivist/static/css/dark.css | 2 + tubearchivist/static/css/light.css | 2 + tubearchivist/static/css/matrix.css | 75 +++++++++++++++++++ tubearchivist/static/css/midnight.css | 14 ++++ tubearchivist/static/css/style.css | 4 +- 13 files changed, 154 insertions(+), 48 deletions(-) create mode 100644 tubearchivist/static/css/matrix.css create mode 100644 tubearchivist/static/css/midnight.css diff --git a/tubearchivist/config/management/commands/ta_startup.py b/tubearchivist/config/management/commands/ta_startup.py index b10fc99..75a5d02 100644 --- a/tubearchivist/config/management/commands/ta_startup.py +++ b/tubearchivist/config/management/commands/ta_startup.py @@ -162,11 +162,11 @@ class Command(BaseCommand): for user in users: new_conf = UserConfig(user) - colors_key = f"{user}:colors" - colors = redis.get_message(colors_key).get("status") - if colors: - new_conf.set_value("colors", colors) - redis.del_message(colors_key) + stylesheet_key = f"{user}:color" + stylesheet = redis.get_message(stylesheet_key).get("status") + if stylesheet: + new_conf.set_value("stylesheet", stylesheet) + redis.del_message(stylesheet_key) sort_by_key = f"{user}:sort_by" sort_by = redis.get_message(sort_by_key).get("status") diff --git a/tubearchivist/home/src/frontend/forms.py b/tubearchivist/home/src/frontend/forms.py index fe5f5b2..5044456 100644 --- a/tubearchivist/home/src/frontend/forms.py +++ b/tubearchivist/home/src/frontend/forms.py @@ -2,9 +2,12 @@ - hold all form classes used in the views """ +import os + from django import forms from django.contrib.auth.forms import AuthenticationForm from django.forms.widgets import PasswordInput, TextInput +from home.src.ta.helper import get_stylesheets class CustomAuthForm(AuthenticationForm): @@ -29,14 +32,16 @@ class CustomAuthForm(AuthenticationForm): class UserSettingsForm(forms.Form): """user configurations values""" - CHOICES = [ - ("", "-- change color scheme --"), - ("dark", "Dark"), - ("light", "Light"), - ] + STYLESHEET_CHOICES = [("", "-- change stylesheet --")] + STYLESHEET_CHOICES.extend( + [ + (stylesheet, os.path.splitext(stylesheet)[0].title()) + for stylesheet in get_stylesheets() + ] + ) - colors = forms.ChoiceField( - widget=forms.Select, choices=CHOICES, required=False + stylesheet = forms.ChoiceField( + widget=forms.Select, choices=STYLESHEET_CHOICES, required=False ) page_size = forms.IntegerField(required=False) diff --git a/tubearchivist/home/src/ta/helper.py b/tubearchivist/home/src/ta/helper.py index 8bce809..e3089d8 100644 --- a/tubearchivist/home/src/ta/helper.py +++ b/tubearchivist/home/src/ta/helper.py @@ -12,6 +12,7 @@ from datetime import datetime from urllib.parse import urlparse import requests +from home.src.ta.settings import EnvironmentSettings def ignore_filelist(filelist: list[str]) -> list[str]: @@ -203,3 +204,21 @@ def ta_host_parser(ta_host: str) -> tuple[list[str], list[str]]: csrf_trusted_origins.append(f"{parsed.scheme}://{parsed.hostname}") return allowed_hosts, csrf_trusted_origins + + +def get_stylesheets(): + """Get all valid stylesheets from /static/css""" + app_root = EnvironmentSettings.APP_DIR + stylesheets = os.listdir(os.path.join(app_root, "static/css")) + stylesheets.remove("style.css") + stylesheets.sort() + stylesheets = list(filter(lambda x: x.endswith(".css"), stylesheets)) + return stylesheets + + +def check_stylesheet(stylesheet: str): + """Check if a stylesheet exists. Return dark.css as a fallback""" + if stylesheet in get_stylesheets(): + return stylesheet + + return "dark.css" diff --git a/tubearchivist/home/src/ta/users.py b/tubearchivist/home/src/ta/users.py index f1017fe..bb6a387 100644 --- a/tubearchivist/home/src/ta/users.py +++ b/tubearchivist/home/src/ta/users.py @@ -7,12 +7,13 @@ Functionality: from typing import TypedDict from home.src.es.connect import ElasticWrap +from home.src.ta.helper import get_stylesheets class UserConfigType(TypedDict, total=False): """describes the user configuration""" - colors: str + stylesheet: str page_size: int sort_by: str sort_order: str @@ -31,7 +32,7 @@ class UserConfig: """Handle settings for an individual user""" _DEFAULT_USER_SETTINGS = UserConfigType( - colors="dark", + stylesheet="dark.css", page_size=12, sort_by="published", sort_order="desc", @@ -46,7 +47,7 @@ class UserConfig: sponsorblock_id=None, ) - VALID_COLORS = ["dark", "light"] + VALID_STYLESHEETS = get_stylesheets() VALID_VIEW_STYLE = ["grid", "list"] VALID_SORT_ORDER = ["asc", "desc"] VALID_SORT_BY = ["published", "downloaded", "views", "likes"] @@ -91,7 +92,7 @@ class UserConfig: ) valid_values = { - "colors": self.VALID_COLORS, + "stylesheet": self.VALID_STYLESHEETS, "sort_by": self.VALID_SORT_BY, "sort_order": self.VALID_SORT_ORDER, "view_style_home": self.VALID_VIEW_STYLE, diff --git a/tubearchivist/home/templates/home/base.html b/tubearchivist/home/templates/home/base.html index b7b998e..40211f2 100644 --- a/tubearchivist/home/templates/home/base.html +++ b/tubearchivist/home/templates/home/base.html @@ -23,11 +23,7 @@ {% else %} TubeArchivist {% endif %} - {% if colors == "dark" %} - - {% else %} - - {% endif %} + {% if cast %} @@ -39,12 +35,7 @@
- {% if colors == 'dark' %} - tube-archivist-banner - {% endif %} - {% if colors == 'light' %} - tube-archivist-banner - {% endif %} + tube-archivist-banner
diff --git a/tubearchivist/home/templates/home/login.html b/tubearchivist/home/templates/home/login.html index ecefb42..fe1d24f 100644 --- a/tubearchivist/home/templates/home/login.html +++ b/tubearchivist/home/templates/home/login.html @@ -18,20 +18,11 @@ - {% if colors == "dark" %} - - {% else %} - - {% endif %} +