Skip to content

Commit 3f08925

Browse files
committed
Only download mermaid.js if MERMAID_USE_CDN is False
This is a follow-up to #20 #21 added a CDN feature, which means the added script tag will point to the CDN version of mermaid.js. But it still tries to download a copy to the package's static directory in AppConfig.ready. Because this isn't necessary in CDN mode and causes us a PermissionError during deployment, I'm proposing we only download in non-CDN mode.
1 parent 8fed55d commit 3f08925

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

src/django_mermaid/apps.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,24 @@
77
from .templatetags import DEFAULT_VERSION
88
from .templatetags import MERMAID_CDN
99

10+
def download_if_necessary(version):
11+
"""Download mermaid.js from CDN if not already present"""
12+
cdn = "https://cdnjs.cloudflare.com/ajax/libs/mermaid/%s/mermaid.min.js" % version
13+
current_dir = pathlib.Path(__file__).parent
14+
static_dir = current_dir / "static"
15+
mermaid_dir = static_dir / "mermaid" / version
16+
mermaid_js = mermaid_dir / "mermaid.js"
17+
if not mermaid_js.exists() or \
18+
mermaid_js.stat().st_size == 0:
19+
mermaid_dir.mkdir(parents=True, exist_ok=True)
20+
urlretrieve(MERMAID_CDN % version, str(mermaid_js))
21+
1022

1123
class MermaidConfig(AppConfig):
1224
name = "django_mermaid"
1325

1426
def ready(self):
15-
"""Download mermaid.js from CDN if not already present"""
16-
version = getattr(settings, "MERMAID_VERSION", DEFAULT_VERSION)
17-
current_dir = pathlib.Path(__file__).parent
18-
static_dir = current_dir / "static"
19-
mermaid_dir = static_dir / "mermaid" / version
20-
mermaid_js = mermaid_dir / "mermaid.js"
21-
if not mermaid_js.exists() or \
22-
mermaid_js.stat().st_size == 0:
23-
mermaid_dir.mkdir(parents=True, exist_ok=True)
24-
urlretrieve(MERMAID_CDN % version, str(mermaid_js))
27+
if not getattr(settings, "MERMAID_USE_CDN", False):
28+
version = getattr(settings, "MERMAID_VERSION", DEFAULT_VERSION)
29+
download_if_necessary(version)
30+

tests/test_package.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from django.test import override_settings
2+
from unittest.mock import patch
3+
from django.apps import apps
4+
5+
@override_settings(MERMAID_USE_CDN=False)
6+
@patch('django_mermaid.apps.download_if_necessary')
7+
def test_download_called_when_use_cdn_false(mock_download):
8+
# Re-run the AppConfig.ready() method manually
9+
app_config = apps.get_app_config('django_mermaid')
10+
app_config.ready()
11+
12+
mock_download.assert_called_once()
13+
14+
15+
@override_settings(MERMAID_USE_CDN=True)
16+
@patch('django_mermaid.apps.download_if_necessary')
17+
def test_download_not_called_when_use_cdn_true(mock_download):
18+
app_config = apps.get_app_config('django_mermaid')
19+
app_config.ready()
20+
21+
mock_download.assert_not_called()

0 commit comments

Comments
 (0)