-
Notifications
You must be signed in to change notification settings - Fork 4k
feat!: upgrade code and fix get_storage_class ( compatibility django42 and django52) #36628
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
base: master
Are you sure you want to change the base?
Conversation
Replaced usage of `get_storage_class`, which was deprecated in Django 4.2 and removed in 5.1, with `import_string` to support dynamic storage class loading.
storage_class_path = config.get('class') | ||
storage_class = import_string(storage_class_path) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unsure if this case could ever occur but if the contents of storage_class_path are incorrect, there could be an error which we should catch e.g
try: storage_class = import_string(storage_class_path) except ImportError as e: raise ImproperlyConfigured( f"Could not import storage class '{storage_class_path}': {e}" )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! |
storage_class_path = config.get('class') | ||
storage_class = import_string(storage_class_path) | ||
return storage_class(**config.get('options', {})) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are not we using the below one?
from django.core.files.storage import storages
storage_class = storages(config.get('class'))
return storage_class(**config.get('options', {}))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2ffa43e
to
03e8c83
Compare
@feanil Please review |
if not storage_class_path: | ||
storage_class_path = ( | ||
getattr(settings, 'DEFAULT_FILE_STORAGE', None) or | ||
getattr(settings, 'STORAGES', {}).get('default', {}).get('BACKEND') or | ||
'django.core.files.storage.FileSystemStorage' | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before we fallback to the default storage class, I think we should also have a named storage class in the STORAGES
dictionary. Maybe named profile_images
?
This way, operators could migrate to defining their various storages config via the new STORAGES
config and we can eventually deprecate the old configuration settings.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@feanil you mean some thing like this
storage_class_path = (
getattr(settings, 'DEFAULT_FILE_STORAGE', None) # legacy setting
or getattr(settings, 'STORAGES', {}).get('profile_images', {}).get('BACKEND') # named storage
or getattr(settings, 'STORAGES', {}).get('default', {}).get('BACKEND') # default
or 'django.core.files.storage.FileSystemStorage' # final fallback
)
Along some settings in common.py
STORAGES = {
'profile_images': {
'BACKEND': 'storages.backends.s3boto3.S3Boto3Storage',
'OPTIONS': {
'bucket_name': 'my-bucket',
...
}
},
'default': {
'BACKEND': 'django.core.files.storage.FileSystemStorage'
}
}
@feanil I have verified on django42 this solution will not work. These settings cannot declare together.
These cannot declared together. It raises error
Django code reference Right now |
|
||
if not storage_class_path: | ||
storage_class_path = ( | ||
getattr(settings, 'STORAGES', {}).get('profile_images', {}).get('BACKEND') or # named storages |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
profile_images
it does not exists right now so it will use following storages.
openedx/public-engineering#340
Fixing breaking change for django52.