wagtail/wagtail/wagtailcore/hooks.py
Matt Westcott 7a80c8adca Move the 'hooks' module from wagtailadmin to wagtailcore
Conflicts:
	wagtail/tests/wagtail_hooks.py
	wagtail/wagtailadmin/views/pages.py
2014-07-02 19:59:00 +01:00

38 lines
952 B
Python

from django.conf import settings
try:
from importlib import import_module
except ImportError:
# for Python 2.6, fall back on django.utils.importlib (deprecated as of Django 1.7)
from django.utils.importlib import import_module
_hooks = {}
# TODO: support 'register' as a decorator:
# @hooks.register('construct_main_menu')
# def construct_main_menu(menu_items):
# ...
def register(hook_name, fn):
if hook_name not in _hooks:
_hooks[hook_name] = []
_hooks[hook_name].append(fn)
_searched_for_hooks = False
def search_for_hooks():
global _searched_for_hooks
if not _searched_for_hooks:
for app_module in settings.INSTALLED_APPS:
try:
import_module('%s.wagtail_hooks' % app_module)
except ImportError:
continue
_searched_for_hooks = True
def get_hooks(hook_name):
search_for_hooks()
return _hooks.get(hook_name, [])