forked from django-cms/django-cms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoolbar_pool.py
52 lines (45 loc) · 1.69 KB
/
toolbar_pool.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# -*- coding: utf-8 -*-
from cms.exceptions import ToolbarAlreadyRegistered
from cms.utils.conf import get_cms_setting
from cms.utils.django_load import load, iterload_objects
from django.core.exceptions import ImproperlyConfigured
class ToolbarPool(object):
def __init__(self):
self.toolbars = {}
self.reverse = {}
self.discovered = False
self.block_register = False
def discover_toolbars(self):
if self.discovered:
return
#import all the modules
toolbars = get_cms_setting('TOOLBARS')
if toolbars:
self.block_register = True
for cls in iterload_objects(toolbars):
self.block_register = False
self.register(cls)
self.block_register = True
self.block_register = False
else:
load('cms_toolbar')
self.discovered = True
def clear(self):
self.apps = {}
self.discovered = False
def register(self, toolbar):
if self.block_register:
return
from cms.toolbar_base import CMSToolbar
# validate the app
if not issubclass(toolbar, CMSToolbar):
raise ImproperlyConfigured('CMS Toolbar must inherit '
'cms.toolbar_base.CMSToolbar, %r does not' % toolbar)
name = "%s.%s" % (toolbar.__module__, toolbar.__name__)
if name in self.toolbars.keys():
raise ToolbarAlreadyRegistered("[%s] a toolbar with this name is already registered" % name)
self.toolbars[name] = toolbar
def get_toolbars(self):
self.discover_toolbars()
return self.toolbars
toolbar_pool = ToolbarPool()