Skip to content

Commit

Permalink
Ported: Override urlconf_module so that Django system checks don't cr…
Browse files Browse the repository at this point in the history
…ash. (#6873)

Ported from: efbeaa0

* Added test to expose the issue
* Added fix to the AppRegexURLResolver
  • Loading branch information
Aiky30 committed Jun 26, 2020
1 parent d6cabc4 commit f1226a5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
18 changes: 13 additions & 5 deletions cms/appresolver.py
Expand Up @@ -55,13 +55,21 @@ def __init__(self, *args, **kwargs):
self.url_patterns_dict = {}
super(AppRegexURLResolver, self).__init__(*args, **kwargs)

@property
def urlconf_module(self):
# It is valid for urlconf_module to be a list of patterns. So we just
# return the list here.
#
# See https://github.com/django/django/blob/2.2.4/django/urls/resolvers.py#L578
#
return self.url_patterns_dict.get(get_language(), [])

# On URLResolver the url_patterns property is cached and thus calls made after
# language changes (different return values for get_language()) would not return the
# right value. Overriding here prevents caching.
@property
def url_patterns(self):
language = get_language()
if language in self.url_patterns_dict:
return self.url_patterns_dict[language]
else:
return []
return self.urlconf_module

def resolve_page_id(self, path):
"""Resolves requested path similar way how resolve does, but instead
Expand Down
12 changes: 12 additions & 0 deletions cms/tests/test_apphooks.py
Expand Up @@ -5,6 +5,7 @@
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Permission
from django.contrib.sites.models import Site
from django.core import checks
from django.core.cache import cache
from django.test.utils import override_settings
from django.urls import NoReverseMatch, clear_url_caches, resolve, reverse
Expand Down Expand Up @@ -149,6 +150,17 @@ def test_apphook_on_homepage(self):

self.apphook_clear()

@override_settings(ROOT_URLCONF='cms.test_utils.project.urls_for_apphook_tests')
def test_apphook_does_not_crash_django_checks(self):
# This test case reproduced the situation causing the error reported in issue #6717.
self.apphook_clear()
superuser = get_user_model().objects.create_superuser('admin', 'admin@admin.com', 'admin')
create_page("apphooked-page", "nav_playground.html", "en",
created_by=superuser, apphook="SampleApp")
self.reload_urls()
checks.run_checks()
self.apphook_clear()

@override_settings(ROOT_URLCONF='cms.test_utils.project.urls_for_apphook_tests')
def test_apphook_on_root_reverse(self):
self.apphook_clear()
Expand Down

0 comments on commit f1226a5

Please sign in to comment.