Skip to content

Commit

Permalink
Merge branch 'fix/no-default-site-breakage' into 'master'
Browse files Browse the repository at this point in the history
Fix site breakage caused when no default site is set

See merge request buckinghamshire-council/bc!687
  • Loading branch information
alxbridge committed Apr 8, 2024
2 parents 0d5f535 + 6a06f82 commit dd5eb98
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ repos:
- id: isort
language_version: python3
- repo: https://github.com/pycqa/flake8
rev: 3.7.7
rev: 3.8.4
hooks:
- id: flake8
language_version: python3
Expand Down
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
# Release History

## Unreleased
## 70.01 (2024-04-03)

- Update `flake8` to version 3.8.4 for Python 3.8 compatibility
- Fix `global_vars` context processor when no default site is set

Compare: <https://git.torchbox.com/buckinghamshire-council/bc/compare/70.01...70.00>

## 70.00 (2024-03-18)

Wagtail 5.2 and Django 4.2 upgrade

Compare: <https://git.torchbox.com/buckinghamshire-council/bc/compare/69.14...HEAD>
Compare: <https://git.torchbox.com/buckinghamshire-council/bc/compare/70.00...69.14>

## 69.14 (2024-02-05)

Expand Down
2 changes: 1 addition & 1 deletion bc/cases/tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class ApteanRespondCaseFormPageFactory(factory.django.DjangoModelFactory):
class Meta:
model = "cases.ApteanRespondCaseFormPage"

title = factory.Sequence(lambda n: f"Aptean Respond Case Form Page")
title = factory.Sequence(lambda n: "Aptean Respond Case Form Page")
listing_summary = "Aptean Respond Case Form Page"
form = APTEAN_FORM_COMPLAINT
completion_title = "foo"
2 changes: 1 addition & 1 deletion bc/forms/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class LookupPageFactory(factory.django.DjangoModelFactory):
class Meta:
model = "forms.LookupPage"

title = factory.Sequence(lambda n: f"Lookup Response Page")
title = factory.Sequence(lambda n: "Lookup Response Page")
listing_summary = "Lookup Response Page"
form_heading = "Look something up"
input_label = "Submit this query"
Expand Down
2 changes: 1 addition & 1 deletion bc/recruitment/tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class RecruitmentHomePageFactory(factory.django.DjangoModelFactory):
class Meta:
model = "recruitment.RecruitmentHomePage"

title = factory.Sequence(lambda n: f"Recruitment HomePage")
title = factory.Sequence(lambda n: "Recruitment HomePage")
listing_summary = "Recruitment HomePage"
hero_image = factory.SubFactory("bc.images.tests.fixtures.ImageFactory")
hero_title = "foo"
Expand Down
4 changes: 2 additions & 2 deletions bc/recruitment_api/management/commands/import_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def handle(self, *args, **options):
# We import jobs for each Recruitment Homepage, including draft ones
# so it is possible to import jobs before going live.
if not RecruitmentHomePage.objects.exists():
msg = f"Please create a RecruitmentHomePage page before running the import."
msg = "Please create a RecruitmentHomePage page before running the import."
self.stdout.write(self.style.ERROR(msg))

for homepage in RecruitmentHomePage.objects.all():
Expand Down Expand Up @@ -157,7 +157,7 @@ def handle(self, *args, **options):
imported_before=import_timestamp, homepage=homepage
)
except Exception as e:
msg = f"Error occurred while deleting jobs:\n" + str(e)
msg = "Error occurred while deleting jobs:\n" + str(e)
errors.append(msg)

self.stdout.write("No more results")
Expand Down
28 changes: 19 additions & 9 deletions bc/utils/context_processors.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import logging

from django.conf import settings

from wagtail.models import Site
Expand All @@ -10,19 +12,27 @@
BASE_PAGE_TEMPLATE_RECRUITMENT,
)

logger = logging.getLogger(__name__)


def global_vars(request):
site = Site.find_for_request(request)
if is_subsite(site):
base_page_template = BASE_PAGE_TEMPLATE_FAMILY_INFORMATION
elif is_recruitment_site(site):
base_page_template = BASE_PAGE_TEMPLATE_RECRUITMENT
if site := Site.find_for_request(request):
if is_subsite(site):
base_page_template = BASE_PAGE_TEMPLATE_FAMILY_INFORMATION
elif is_recruitment_site(site):
base_page_template = BASE_PAGE_TEMPLATE_RECRUITMENT
else:
base_page_template = BASE_PAGE_TEMPLATE

is_pensions_site = (
base_page_template == BASE_PAGE_TEMPLATE_FAMILY_INFORMATION
) and site.root_page.specific.is_pensions_site
else:
logger.warning(
"No site found for request - has the default site been unset or deleted?"
)
base_page_template = BASE_PAGE_TEMPLATE

is_pensions_site = (
base_page_template == BASE_PAGE_TEMPLATE_FAMILY_INFORMATION
) and site.root_page.specific.is_pensions_site
is_pensions_site = False

return {
"GOOGLE_TAG_MANAGER_ID": getattr(settings, "GOOGLE_TAG_MANAGER_ID", None),
Expand Down
45 changes: 44 additions & 1 deletion bc/utils/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.urls import reverse

from wagtail.models import Page, Site
from wagtail.test.utils import WagtailTestUtils

from bc.home.tests.fixtures import HomePageFactory
from bc.recruitment.tests.fixtures import RecruitmentHomePageFactory
Expand All @@ -16,7 +17,7 @@


@override_settings(ALLOWED_HOSTS=[MAIN_HOSTNAME, RECRUITMENT_HOSTNAME])
class BasePageTemplateTest(TestCase):
class BasePageTemplateTest(WagtailTestUtils, TestCase):
def setUp(self):
root_page = Page.objects.get(id=1)

Expand Down Expand Up @@ -124,6 +125,48 @@ def test_recruitment_search_uses_recruitment_site(self):
response.context["base_page_template"], BASE_PAGE_TEMPLATE_RECRUITMENT
)

@override_settings(
ALLOWED_HOSTS=[MAIN_HOSTNAME, RECRUITMENT_HOSTNAME, "oof.example.com"]
)
def test_default_site_handling(self):
"""
Checks that the global_vars context processor handles requests without breaking,
regardless of whether a default site is set
"""
# We're testing the /admin/ page, so we need to log in first
self.user = self.login()

# Test that no site is returned for requests with unknown server names,
# when no default site is set
default_site = Site.objects.get(is_default_site=True)
default_site.is_default_site = False
default_site.save()
request = RequestFactory().get("/admin/", SERVER_NAME="oof.example.com")
self.assertIsNone(Site.find_for_request(request))

# Test that fallback values are added by the context processor
# for requests with unknown server names, when no default site is set
response = self.client.get("/admin/", SERVER_NAME="oof.example.com")
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context["is_pensions_site"], False)
self.assertEqual(response.context["base_page_template"], BASE_PAGE_TEMPLATE)

# Test that the default site is returned for requests with unknown server names,
# when one is set
self.recruitment_site.is_default_site = True
self.recruitment_site.save()
request = RequestFactory().get("/admin/", SERVER_NAME="oof.example.com")
self.assertEqual(Site.find_for_request(request), self.recruitment_site)

# Test that site-specific values are added by the context processor
# for requests with unknown server names, when a default site is set
response = self.client.get("/admin/", SERVER_NAME="oof.example.com")
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context["is_pensions_site"], False)
self.assertEqual(
response.context["base_page_template"], BASE_PAGE_TEMPLATE_RECRUITMENT
)


class NoSearchResultsTemplateTest(TestCase):
def setUp(self):
Expand Down
40 changes: 14 additions & 26 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pymdown-extensions = "9.3"
# Linting
black = "22.3.0"
detect-secrets = "0.13.0"
flake8 = "3.7.7"
flake8 = "3.8.4"
isort = "4.3.18"
seed-isort-config = "1.9.0"

Expand Down

0 comments on commit dd5eb98

Please sign in to comment.