diff --git a/core/scss/processor.py b/core/scss/processor.py deleted file mode 100644 index eb682747..00000000 --- a/core/scss/processor.py +++ /dev/null @@ -1,55 +0,0 @@ -#!/usr/bin/env python3 -# -# Copyright 2017 -# - Sli -# -# 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) diff --git a/core/scss/storage.py b/core/scss/storage.py deleted file mode 100644 index 87ccd7a3..00000000 --- a/core/scss/storage.py +++ /dev/null @@ -1,43 +0,0 @@ -#!/usr/bin/env python3 -# -# Copyright 2017 -# - Sli -# -# 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 diff --git a/core/templatetags/renderer.py b/core/templatetags/renderer.py index cfd3ef91..beef3f43 100644 --- a/core/templatetags/renderer.py +++ b/core/templatetags/renderer.py @@ -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) diff --git a/core/scss/finder.py b/sith/finders.py similarity index 81% rename from core/scss/finder.py rename to sith/finders.py index a4b7d030..e349dfda 100644 --- a/core/scss/finder.py +++ b/sith/finders.py @@ -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 [] diff --git a/sith/settings.py b/sith/settings.py index 25165fc8..0e1c1002 100644 --- a/sith/settings.py +++ b/sith/settings.py @@ -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: