Files
shoppy/web/management/commands/build_tailwind_theme.py
T
2025-02-19 18:01:52 +01:00

57 lines
1.3 KiB
Python

import os
import subprocess
from dataclasses import dataclass
from django.conf import settings
from django.core.management.base import BaseCommand
from django.template.loader import render_to_string
from django.core.management import call_command
from web.models import WebSettings
@dataclass
class Color:
name: str
index: int
value: str
class Command(BaseCommand):
"""
Usage:
python manage.py build_tailwind_theme
"""
def handle(self, *args, **options):
web_settings = WebSettings.load()
path = os.path.join(settings.BASE_DIR, "web", "static", "css", "main.css")
colors = []
for color_name, data in web_settings.theme_colors.items():
for index, value in data.items():
color = Color(color_name, index, value)
colors.append(color)
result = str(
render_to_string(
"theme/main.css.template",
context={"colors": colors},
)
)
with open(path, "w") as f:
f.write(result)
subprocess.run(
[
"tailwindcss",
"-i",
"web/static/css/main.css",
"-o",
"web/static/css/styles.css",
"--minify",
]
)
call_command("collectstatic", "--no-input")