Skip to content
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

Update checks.py #10462

Closed
wants to merge 4 commits into from
Closed

Conversation

phijma-leukeleu
Copy link
Contributor

Prevent "RemovedInDjango51Warning: The DEFAULT_FILE_STORAGE setting is deprecated. Use STORAGES instead."

Fixes #10461

@squash-labs
Copy link

squash-labs bot commented May 22, 2023

Manage this branch in Squash

Test this branch here: https://phijma-leukeleupatch-1-ihp5w.squash.io

@lb- lb- added status:Needs Review Compatibility For issues that explicitly relate to compatibility with dependencies and other packages labels Jun 3, 2023
Copy link
Member

@lb- lb- left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@phijma-leukeleu thanks for this, could you please update this code to check the Django version instead. This is the way we resolve compatibility issues and also ensure that we can find when to remove the legacy code once we drop support for Django versions in the future.

Examples
https://github.com/wagtail/wagtail/blob/main/wagtail/contrib/modeladmin/views.py#L347
https://github.com/wagtail/wagtail/blob/main/wagtail/images/migrations/0025_alter_image_file_alter_rendition_file.py#L24

if DJANGO_VERSION >= (4, 2):
    # ...

Prevent "RemovedInDjango51Warning: The DEFAULT_FILE_STORAGE setting is deprecated. Use STORAGES instead."
@phijma-leukeleu
Copy link
Contributor Author

@lb- I looked at it, but not sure how to do this in as few lines as possible.

This is all I can think of:

    if DJANGO_VERSION >= (4, 2):
        try:
            file_storage = getattr(settings, "STORAGES")["default"]["BACKEND"]
        except AttributeError:
            file_storage = getattr(settings, "DEFAULT_FILE_STORAGE", None)
    elif DJANGO_VERSION >= (5, 1):
        file_storage = getattr(settings, "STORAGES")["default"]["BACKEND"]
    else:
        file_storage = getattr(settings, "DEFAULT_FILE_STORAGE", None)

Because in django 4.2, both are still valid.

@lb-
Copy link
Member

lb- commented Jun 28, 2023

Sorry for the delay, let's go with that @phijma-leukeleu - it's a bit verbose I agree but makes it easier to understand the version based context.

@lb-
Copy link
Member

lb- commented Oct 5, 2023

Hey @phijma-leukeleu let us know if you are still able to make those changes based on the initial round of feedback.

@phijma-leukeleu
Copy link
Contributor Author

Hey @phijma-leukeleu let us know if you are still able to make those changes based on the initial round of feedback.

Committed the suggested change above.

@lb-
Copy link
Member

lb- commented Oct 18, 2023

Just started the CI, added needs review flag. Thanks @phijma-leukeleu

We are getting a ui_test error as follows now though.

    execute_from_command_line(sys.argv)
  File "/home/circleci/project/.venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "/home/circleci/project/.venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 440, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/circleci/project/.venv/lib/python3.8/site-packages/django/core/management/base.py", line 414, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/circleci/project/.venv/lib/python3.8/site-packages/django/core/management/base.py", line 460, in execute
    output = self.handle(*args, **options)
  File "/home/circleci/project/.venv/lib/python3.8/site-packages/django/core/management/base.py", line 98, in wrapped
    res = handle_func(*args, **kwargs)
  File "/home/circleci/project/.venv/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 91, in handle
    self.check(databases=[database])
  File "/home/circleci/project/.venv/lib/python3.8/site-packages/django/core/management/base.py", line 487, in check
    all_issues = checks.run_checks(
  File "/home/circleci/project/.venv/lib/python3.8/site-packages/django/core/checks/registry.py", line 88, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
  File "/home/circleci/project/wagtail/admin/checks.py", line 192, in file_overwrite_check
    if DJANGO_VERSION >= (4, 2):
NameError: name 'DJANGO_VERSION' is not defined

Co-authored-by: LB (Ben Johnston) <mail@lb.ee>
file_storage = getattr(settings, "STORAGES")["default"]["BACKEND"]
except AttributeError:
file_storage = getattr(settings, "DEFAULT_FILE_STORAGE", None)
elif DJANGO_VERSION >= (5, 1):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This elif doesn't make sense here - if DJANGO_VERSION is greater than (5,1) then the if DJANGO_VERSION >= (4, 2): will have been taken, so this code is never reached.

file_storage = getattr(settings, "DEFAULT_FILE_STORAGE", None)
from django import VERSION as DJANGO_VERSION

if DJANGO_VERSION >= (4, 2):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦

@gasman Something like this then?

Suggested change
if DJANGO_VERSION >= (4, 2):
if (4, 2) <= DJANGO_VERSION < (5, 1):

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would work, but I think it might be more readable to reorder the cases so that DJANGO_VERSION >= (5, 1) comes first, then elif DJANGO_VERSION >= (4, 2).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Done.

The commit history is very messy now. What would you prefer in this case? A new PR? Squashing/renaming commits?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's all fine, thanks! I'll deal with squashing the commits when I merge (which I'll do now - everything looks good).

gasman pushed a commit that referenced this pull request Oct 23, 2023
Prevent "RemovedInDjango51Warning: The DEFAULT_FILE_STORAGE setting is deprecated. Use STORAGES instead."
@gasman
Copy link
Collaborator

gasman commented Oct 23, 2023

Merged in 1fd4b86. Have credited you in the contributors list as phijma-leukeleu - let me know if you'd like that name updating to something else.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Compatibility For issues that explicitly relate to compatibility with dependencies and other packages status:Needs Work
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Deprecation warning about using DEFAULT_FILE_STORAGE
3 participants