simplify scss management

This commit is contained in:
thomas girod 2024-08-13 23:02:51 +02:00
parent 68d0a16d1c
commit 52c19e9962
5 changed files with 20 additions and 114 deletions

View File

@ -1,55 +0,0 @@
#!/usr/bin/env python3
#
# Copyright 2017
# - Sli <antoine@bartuccio.fr>
#
# Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM,
# http://ae.utbm.fr.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License a published by the Free Software
# Foundation; either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Sofware Foundation, Inc., 59 Temple
# Place - Suite 330, Boston, MA 02111-1307, USA.
#
#
import functools
from pathlib import Path
import sass
from django.conf import settings
from django.core.files.base import ContentFile
from django.templatetags.static import static
from django_jinja.builtins.filters import static
from core.scss.storage import ScssFileStorage, find_file
@functools.cache
def _scss_storage():
return ScssFileStorage()
def process_scss_path(path: Path):
css_path = path.with_suffix(".css")
if settings.DEBUG:
compile_args = {
"filename": find_file(path),
"include_paths": settings.SASS_INCLUDE_FOLDERS,
}
if settings.SASS_PRECISION:
compile_args["precision"] = settings.SASS_PRECISION
content = sass.compile(**compile_args)
storage = _scss_storage()
if storage.exists(css_path):
storage.delete(css_path)
storage.save(css_path, ContentFile(content))
return static(css_path)

View File

@ -1,43 +0,0 @@
#!/usr/bin/env python3
#
# Copyright 2017
# - Sli <antoine@bartuccio.fr>
#
# Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM,
# http://ae.utbm.fr.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License a published by the Free Software
# Foundation; either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Sofware Foundation, Inc., 59 Temple
# Place - Suite 330, Boston, MA 02111-1307, USA.
#
#
from django.conf import settings
from django.contrib.staticfiles.finders import get_finders
from django.core.files.storage import FileSystemStorage
class ScssFileStorage(FileSystemStorage):
def __init__(self, location=None, base_url=None, *args, **kwargs):
if location is None:
location = settings.STATIC_ROOT
if base_url is None:
base_url = settings.STATIC_URL
super().__init__(location, base_url, *args, **kwargs)
def find_file(path):
for finder in get_finders():
result = finder.find(path)
if result:
return result

View File

@ -26,14 +26,19 @@ import datetime
from pathlib import Path
import phonenumbers
import sass
from django import template
from django.conf import settings
from django.contrib.staticfiles.finders import find
from django.core.files.base import ContentFile
from django.core.files.storage import storages
from django.template import TemplateSyntaxError
from django.template.defaultfilters import stringfilter
from django.templatetags.static import static
from django.utils.safestring import mark_safe
from django.utils.translation import ngettext
from core.markdown import markdown as md
from core.scss.processor import process_scss_path
register = template.Library()
@ -91,4 +96,15 @@ def scss(path):
path = Path(path)
if path.suffix != ".scss":
raise TemplateSyntaxError("`scss` tag has been called with a non-scss file")
return process_scss_path(path)
css_path = path.with_suffix(".css")
if settings.DEBUG:
compile_args = {"filename": find(path)}
if settings.SASS_PRECISION:
compile_args["precision"] = settings.SASS_PRECISION
content = sass.compile(**compile_args)
storage = storages["staticfiles"]
if storage.exists(css_path):
storage.delete(css_path)
storage.save(css_path, ContentFile(content))
return static(css_path)

View File

@ -22,8 +22,6 @@
#
#
from collections import OrderedDict
from django.conf import settings
from django.contrib.staticfiles.finders import FileSystemFinder
from django.core.files.storage import FileSystemStorage
@ -32,19 +30,10 @@ from django.core.files.storage import FileSystemStorage
class ScssFinder(FileSystemFinder):
"""Find static *.css files compiled on the fly."""
locations = []
def __init__(self, apps=None, *args, **kwargs):
location = settings.STATIC_ROOT
if not location.is_dir():
return
self.locations = [("", location)]
self.storages = OrderedDict()
self.storages = {}
filesystem_storage = FileSystemStorage(location=location)
filesystem_storage.prefix = self.locations[0][0]
self.storages[location] = filesystem_storage
def find(self, path, all=False): # noqa A002 (shadows the builtin `all` function)
if path.endswith(".css"):
return super().find(path, all)
return []

View File

@ -270,7 +270,7 @@ STATIC_ROOT = BASE_DIR / "static"
STATICFILES_FINDERS = [
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
"core.scss.finder.ScssFinder",
"sith.finders.ScssFinder",
]
# Auth configuration
@ -704,7 +704,6 @@ if DEBUG:
"debug_toolbar.panels.signals.SignalsPanel",
"debug_toolbar.panels.redirects.RedirectsPanel",
]
SASS_INCLUDE_FOLDERS = ["core/static/"]
SENTRY_ENV = "development"
if TESTING: