Skip to content

Commit 0612394

Browse files
committed
Fix #50 -- Add check for placeholders missing URL config
1 parent 6c50510 commit 0612394

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

pictures/apps.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from django.apps import AppConfig
2+
3+
4+
class PicturesAppConfig(AppConfig):
5+
name = "pictures"
6+
7+
def ready(self):
8+
import pictures.checks # noqa

pictures/checks.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from django.core.checks import Error, Tags, register
2+
from django.urls import NoReverseMatch, reverse
3+
4+
from . import conf
5+
6+
__all__ = ["placeholder_url_check"]
7+
8+
9+
@register(Tags.urls)
10+
def placeholder_url_check(app_configs, **kwargs):
11+
errors = []
12+
if conf.get_settings().USE_PLACEHOLDERS:
13+
try:
14+
reverse(
15+
"pictures:placeholder",
16+
kwargs={
17+
"alt": "test",
18+
"width": 100,
19+
"ratio": "1x1",
20+
"file_type": "jpg",
21+
},
22+
)
23+
except NoReverseMatch:
24+
errors.append(
25+
Error(
26+
"Placeholder URLs are not configured correctly.",
27+
hint=(
28+
'PICTURES["USE_PLACEHOLDERS"] is True,'
29+
' but include("pictures.urls") is missing for your URL config.'
30+
),
31+
id="pictures.E001",
32+
)
33+
)
34+
return errors

tests/test_checks.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from unittest.mock import Mock
2+
3+
from django.urls import NoReverseMatch
4+
5+
from pictures import checks
6+
7+
8+
def test_placeholder_url_check(settings, monkeypatch):
9+
"""Test that the placeholder URL check works."""
10+
11+
settings.PICTURES["USE_PLACEHOLDERS"] = True
12+
assert not checks.placeholder_url_check({})
13+
14+
reverse = Mock(side_effect=NoReverseMatch)
15+
monkeypatch.setattr(checks, "reverse", reverse)
16+
17+
assert checks.placeholder_url_check({})
18+
19+
settings.PICTURES["USE_PLACEHOLDERS"] = False
20+
assert not checks.placeholder_url_check({})

0 commit comments

Comments
 (0)