Skip to content

Commit

Permalink
#162 fix site_template_base_dir raising an exception if there are no …
Browse files Browse the repository at this point in the history
…wagtail sites
  • Loading branch information
ephes committed Sep 6, 2024
1 parent ba39434 commit 7da494a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
8 changes: 7 additions & 1 deletion cast/context_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from .models import TemplateBaseDirectory

DEFAULT_TEMPLATE_BASE_DIR = "does_not_exist"


def site_template_base_dir(request: HttpRequest) -> dict[str, str]:
"""
Expand All @@ -11,7 +13,11 @@ def site_template_base_dir(request: HttpRequest) -> dict[str, str]:
if hasattr(request, "cast_site_template_base_dir"):
site_template_base_dir_name = request.cast_site_template_base_dir
else:
site_template_base_dir_name = TemplateBaseDirectory.for_request(request).name
try:
site_template_base_dir_name = TemplateBaseDirectory.for_request(request).name
except TemplateBaseDirectory.DoesNotExist:
# If the site template base directory does not exist, use the default
site_template_base_dir_name = DEFAULT_TEMPLATE_BASE_DIR
return {
"cast_site_template_base_dir": site_template_base_dir_name,
"cast_base_template": f"cast/{site_template_base_dir_name}/base.html",
Expand Down
2 changes: 2 additions & 0 deletions docs/releases/0.2.38.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
-------------------

Support for Wagtail 6.2.

- #162 return a default template base dir name ("does_not_exist") instead of raising an exception when there are no wagtail sites
15 changes: 14 additions & 1 deletion tests/theme_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import pytest
from django.template import engines
from django.urls import reverse
from wagtail.models import Site

from cast.context_processors import site_template_base_dir
from cast.context_processors import DEFAULT_TEMPLATE_BASE_DIR, site_template_base_dir
from cast.models.theme import (
get_required_template_names,
get_template_base_dir_candidates,
Expand Down Expand Up @@ -150,3 +151,15 @@ def test_post_select_theme_view_happy(client):
assert response.status_code == 302
assert next_url == response.url
assert client.session["template_base_dir"] == "plain"


@pytest.mark.django_db
def test_non_existent_theme_returns_default(rf):
"""
Make sure that if there are no sites and the template base dir does not exist,
the default is returned.
"""
Site.objects.all().delete() # make sure there are no sites
request = rf.get("/")
context = site_template_base_dir(request)
assert context["cast_site_template_base_dir"] == DEFAULT_TEMPLATE_BASE_DIR

0 comments on commit 7da494a

Please sign in to comment.