Skip to content
Draft
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
1 change: 1 addition & 0 deletions debug_hook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print("use_sentry value:", "{{cookiecutter.use_sentry}}")
3 changes: 3 additions & 0 deletions hooks/post_gen_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ def remove(path):
if "{{cookiecutter.css}}" != "Bootstrap":
remove("{{cookiecutter.project_slug}}/static/bootstrap.min.css")
remove("{{cookiecutter.project_slug}}/static/bootstrap.min.js")

if "{{cookiecutter.use_sentry}}" == "False":
remove("{{cookiecutter.project_slug}}/core/context_processors.py")
51 changes: 43 additions & 8 deletions tests/test_sentry.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,75 @@
def test_default_true(cookies):
result = cookies.bake()

with open(result.project / DEFAULT_PROJECT / "settings.py") as f:
with open(result.project_path / DEFAULT_PROJECT / "settings.py") as f:
content = f.read()
assert "import sentry_sdk" in content
assert "from sentry_sdk.integrations.django import DjangoIntegration" in content
assert 'SENTRY_DSN = config("SENTRY_DSN", default=None)' in content
assert "DjangoIntegration(cache_spans=True)" in content
# Check frontend context processor
assert "core.context_processors.sentry_context" in content

with open(result.project / "contrib" / "env-sample") as f:
with open(result.project_path / "contrib" / "env-sample") as f:
assert "SENTRY_DSN=" in f.read()

with open(result.project / ".env") as f:
with open(result.project_path / ".env") as f:
assert "SENTRY_DSN=" in f.read()

with open(result.project / "pyproject.toml") as f:
with open(result.project_path / "pyproject.toml") as f:
assert 'sentry-sdk = {extras = ["django"], version = "' in f.read()

# Check frontend integration
core_app_path = result.project_path / DEFAULT_PROJECT / "core"

# Check context processor file exists
assert (core_app_path / "context_processors.py").exists()

with open(core_app_path / "context_processors.py") as f:
content = f.read()
assert "def sentry_context(request):" in content
assert "SENTRY_DSN" in content
assert "SENTRY_ENV" in content

# Check base.html includes Sentry script
with open(core_app_path / "templates" / "base.html") as f:
content = f.read()
assert "browser.sentry-cdn.com" in content
assert "Sentry.init" in content
assert "{{ SENTRY_DSN" in content


def test_false(cookies):
result = cookies.bake(extra_context={"use_sentry": False})

with open(result.project / DEFAULT_PROJECT / "settings.py") as f:
with open(result.project_path / DEFAULT_PROJECT / "settings.py") as f:
content = f.read()
assert "import sentry_sdk" not in content
assert (
"from sentry_sdk.integrations.django import DjangoIntegration"
not in content
)
assert 'SENTRY_DSN = config("SENTRY_DSN", default=None)' not in content
# Check context processor is not included
assert "core.context_processors.sentry_context" not in content

with open(result.project / "contrib" / "env-sample") as f:
with open(result.project_path / "contrib" / "env-sample") as f:
assert "SENTRY_DSN=" not in f.read()

with open(result.project / ".env") as f:
with open(result.project_path / ".env") as f:
assert "SENTRY_DSN=" not in f.read()

with open(result.project / "pyproject.toml") as f:
with open(result.project_path / "pyproject.toml") as f:
assert 'sentry-sdk = {extras = ["django"], version = "' not in f.read()

# Check frontend integration is not included
core_app_path = result.project_path / DEFAULT_PROJECT / "core"

# Check context processor file doesn't exist
assert not (core_app_path / "context_processors.py").exists()

# Check base.html doesn't include Sentry script
with open(core_app_path / "templates" / "base.html") as f:
content = f.read()
assert "browser.sentry-cdn.com" not in content
assert "Sentry.init" not in content
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% if cookiecutter.use_sentry %}from django.conf import settings
from decouple import config


def sentry_context(request):
"""Add Sentry configuration to template context."""
return {
"SENTRY_DSN": getattr(settings, "SENTRY_DSN", None),
"SENTRY_ENV": config("ENV", default="production"),
}
{% endif %}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,32 @@
{{"{% load django_htmx %}"}}
{{"{% htmx_script %}"}}
{%- endif %}
{%- if cookiecutter.use_sentry %}
<script
src="https://browser.sentry-cdn.com/8.32.0/bundle.min.js"
integrity="sha384-dOOeMzjNyY5lFvDO6OPa6OLJLWBDc21zqfRxvl7VvAKpXiP5ZAYML3vSv2mY1Fp+"
crossorigin="anonymous"
></script>
{%- endif %}
</head>
<body>
<div id="content">

{{"{% block content %}"}}{{"{% endblock %}"}}

</div>
{%- if cookiecutter.use_sentry %}
<script>
const sentryDsn = "{{"{{ SENTRY_DSN|default_if_none:'' }}"}}";
if (sentryDsn) {
Sentry.init({
dsn: sentryDsn,
environment: "{{"{{ SENTRY_ENV|default:'production' }}"}}",
tracesSampleRate: 1.0,
});
}
</script>
{%- endif %}
{%- if cookiecutter.css == 'Bootstrap' %}
<script src="{{"{% static 'bootstrap.min.js' %}"}}" defer></script>
{%- endif %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
{%- if cookiecutter.use_sentry %}
"{{ cookiecutter.project_slug }}.core.context_processors.sentry_context",
{%- endif %}
],
},
},
Expand Down