Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #169 - Updates deprecated STATICFILES_STORAGE to STORAGES #173

Merged
merged 3 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,7 @@ jobs:
strategy:
matrix:
python-version: ["3.7", "3.8", "3.9", "3.10"]
django-version: ["2.2", "3.2", "4.0"]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Django versions 3.2 and 4.1 are not yet EOL, please check here: https://www.djangoproject.com/download/#supported-versions

This could break the build for many users.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moreover, if you change this settings, you should also change classifiers in setup.cfg.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed Django 2.2 and added 4.1 and 4.2 to this workflow. I also changed setup.cfg to update the allowed Django versions.

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
django-version: ["4.2"]

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

**django-sass-processor** is shipped with a special finder, to locate the generated `*.css` files
in the directory referred by `SASS_PROCESSOR_ROOT` (or, if unset `STATIC_ROOT`). Just add it to
in the directory referred by `STORAGES['sass_processor']['ROOT']` (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 +129,7 @@ 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 `SASS_PROCESSOR_ROOT` (if unset, this setting defaults to `STATIC_ROOT`).
folder referenced by `STORAGES['sass_processor']['ROOT']` (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 +311,7 @@ above command:

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

Or you may compile results to the `SASS_PROCESSOR_ROOT` directory directy (if not specified - to
Or you may compile results to the `STORAGES['sass_processor']['ROOT']` directory directy (if not specified - to
`STATIC_ROOT`):

```shell
Expand Down Expand Up @@ -424,21 +424,23 @@ to the Django logger.

## Using other storage backends for compiled CSS files

Under the hood, SASS processor will use any storage configured in your settings as `STATICFILES_STORAGE`.
Under the hood, SASS processor will use any storage configured in your settings as `STORAGES['staticfiles']`.
This means you can use anything you normally use for serving static files, e.g. S3.

A custom Storage class can be used if your deployment needs to serve generated CSS files from elsewhere,
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 `SASS_PROCESSOR_STORAGE`. You can also
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 `SASS_PROCESSOR_STORAGE_OPTIONS` (e.g. if you want to use `FileSystemStorage`, but with
in `STORAGES['sass_processor']['OPTIONS']` (e.g. if you want to use `FileSystemStorage`, but with
a different `location` or `base_url`:

```python
SASS_PROCESSOR_STORAGE = 'django.core.files.storage.FileSystemStorage'
SASS_PROCESSOR_STORAGE_OPTIONS = {
'location': '/srv/media/generated',
'base_url': 'https://media.myapp.example.com/generated'
STORAGES['sass_processor'] = {
'BACKEND': 'django.core.files.storage.FileSystemStorage',
'OPTIONS': {
'location': '/srv/media/generated',
'base_url': 'https://media.myapp.example.com/generated'
}
}
```

Expand All @@ -449,7 +451,9 @@ 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
SASS_PROCESSOR_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
STORAGES['sass_processor'] = {
'BACKEND': 'storages.backends.s3boto3.S3Boto3Storage'
}
```


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


class SassFileStorage(LazyObject):
def _setup(self):
storage_path = getattr(settings, 'SASS_PROCESSOR_STORAGE', settings.STATICFILES_STORAGE)
storage_options = getattr(settings, 'SASS_PROCESSOR_STORAGE_OPTIONS', {})
storage_class = get_storage_class(storage_path)
staticfiles_storage_backend = settings.STORAGES.get("staticfiles", {}).get("BACKEND")
sass_processor_storage = settings.STORAGES.get("sass_processor", {})

if storage_path == settings.STATICFILES_STORAGE and issubclass(storage_class, FileSystemStorage):
storage_options['location'] = getattr(settings, 'SASS_PROCESSOR_ROOT', settings.STATIC_ROOT)
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)

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

self._wrapped = storage_class(**storage_options)
Expand Down
Loading