Skip to content

Commit

Permalink
feat: add retrocompatibility for Django <= 4.1.*
Browse files Browse the repository at this point in the history
  • Loading branch information
bruunotrindade committed Nov 4, 2023
1 parent 1e4416f commit 2048348
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 14 deletions.
13 changes: 12 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,18 @@ jobs:
strategy:
matrix:
python-version: ["3.7", "3.8", "3.9", "3.10"]
django-version: ["4.2"]
django-version: ["3.2", "4.0", "4.1", "4.2"]
exclude:
- python-version: 3.9
django-version: 2.2
- python-version: 3.10
django-version: 2.2
- python-version: 3.7
django-version: 4.0
- python-version: 3.7
django-version: 4.1
- python-version: 3.7
django-version: 4.2

steps:
- uses: actions/checkout@v3
Expand Down
25 changes: 20 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ INSTALLED_APPS = [
```

**django-sass-processor** is shipped with a special finder, to locate the generated `*.css` files
in the directory referred by `STORAGES['sass_processor']['ROOT']` (or, if unset `STATIC_ROOT`). Just add it to
in the directory referred by `STORAGES['sass_processor']['ROOT']` (for Django >= 4.2.*) or
`SASS_PROCESSOR_ROOT` (for Django <= 4.1.*), or, if unset `STATIC_ROOT`. Just add it to
your `settings.py`. If there is no `STATICFILES_FINDERS` in your `settings.py` don't forget
to include the **Django** [default finders](https://docs.djangoproject.com/en/stable/ref/settings/#std:setting-STATICFILES_FINDERS).

Expand Down Expand Up @@ -129,7 +130,8 @@ letter or number are intended to be included by the HTML tag
`<link href="{% sass_src 'path/to/file.scss' %}" ...>`.

During development, or when `SASS_PROCESSOR_ENABLED = True`, the compiled file is placed into the
folder referenced by `STORAGES['sass_processor']['ROOT']` (if unset, this setting defaults to `STATIC_ROOT`).
folder referenced by `STORAGES['sass_processor']['ROOT']` (for Django >= 4.2.*) or `SASS_PROCESSOR_ROOT` (for Django <= 4.1.*).
If unset, this setting defaults to `STATIC_ROOT`.
Having a location outside of the working directory prevents to pollute your local `static/css/...`
directories with auto-generated files. Therefore assure, that this directory is writable by the
Django runserver.
Expand Down Expand Up @@ -311,7 +313,8 @@ above command:

This will remove all occurrences of previously generated `*.css` files.

Or you may compile results to the `STORAGES['sass_processor']['ROOT']` directory directy (if not specified - to
Or you may compile results to the `STORAGES['sass_processor']['ROOT']` [Django >= 4.2.*] or `SASS_PROCESSOR_ROOT`
[Django <= 4.1.*] directory directy (if not specified - to
`STATIC_ROOT`):

```shell
Expand Down Expand Up @@ -431,17 +434,25 @@ A custom Storage class can be used if your deployment needs to serve generated C
e.g. when your static files storage is not writable at runtime and you nede to re-compile CSS
in production. To use a custom storage, configure it in `STORAGES['sass_processor']['BACKEND']`. You can also
configure a dictionary with options that will be passed to the storage class as keyword arguments
in `STORAGES['sass_processor']['OPTIONS']` (e.g. if you want to use `FileSystemStorage`, but with
a different `location` or `base_url`:
in `STORAGES['sass_processor']['OPTIONS']` [Django >= 4.2.*] or `SASS_PROCESSOR_STORAGE_OPTIONS` [Django <= 4.1.*>]
(e.g. if you want to use `FileSystemStorage`, but with a different `location` or `base_url`:

```python
# For Django >= 4.2.*
STORAGES['sass_processor'] = {
'BACKEND': 'django.core.files.storage.FileSystemStorage',
'OPTIONS': {
'location': '/srv/media/generated',
'base_url': 'https://media.myapp.example.com/generated'
}
}

# For Django <= 4.1.*
SASS_PROCESSOR_STORAGE = 'django.core.files.storage.FileSystemStorage'
SASS_PROCESSOR_STORAGE_OPTIONS = {
'location': '/srv/media/generated',
'base_url': 'https://media.myapp.example.com/generated'
}
```


Expand All @@ -451,9 +462,13 @@ Using the S3 storage backend from [django-storages](https://django-storages.read
with its regular configuration (if you do not otherwise use it for service static files):

```python
# For Django >= 4.2.*
STORAGES['sass_processor'] = {
'BACKEND': 'storages.backends.s3boto3.S3Boto3Storage'
}

# For Django <= 4.1.*
SASS_PROCESSOR_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
```


Expand Down
24 changes: 17 additions & 7 deletions sass_processor/storage.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
from django import VERSION
from django.conf import settings
from django.contrib.staticfiles.finders import get_finders
from django.core.files.storage import FileSystemStorage
from django.core.files.storage import FileSystemStorage, get_storage_class
from django.utils.functional import LazyObject
from django.utils.module_loading import import_string


class SassFileStorage(LazyObject):
def _setup(self):
staticfiles_storage_backend = settings.STORAGES.get("staticfiles", {}).get("BACKEND")
sass_processor_storage = settings.STORAGES.get("sass_processor", {})
version_parts = VERSION[:2]
if version_parts[0] > 4 or (version_parts[0] == 4 and version_parts[1] >= 2):
staticfiles_storage_backend = settings.STORAGES.get("staticfiles", {}).get("BACKEND")
sass_processor_storage = settings.STORAGES.get("sass_processor", {})

storage_path = sass_processor_storage.get("BACKEND") or staticfiles_storage_backend
storage_options = sass_processor_storage.get("OPTIONS") or {}
storage_class = import_string(storage_path)
storage_path = sass_processor_storage.get("BACKEND") or staticfiles_storage_backend
storage_options = sass_processor_storage.get("OPTIONS") or {}
storage_class = import_string(storage_path)
else:
staticfiles_storage_backend = settings.STATICFILES_STORAGE
storage_path = getattr(settings, 'SASS_PROCESSOR_STORAGE', staticfiles_storage_backend)
storage_options = getattr(settings, 'SASS_PROCESSOR_STORAGE_OPTIONS', {})
storage_class = get_storage_class(storage_path)

storage_options["ROOT"] = getattr(settings, 'SASS_PROCESSOR_ROOT', settings.STATIC_ROOT)

if storage_path == staticfiles_storage_backend and issubclass(storage_class, FileSystemStorage):
storage_options['location'] = storage_options.get("ROOT") or settings.STATIC_ROOT
storage_options['location'] = storage_options.pop("ROOT", None) or settings.STATIC_ROOT
storage_options['base_url'] = settings.STATIC_URL

self._wrapped = storage_class(**storage_options)
Expand Down
3 changes: 2 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ classifiers =
Programming Language :: Python :: 3.8
Programming Language :: Python :: 3.9
Programming Language :: Python :: 3.10
Framework :: Django :: 2.2
Framework :: Django :: 3.2
Framework :: Django :: 4.0
Framework :: Django :: 4.1
Framework :: Django :: 4.2

[options]
packages = find:
Expand Down

0 comments on commit 2048348

Please sign in to comment.