Skip to content

Skip download in CDN mode #22

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

Merged
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
4 changes: 4 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ classifiers =
Framework :: Django :: 4.0
Framework :: Django :: 4.1
Framework :: Django :: 4.2
Framework :: Django :: 5.0
Framework :: Django :: 5.1
Framework :: Django :: 5.2
Programming Language :: Python
Programming Language :: Python :: 3
Programming Language :: Python :: 3.6
Expand All @@ -40,6 +43,7 @@ classifiers =
Programming Language :: Python :: 3.9
Programming Language :: Python :: 3.10
Programming Language :: Python :: 3.11
Programming Language :: Python :: 3.12
License :: OSI Approved :: MIT License

[options]
Expand Down
2 changes: 1 addition & 1 deletion src/django_mermaid/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.0.9"
__version__ = "0.1.0"
26 changes: 16 additions & 10 deletions src/django_mermaid/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,24 @@
from .templatetags import DEFAULT_VERSION
from .templatetags import MERMAID_CDN

def download_if_necessary(version):
"""Download mermaid.js from CDN if not already present"""
cdn = "https://cdnjs.cloudflare.com/ajax/libs/mermaid/%s/mermaid.min.js" % version
current_dir = pathlib.Path(__file__).parent
static_dir = current_dir / "static"
mermaid_dir = static_dir / "mermaid" / version
mermaid_js = mermaid_dir / "mermaid.js"
if not mermaid_js.exists() or \
mermaid_js.stat().st_size == 0:
mermaid_dir.mkdir(parents=True, exist_ok=True)
urlretrieve(MERMAID_CDN % version, str(mermaid_js))


class MermaidConfig(AppConfig):
name = "django_mermaid"

def ready(self):
"""Download mermaid.js from CDN if not already present"""
version = getattr(settings, "MERMAID_VERSION", DEFAULT_VERSION)
current_dir = pathlib.Path(__file__).parent
static_dir = current_dir / "static"
mermaid_dir = static_dir / "mermaid" / version
mermaid_js = mermaid_dir / "mermaid.js"
if not mermaid_js.exists() or \
mermaid_js.stat().st_size == 0:
mermaid_dir.mkdir(parents=True, exist_ok=True)
urlretrieve(MERMAID_CDN % version, str(mermaid_js))
if not getattr(settings, "MERMAID_USE_CDN", False):
version = getattr(settings, "MERMAID_VERSION", DEFAULT_VERSION)
download_if_necessary(version)

2 changes: 1 addition & 1 deletion src/django_mermaid/templatetags/mermaid.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def startmermaid(parser, token):
else:
theme = None

nodelist = parser.parse(('endmermaid',))
nodelist = parser.parse(("endmermaid",))
parser.delete_first_token()

return MermaidNode(nodelist, theme)
Expand Down
39 changes: 39 additions & 0 deletions tests/test_package.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from os.path import exists, join
from unittest.mock import patch

from django.apps import apps
from django.test import override_settings

try:
import site

site_packages = site.getsitepackages()[0]
except (ImportError, IndexError):
import sysconfig

site_packages = sysconfig.get_paths()["purelib"]


@override_settings(MERMAID_USE_CDN=False)
@patch("django_mermaid.apps.download_if_necessary")
def test_download_called_when_use_cdn_false(mock_download):
# Re-run the AppConfig.ready() method manually
app_config = apps.get_app_config("django_mermaid")
app_config.ready()

mock_download.assert_called_once()


@override_settings(MERMAID_USE_CDN=True)
@patch("django_mermaid.apps.download_if_necessary")
def test_download_not_called_when_use_cdn_true(mock_download):
app_config = apps.get_app_config("django_mermaid")
app_config.ready()

mock_download.assert_not_called()


def test_tag_use_custom_version():
static_dir = join(site_packages, "django_mermaid", "static")
assert exists(join(static_dir, "mermaid", "8.6.3", "mermaid.js"))
assert exists(join(static_dir, "mermaid", "9.4.3", "mermaid.js"))
9 changes: 0 additions & 9 deletions tests/test_tag.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
from os.path import exists
from os.path import join

import pytest
from django.conf import settings
from django.template import Context
Expand Down Expand Up @@ -123,9 +120,3 @@ def test_tag_use_custom_theme_variables_with_base_theme(version, template_code):
"<script>mermaid.initialize({\"startOnLoad\": true, \"theme\": \"base\", \"themeVariables\": "
"{\"primaryColor\": \"#efefef\"}});</script>" % version
)


def test_tag_use_custom_version():
static_dir = join(site_packages, "django_mermaid", "static")
assert exists(join(static_dir, "mermaid", "8.6.3", "mermaid.js"))
assert exists(join(static_dir, "mermaid", "9.4.3", "mermaid.js"))