Skip to content

Commit

Permalink
Fixing issue #54
Browse files Browse the repository at this point in the history
STATICFILES_STORAGE and DEFAULT_FILE_STORAGE were replaced by STORAGES in Django 5.1

Minimum supported version now is Django>=4.2
  • Loading branch information
theriverman committed Dec 27, 2024
1 parent e512b06 commit 9559249
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 19 deletions.
11 changes: 9 additions & 2 deletions DjangoExampleProject/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,6 @@
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_STORAGE = 'django_minio_backend.models.MinioBackendStatic'
DEFAULT_FILE_STORAGE = 'django_minio_backend.models.MinioBackend'

# #################### #
# django_minio_backend #
Expand Down Expand Up @@ -173,3 +171,12 @@
MINIO_MEDIA_FILES_BUCKET = 'my-media-files-bucket' # replacement for STATIC_ROOT
MINIO_STATIC_FILES_BUCKET = 'my-static-files-bucket' # replacement for MEDIA_ROOT
MINIO_BUCKET_CHECK_ON_SAVE = False # Create bucket if missing, then save

STORAGES = { # -- ADDED IN Django 5.1
"default": {
"BACKEND": "django_minio_backend.models.MinioBackend",
},
"staticfiles": {
"BACKEND": "django_minio_backend.models.MinioBackendStatic",
},
}
38 changes: 29 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ because this operation can noticeably slow down Django's boot time when many buc
from datetime import timedelta
from typing import List, Tuple

STORAGES = { # -- ADDED IN Django 5.1
"default": {
"BACKEND": "django_minio_backend.models.MinioBackend",
},
# "staticfiles": { # -- OPTIONAL
# "BACKEND": "django_minio_backend.models.MinioBackendStatic",
# },
}

MINIO_ENDPOINT = 'minio.your-company.co.uk'
MINIO_EXTERNAL_ENDPOINT = "external-minio.your-company.co.uk" # Default is same as MINIO_ENDPOINT
MINIO_EXTERNAL_ENDPOINT_USE_HTTPS = True # Default is same as MINIO_USE_HTTPS
Expand Down Expand Up @@ -93,7 +102,7 @@ class PrivateAttachment(models.Model):
```

5. Initialize the buckets & set their public policy (OPTIONAL):<br>
This `django-admin` command creates both the private and public buckets in case one of them does not exists,
This `django-admin` command creates both the private and public buckets in case one of them does not exist,
and sets the *public* bucket's privacy policy from `private`(default) to `public`.<br>
```bash
python manage.py initialize_buckets
Expand All @@ -103,13 +112,20 @@ Code reference: [initialize_buckets.py](django_minio_backend/management/commands

### Static Files Support
**django-minio-backend** allows serving static files from MinIO.
To learn more about Django static files, see [Managing static files](https://docs.djangoproject.com/en/3.2/howto/static-files/), and [STATICFILES_STORAGE](https://docs.djangoproject.com/en/3.2/ref/settings/#staticfiles-storage).
To learn more about Django static files, see [Managing static files](https://docs.djangoproject.com/en/5.1/howto/static-files/), [STATICFILES_STORAGE](https://docs.djangoproject.com/en/5.1/ref/settings/#static-files) and [STORAGES](https://docs.djangoproject.com/en/5.1/ref/settings/#std-setting-STORAGES).

To enable static files support, update your `settings.py`:
```python
STATICFILES_STORAGE = 'django_minio_backend.models.MinioBackendStatic'
STORAGES = { # -- ADDED IN Django 5.1
"default": {
"BACKEND": "django_minio_backend.models.MinioBackend",
},
"staticfiles": { # -- ADD THESE LINES FOR STATIC FILES SUPPORT
"BACKEND": "django_minio_backend.models.MinioBackendStatic",
},
}
MINIO_STATIC_FILES_BUCKET = 'my-static-files-bucket' # replacement for STATIC_ROOT
# Add the value of MINIO_STATIC_FILES_BUCKET to one of the pre-configured bucket lists. eg.:
# Add the value of MINIO_STATIC_FILES_BUCKET to one of the pre-configured bucket lists. e.g.:
# MINIO_PRIVATE_BUCKETS.append(MINIO_STATIC_FILES_BUCKET)
# MINIO_PUBLIC_BUCKETS.append(MINIO_STATIC_FILES_BUCKET)
```
Expand All @@ -124,13 +140,17 @@ otherwise **django-minio-backend** will raise an exception. This setting determi

### Default File Storage Support
**django-minio-backend** can be configured as a default file storage.
To learn more, see [DEFAULT_FILE_STORAGE](https://docs.djangoproject.com/en/3.2/ref/settings/#default-file-storage).
To learn more, see [STORAGES](https://docs.djangoproject.com/en/5.1/ref/settings/#std-setting-STORAGES).

To configure **django-minio-backend** as the default file storage, update your `settings.py`:
```python
DEFAULT_FILE_STORAGE = 'django_minio_backend.models.MinioBackend'
STORAGES = { # -- ADDED IN Django 5.1
"default": {
"BACKEND": "django_minio_backend.models.MinioBackend",
}
}
MINIO_MEDIA_FILES_BUCKET = 'my-media-files-bucket' # replacement for MEDIA_ROOT
# Add the value of MINIO_STATIC_FILES_BUCKET to one of the pre-configured bucket lists. eg.:
# Add the value of MINIO_STATIC_FILES_BUCKET to one of the pre-configured bucket lists. e.g.:
# MINIO_PRIVATE_BUCKETS.append(MINIO_STATIC_FILES_BUCKET)
# MINIO_PUBLIC_BUCKETS.append(MINIO_STATIC_FILES_BUCKET)
```
Expand Down Expand Up @@ -205,8 +225,8 @@ To learn more about Docker networking, see [Networking overview](https://docs.do
See [README.Docker.md](README.Docker.md) for a real-life Docker Compose demonstration.

## Compatibility
* Django 3.2 or later
* Python 3.8.0 or later
* Django 4.2 or later
* Python 3.10.0 or later
* MinIO SDK 7.0.2 or later

## Contribution
Expand Down
11 changes: 9 additions & 2 deletions django_minio_backend/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ class DjangoMinioBackendConfig(AppConfig):
name = 'django_minio_backend'

def ready(self):
# Validate configuration for Django 5.1=< projects
if STATICFILES_STORAGE := get_setting('STATICFILES_STORAGE'):
if STATICFILES_STORAGE.endswith(MinioBackendStatic.__name__):
raise ConfigurationError("STATICFILES_STORAGE and DEFAULT_FILE_STORAGE were replaced by STORAGES. "
"See django-minio-backend's README for more information.")

mb = MinioBackend()
mb.validate_settings()

Expand All @@ -26,7 +32,8 @@ def ready(self):
raise ConfigurationError('MINIO_EXTERNAL_ENDPOINT must be configured together with MINIO_EXTERNAL_ENDPOINT_USE_HTTPS')

# Validate static storage and default storage configurations
staticfiles_storage: str = get_setting('STATICFILES_STORAGE')
if staticfiles_storage.endswith(MinioBackendStatic.__name__):
storages = get_setting('STORAGES')
staticfiles_backend = storages["staticfiles"]["BACKEND"]
if staticfiles_backend.endswith(MinioBackendStatic.__name__):
mbs = MinioBackendStatic()
mbs.check_bucket_existence()
2 changes: 1 addition & 1 deletion django_minio_backend/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ def validate_settings(self):
class MinioBackendStatic(MinioBackend):
"""
MinIO-compatible Django custom storage system for Django static files.
The used bucket can be configured in settings.py through `MINIO_STATIC_FILES_BUCKET`
The used bucket can be configured in settings.py through `STORAGES.staticfiles.BACKEND`
:arg *args: Should not be used for static files. It's here for compatibility only
:arg **kwargs: Should not be used for static files. It's here for compatibility only
"""
Expand Down
10 changes: 5 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,23 @@
author='Kristof Daja (theriverman)',
author_email='[email protected]',
install_requires=[
'Django>=3.2',
'Django>=4.2',
'minio>=7.2.8'
],
classifiers=[
'Environment :: Web Environment',
'Framework :: Django',
'Framework :: Django :: 3.2',
'Framework :: Django :: 4.0',
'Framework :: Django :: 4.1',
'Framework :: Django :: 4.2',
'Framework :: Django :: 5.0',
'Framework :: Django :: 5.1',
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
'Operating System :: OS Independent',
'Programming Language :: Python',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: 3.11',
'Programming Language :: Python :: 3.12',
'Programming Language :: Python :: 3.13',
'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
'Topic :: Internet :: WWW/HTTP :: Dynamic Content :: Content Management System',
'Topic :: Internet :: WWW/HTTP :: WSGI :: Application',
Expand Down

0 comments on commit 9559249

Please sign in to comment.