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

Fix deprecation warning for get_storage_class #669

Merged
merged 2 commits into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,16 @@ When enabled, a graph visualisation generated using [gprof2dot](https://github.c
A custom storage class can be used for the saved generated binary `.prof` files:

```python
# For Django >= 4.2 and Django-Silk >= 5.1.0:
# See https://docs.djangoproject.com/en/5.0/ref/settings/#std-setting-STORAGES
STORAGES = {
'SILKY_STORAGE': {
'BACKEND': 'path.to.StorageClass',
},
# ...
}
Comment on lines +175 to +180
Copy link
Member

Choose a reason for hiding this comment

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

👍🏼


# For Django < 4.2 or Django-Silk < 5.1.0
SILKY_STORAGE_CLASS = 'path.to.StorageClass'
```

Expand Down
17 changes: 15 additions & 2 deletions silk/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from uuid import uuid4

import sqlparse
from django.core.files.storage import get_storage_class
from django.conf import settings
from django.db import models, transaction
from django.db.models import (
BooleanField,
Expand All @@ -25,7 +25,20 @@
from silk.config import SilkyConfig
from silk.utils.profile_parser import parse_profile

silk_storage = get_storage_class(SilkyConfig().SILKY_STORAGE_CLASS)()
try:
# New in Django 4.2
from django.core.files.storage import storages
from django.core.files.storage.handler import InvalidStorageError
try:
silk_storage = storages['SILKY_STORAGE']
except InvalidStorageError:
from django.utils.module_loading import import_string
storage_class = SilkyConfig().SILKY_STORAGE_CLASS or settings.DEFAULT_FILE_STORAGE
silk_storage = import_string(storage_class)()

Check warning on line 37 in silk/models.py

View check run for this annotation

Codecov / codecov/patch

silk/models.py#L31-L37

Added lines #L31 - L37 were not covered by tests
except ImportError:
# Deprecated since Django 4.2, Removed in Django 5.1
from django.core.files.storage import get_storage_class
silk_storage = get_storage_class(SilkyConfig().SILKY_STORAGE_CLASS)()


# Seperated out so can use in tests w/o models
Expand Down
Loading