Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

101 lines (72 sloc) 2.909 kb
# -*- coding: utf-8 -*-
import warnings
from django.core.exceptions import ImproperlyConfigured
from cms.app_base import CMSApp
from cms.exceptions import AppAlreadyRegistered
from cms.utils.conf import get_cms_setting
from cms.utils.django_load import load, iterload_objects
class ApphookPool(object):
def __init__(self):
self.apphooks = []
self.apps = {}
self.discovered = False
def clear(self):
# TODO: remove this method, it's Python, we don't need it.
self.apphooks = []
self.apps = {}
self.discovered = 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 app
if app.__name__ in self.apps:
raise AppAlreadyRegistered(
'A CMS application %r is already registered' % app.__name__)
if not issubclass(app, CMSApp):
raise ImproperlyConfigured(
'CMS application must inherit from cms.app_base.CMSApp, '
'but %r does not' % app.__name__)
if not hasattr(app, 'menus') and hasattr(app, 'menu'):
warnings.warn("You define a 'menu' attribute on CMS application "
"%r, 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')
if self.apphooks:
for cls in iterload_objects(self.apphooks):
self.register(cls, discovering_apps=True)
except AppAlreadyRegistered:
self.discovered = True
def get_apphooks(self):
hooks = []
if not self.discovered:
for app_name in self.apps:
app = self.apps[app_name]
if app.urls:
# Unfortunately, we lose the ordering since we now have a list of
# tuples. Let's reorder by app_name:
hooks = sorted(hooks, key=lambda hook: hook[1])
return hooks
def get_apphook(self, app_name):
if not self.discovered:
return self.apps[app_name]
except KeyError:
# deprecated: return apphooks registered in db with urlconf name
# instead of apphook class name
for app in self.apps.values():
if app_name in app.urls:
return app
raise ImproperlyConfigured('No registered apphook %r found' % app_name)
apphook_pool = ApphookPool()
Jump to Line
Something went wrong with that request. Please try again.