Skip to content

Commit

Permalink
fix media storage default logic also on 4.x channel (#272)
Browse files Browse the repository at this point in the history
* fix media storage default logic

* completed the incomplete MEDIA_URL changes to properly function as expected

* linting

* linting

* also set the storage key

* Don't lazy load MEDIA_URL

* Fix lint

---------

Co-authored-by: Michael J. Nicholson <[email protected]>
  • Loading branch information
kinkerl and michjnich authored Jan 30, 2024
1 parent c7c3949 commit 1c2a0c3
Showing 1 changed file with 12 additions and 19 deletions.
31 changes: 12 additions & 19 deletions aldryn_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,30 +391,23 @@ def storage_settings_for_media(self, settings, env):
settings['MEDIA_ROOT'] = env('MEDIA_ROOT', os.path.join(settings['DATA_ROOT'], 'media'))

storage_dsn = env(DEFAULT_STORAGE_KEY, )
settings['DEFAULT_FILE_STORAGE'] = 'aldryn_django.storage.DefaultStorage'
if not storage_dsn:
settings['MEDIA_URL'] = env('MEDIA_URL', '/media/')
if not settings['MEDIA_URL'].endswith('/'):
# Django (or something else?) silently sets MEDIA_URL to an empty
# string if it does not end with a '/'
settings['MEDIA_URL'] = '{}/'.format(settings['MEDIA_URL'])
dsn = furl.furl()
dsn.scheme = 'file'
dsn.path = settings['MEDIA_ROOT']
dsn.args.set("url", env('MEDIA_URL', '/media/'))
dsn.args.set("url", settings['MEDIA_URL'])
storage_dsn = str(dsn)
settings[DEFAULT_STORAGE_KEY] = storage_dsn

settings['DEFAULT_FILE_STORAGE'] = 'aldryn_django.storage.DefaultStorage'

# Handle MEDIA_URL
settings['MEDIA_URL'] = env('MEDIA_URL', '/media/')
if not settings['MEDIA_URL']:
media_url = lazy_setting(
'MEDIA_URL',
get_default_storage_url,
str,
)

settings['MEDIA_URL'] = media_url
elif not settings['MEDIA_URL'].endswith('/'):
# Django (or something else?) silently sets MEDIA_URL to an empty
# string if it does not end with a '/'
settings['MEDIA_URL'] = '{}/'.format(settings['MEDIA_URL'])
settings[DEFAULT_STORAGE_KEY] = storage_dsn
else:
# lazy_setting is incompatible with django-cms and causes error on first load
settings[DEFAULT_STORAGE_KEY] = storage_dsn
settings['MEDIA_URL'] = get_default_storage_url()

# Handle media domain for built-in serving
settings['MEDIA_URL_IS_ON_OTHER_DOMAIN'] = env('MEDIA_URL_IS_ON_OTHER_DOMAIN', None)
Expand Down

0 comments on commit 1c2a0c3

Please sign in to comment.