Skip to content

fix: Fall back to class name when app name is None #8059

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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
3 changes: 1 addition & 2 deletions cms/apphook_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,8 @@ def get_apphooks(self):

for app_name in self.apps:
app = self.apps[app_name]

if app.get_urls():
hooks.append((app_name, app.name))
hooks.append((app_name, app.name or app_name))

# Unfortunately, we lose the ordering since we now have a list of
# tuples. Let's reorder by app_name:
Expand Down
27 changes: 22 additions & 5 deletions cms/tests/test_apphooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@
from cms.api import create_page, create_page_content
from cms.app_base import CMSApp
from cms.apphook_pool import apphook_pool
from cms.appresolver import (
applications_page_check,
clear_app_resolvers,
get_app_patterns,
)
from cms.appresolver import applications_page_check, clear_app_resolvers, get_app_patterns
from cms.middleware.page import get_page
from cms.models import PageContent
from cms.test_utils.project.placeholderapp.models import Example1
Expand Down Expand Up @@ -332,6 +328,27 @@ def test_apphooks_with_excluded_permissions(self):
self.assertEqual(not_excluded_response.status_code, 302)
self.apphook_clear()

@override_settings(CMS_APPHOOKS=[f'{APP_MODULE}.{APP_NAME}'])
def test_apphook_without_name_defaults_to_class_name(self):
"""
Test that an apphook without a name defaults to the class name.
"""
@apphook_pool.register
class AppWithoutName(CMSApp):
def get_urls(self, page=None, language=None, **kwargs):
return ["sampleapp.urls"]

@apphook_pool.register
class AppWithName(CMSApp):
name = "Custom name"
def get_urls(self, page=None, language=None, **kwargs):
return ["sampleapp.urls"]

hooks = apphook_pool.get_apphooks()
hook_names = dict(hooks)
self.assertEqual(hook_names.get('AppWithoutName'), 'AppWithoutName')
self.assertEqual(hook_names.get('AppWithName'), 'Custom name')

@override_settings(ROOT_URLCONF='cms.test_utils.project.urls_3')
def test_get_page_for_apphook_on_preview_or_edit(self):
if get_user_model().USERNAME_FIELD == 'email':
Expand Down
11 changes: 11 additions & 0 deletions cms/tests/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,17 @@ def test_non_numeric_site_id(self):
with self.settings(SITE_ID='broken'):
self.assertCheck(False, warnings=0, errors=1)

def test_cmsapps_check(self):
from cms.app_base import CMSApp
from cms.apphook_pool import apphook_pool
class AppWithoutName(CMSApp):
def get_urls(self, page=None, language=None, **kwargs):
return ["sampleapp.urls"]

app = apphook_pool.register(AppWithoutName)

self.assertCheck(True, warnings=1, errors=0)
apphook_pool.apps.pop(app.__name__)

class CheckWithDatabaseTests(CheckAssertMixin, TestCase):

Expand Down
12 changes: 12 additions & 0 deletions cms/utils/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,18 @@ def check_template_conf(output):
"Will run in headless mode with one placeholder called \"content\"")


@define_check
def check_cmsapps_names(output):
from cms.apphook_pool import apphook_pool
with output.section("Apphooks") as section:
for hook, name in apphook_pool.get_apphooks():
if apphook_pool.get_apphook(hook).name is None:
section.warn("CMSApps should define a name. %s doesn't have a name" % name)
if section.successful:
section.finish_success("CMSApps configuration is okay")



def check(output):
"""
Checks the configuration/environment of this django CMS installation.
Expand Down