Skip to content

Commit 8d09f1a

Browse files
Update Asset to use StaticFilesStorage (#165)
* Update staticfiles handling to use StaticFilesStorage and improve asset URL generation * Update CHANGELOG.md with asset URL generation fix
1 parent 13fd278 commit 8d09f1a

File tree

4 files changed

+16
-21
lines changed

4 files changed

+16
-21
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ and this project attempts to adhere to [Semantic Versioning](https://semver.org/
1818

1919
## [Unreleased]
2020

21+
### Fixed
22+
- Fixed asset URL generation to properly use Django's static file storage system instead of raw file paths.
23+
2124
## [0.13.0]
2225

2326
🚨 This release contains some breaking changes. See the Deprecated and Removed sections for more information. 🚨

src/django_bird/staticfiles.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
from django.contrib.staticfiles import finders
1515
from django.contrib.staticfiles.finders import BaseFinder
16+
from django.contrib.staticfiles.storage import StaticFilesStorage
1617
from django.core.checks import CheckMessage
1718
from django.core.files.storage import FileSystemStorage
1819
from django.urls import reverse
@@ -85,7 +86,7 @@ def relative_path(self):
8586

8687
@property
8788
def storage(self):
88-
storage = FileSystemStorage(location=str(self.template_dir))
89+
storage = StaticFilesStorage(location=str(self.template_dir))
8990
storage.prefix = DjangoBirdAppConfig.label # type: ignore[attr-defined]
9091
return storage
9192

@@ -101,8 +102,10 @@ def template_dir(self):
101102

102103
@property
103104
def url(self) -> str:
104-
path = finders.find(str(self.relative_path))
105-
return path or reverse(
105+
static_path = finders.find(str(self.relative_path))
106+
if static_path is not None:
107+
return self.storage.url(static_path)
108+
return reverse(
106109
"django_bird:asset",
107110
kwargs={
108111
"component_name": self.path.stem,

tests/conftest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ def pytest_configure(config):
3434
TEST_SETTINGS = {
3535
"INSTALLED_APPS": [
3636
"django_bird",
37+
"django.contrib.staticfiles",
3738
],
39+
"STATIC_URL": "/static/",
3840
"TEMPLATES": [
3941
{
4042
"BACKEND": "django.template.backends.django.DjangoTemplates",

tests/test_staticfiles.py

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ def test_storage(self, templates_dir, settings):
7272
component = Component.from_name(button.name)
7373
asset = component.get_asset(button_css.file.name)
7474

75-
assert str(asset.storage.location) in str(button_css.file)
75+
with override_settings(
76+
STATIC_URL="/static/",
77+
):
78+
assert str(asset.storage.location) in str(button_css.file)
7679

7780
def test_template_dir(self, templates_dir):
7881
button = TestComponent(
@@ -152,7 +155,7 @@ def test_url_with_staticfiles_finder(self, templates_dir):
152155
component = Component.from_name(button.name)
153156
asset = component.get_asset(button_css.file.name)
154157

155-
assert asset.url == str(button_css.file)
158+
assert asset.url == f"/static{button_css.file.parent}/{button_css.file.name}"
156159

157160
def test_url_with_reverse_fallback(self, templates_dir):
158161
button = TestComponent(
@@ -415,14 +418,6 @@ def test_find_all(self, templates_dir):
415418

416419

417420
class TestStaticCollection:
418-
@pytest.fixture(autouse=True)
419-
def staticfiles_app(self):
420-
with override_settings(
421-
INSTALLED_APPS=settings.INSTALLED_APPS + ["django.contrib.staticfiles"],
422-
STATIC_URL="/static/",
423-
):
424-
yield
425-
426421
@pytest.fixture
427422
def static_root(self, tmp_path):
428423
static_dir = tmp_path / "static"
@@ -554,14 +549,6 @@ def test_clear_collection(self, templates_dir, static_root):
554549

555550

556551
class TestStaticTemplateTag:
557-
@pytest.fixture(autouse=True)
558-
def staticfiles_app(self):
559-
with override_settings(
560-
INSTALLED_APPS=settings.INSTALLED_APPS + ["django.contrib.staticfiles"],
561-
STATIC_URL="/static/",
562-
):
563-
yield
564-
565552
def test_static_tag(self, templates_dir):
566553
button = TestComponent(
567554
name="button", content="<button>Click me</button>"

0 commit comments

Comments
 (0)