Skip to content

Commit

Permalink
Allow using apphook_pool.register() as a decorator.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
qris committed Aug 18, 2014
1 parent 5b202e5 commit 07e2b2e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
9 changes: 7 additions & 2 deletions cms/apphook_pool.py
Expand Up @@ -22,9 +22,13 @@ def clear(self):
self.apps = {}
self.discovered = False

def register(self, app, discovering_apps=False):
def register(self, app=None, discovering_apps=False):
# allow use as a decorator
if app is None:
return lambda app: self.register(app, discovering_apps)

if self.apphooks and not discovering_apps:
return
return app

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

self.apps[app.__name__] = app
return app

def discover_apps(self):
self.apphooks = get_cms_setting('APPHOOKS')
Expand Down
15 changes: 15 additions & 0 deletions cms/tests/apphooks.py
Expand Up @@ -6,6 +6,7 @@
from django.core.urlresolvers import clear_url_caches, reverse

from cms.api import create_page, create_title
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.models import Title
Expand Down Expand Up @@ -444,6 +445,20 @@ def test_multiple_apphooks(self):

apphook_pool.clear()

def test_apphook_pool_register_returns_apphook(self):
@apphook_pool.register
class TestApp(CMSApp):
name = "Test App"
self.assertIsNotNone(TestApp)

# Now test the quick return codepath, when apphooks is not empty
apphook_pool.apphooks.append("foo")

@apphook_pool.register
class TestApp2(CMSApp):
name = "Test App 2"
self.assertIsNotNone(TestApp2)


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

0 comments on commit 07e2b2e

Please sign in to comment.