Skip to content

Commit

Permalink
Fix STORAGES fixer when only one defined (#376)
Browse files Browse the repository at this point in the history
Co-authored-by: Adam Johnson <[email protected]>
  • Loading branch information
browniebroke and adamchainz authored Aug 16, 2023
1 parent 5f43ed0 commit 4bc6021
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
Changelog
=========

* Fix bug in ``STORAGES`` fixer when only one of ``DEFAULT_FILE_STORAGE`` or ``STATICFILES_STORAGE`` was defined.

Thanks to Bruno Alla in `PR #376 <https:/adamchainz/django-upgrade/pull/376>`__.

1.14.0 (2023-06-14)
-------------------

Expand Down
16 changes: 16 additions & 0 deletions src/django_upgrade/fixers/settings_storages.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
"DEFAULT_FILE_STORAGE": "default",
"STATICFILES_STORAGE": "staticfiles",
}
# Keep initial values from Django if one isn't defined
INITIAL_VALUES = {
"default": "django.core.files.storage.FileSystemStorage",
"staticfiles": "django.contrib.staticfiles.storage.StaticFilesStorage",
}


class SettingsDetails:
Expand Down Expand Up @@ -138,5 +143,16 @@ def replace_storages(
" },",
]
)
elif not details.settings_star_import:
new_name = NAME_MAP[name]
initial_value = INITIAL_VALUES[new_name]
src_fragments.extend(
[
f' "{new_name}": {{',
f' "BACKEND": "{initial_value}",',
" },",
]
)

src_fragments.append("}\n")
insert(tokens, i, new_src="\n".join(src_fragments))
31 changes: 30 additions & 1 deletion tests/fixers/test_settings_storages.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def test_setting_exists():
)


def test_one():
def test_default_only():
check_transformed(
"""\
DEFAULT_FILE_STORAGE = "example.backend"
Expand All @@ -107,6 +107,29 @@ def test_one():
"default": {
"BACKEND": "example.backend",
},
"staticfiles": {
"BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage",
},
}
""",
settings,
filename="settings.py",
)


def test_static_only():
check_transformed(
"""\
STATICFILES_STORAGE = "example.other.backend"
""",
"""\
STORAGES = {
"default": {
"BACKEND": "django.core.files.storage.FileSystemStorage",
},
"staticfiles": {
"BACKEND": "example.other.backend",
},
}
""",
settings,
Expand Down Expand Up @@ -166,6 +189,9 @@ def test_retains_quoting():
"default": {
"BACKEND": 'example.backend',
},
"staticfiles": {
"BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage",
},
}
""",
settings,
Expand All @@ -185,6 +211,9 @@ def test_star_import_not_settings():
"default": {
"BACKEND": "example.backend",
},
"staticfiles": {
"BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage",
},
}
""",
settings,
Expand Down

0 comments on commit 4bc6021

Please sign in to comment.