-
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.
@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: | ||
storages_config = getattr(settings, 'STORAGES', {}) | ||
|
||
if 'PROFILE_IMAGE_STORAGE' in storages_config: |
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.
Note: This implementation is forward-compatible and is intended to work when the legacy PROFILE_IMAGE_BACKEND
setting is removed and the new Django STORAGES
configuration is used instead. For example:
STORAGES = {
'PROFILE_IMAGE_STORAGE': {
'BACKEND': 'storages.backends.s3boto3.S3Boto3Storage',
'OPTIONS': {
'bucket_name': 'profiles',
'default_acl': 'public',
'location': 'profile/images',
}
}
}
openedx/public-engineering#340
Fixing breaking change for django52.