8000 Allow using apphook_pool.register() as a decorator. · django-cms/django-cms@07e2b2e · GitHub
[go: up one dir, main page]

Skip to content

Commit 07e2b2e

Browse files
committed
Allow using apphook_pool.register() as a decorator.
If it returns None, then the value of the defined class will be None, which makes it dfficult to access the class! It can still be used in the old way. The lambda is only necessary when register is invoked with arguments, for example: @apphook_pool.register(discovering_apps=True) class MyApp(CMSApp): ... and I don't know if that should be allowed or not.
1 parent 5b202e5 commit 07e2b2e

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

cms/apphook_pool.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@ def clear(self):
2222
self.apps = {}
2323
self.discovered = False
2424

25-
def register(self, app, discovering_apps=False):
25+
def register(self, app=None, discovering_apps=False):
26+
# allow use as a decorator
27+
if app is None:
28+
return lambda app: self.register(app, discovering_apps)
29+
2630
if self.apphooks and not discovering_apps:
27-
return
31+
return app
2832

2933
if app.__name__ in self.apps:
3034
raise AppAlreadyRegistered(
@@ -40,6 +44,7 @@ def register(self, app, discovering_apps=False):
4044
"but the 'menus' attribute is empty, did you make a typo?" % app.__name__)
4145

4246
self.apps[app.__name__] = app
47+
return app
4348

4449
def discover_apps(self):
4550
self.apphooks = get_cms_setting('APPHOOKS')

cms/tests/apphooks.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from django.core.urlresolvers import clear_url_caches, reverse
77

88
from cms.api import create_page, create_title
9+
from cms.app_base import CMSApp
910
from cms.apphook_pool import apphook_pool
1011
from cms.appresolver import applications_page_check, clear_app_resolvers, get_app_patterns
1112
from cms.models import Title
@@ -444,6 +445,20 @@ def test_multiple_apphooks(self):
444445

445446
apphook_pool.clear()
446447

448+
def test_apphook_pool_register_returns_apphook(self):
449+
@apphook_pool.register
450+
class TestApp(CMSApp):
451+
name = "Test App"
452+
self.assertIsNotNone(TestApp)
453+
454+
# Now test the quick return codepath, when apphooks is not empty
455+
apphook_pool.apphooks.append("foo")
456+
457+
@apphook_pool.register
458+
class TestApp2(CMSApp):
459+
name = "Test App 2"
460+
self.assertIsNotNone(TestApp2)
461+
447462

448463
class ApphooksPageLanguageUrlTestCase(SettingsOverrideTestCase):
449464
settings_overrides = {'ROOT_URLCONF': 'cms.test_utils.project.second_urls_for_apphook_tests'}

0 commit comments

Comments
 (0)
0