Skip to content

Commit

Permalink
handle missing challenge template
Browse files Browse the repository at this point in the history
falls back to configurable default template if the template is missing
  • Loading branch information
struan committed Aug 14, 2024
1 parent 9d0b29b commit 3f70431
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
8 changes: 8 additions & 0 deletions neighbourhood/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from hashlib import sha256
from random import randrange

from django import template
from django.conf import settings
from django.contrib.auth.models import (
AbstractBaseUser,
Expand Down Expand Up @@ -150,6 +151,13 @@ class Challenge(models.Model):
created = models.DateTimeField(auto_now_add=True)
last_updated = models.DateTimeField(auto_now=True)

def get_template_safe(self):
try:
template.loader.get_template(self.template)
return self.template
except template.TemplateDoesNotExist:
return settings.DEFAULT_CHALLENGE_TEMPLATE

def __str__(self):
return self.name

Expand Down
2 changes: 1 addition & 1 deletion neighbourhood/templates/neighbourhood/team_private.html
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ <h2 class="h4 mb-4">Team progress</h2>
<h2 class="h4 mb-4">Current challenge</h2>

{% if team.challenge %}
{% include team.challenge.template with challenge=team.challenge %}
{% include team.challenge.get_template_safe with challenge=team.challenge %}
{% else %}
<div class="mb-5 p-4 p-md-5 border rounded shadow-lg">
<!-- TODO: Customise the current/next step based on member_count etc -->
Expand Down
10 changes: 10 additions & 0 deletions neighbourhood/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,16 @@ def test_team_with_non_default_template_challenge_set(self):

self.assertContains(response, "Use the share buttons below")

def test_team_with_bad_template_challenge_set(self):
challenge = Challenge.objects.get(name="Recruit first team member")
challenge.template = "neighbourhood/challenges/_missing.html"
challenge.save()

self.client.force_login(User.objects.get(email="[email protected]"))
response = self.client.get(reverse("team", args=("holyrood-palace",)))

self.assertContains(response, "Recruit your first team member")

def test_team_with_no_challenge_set(self):
self.client.force_login(
User.objects.get(email="[email protected]")
Expand Down
2 changes: 2 additions & 0 deletions neighbourhood_warmth/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
ALLOWED_HOSTS=(list, []),
HIDE_DEBUG_TOOLBAR=(bool, False),
LOG_LEVEL=(str, "WARNING"),
DEFAULT_CHALLENGE_TEMPLATE=(str, "neighbourhood/challenges/_default.html"),
)
environ.Env.read_env(BASE_DIR / ".env")

Expand All @@ -36,6 +37,7 @@
HIDE_DEBUG_TOOLBAR = env("HIDE_DEBUG_TOOLBAR")
MAPIT_URL = env("MAPIT_URL")
MAPIT_API_KEY = env("MAPIT_API_KEY")
DEFAULT_CHALLENGE_TEMPLATE = env("DEFAULT_CHALLENGE_TEMPLATE")

# make sure CSRF checking still works behind load balancers
SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")
Expand Down

0 comments on commit 3f70431

Please sign in to comment.