general: import plugin system (#6418)
This commit is contained in:
parent
74054c3f61
commit
d7a7d2ed08
|
@ -0,0 +1,19 @@
|
|||
# combo - content management system
|
||||
# Copyright (C) 2015 Entr'ouvert
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify it
|
||||
# under the terms of the GNU Affero General Public License as published
|
||||
# by the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from . import plugins
|
||||
|
||||
plugins.init()
|
|
@ -0,0 +1,75 @@
|
|||
# combo - content management system
|
||||
# Copyright (C) 2015 Entr'ouvert
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify it
|
||||
# under the terms of the GNU Affero General Public License as published
|
||||
# by the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from pkg_resources import iter_entry_points
|
||||
import logging
|
||||
|
||||
from django.conf.urls import patterns, include, url
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
PLUGIN_GROUP_NAME = 'combo.plugin'
|
||||
|
||||
class PluginError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def get_plugins(*args, **kwargs):
|
||||
plugins = []
|
||||
for entrypoint in iter_entry_points(PLUGIN_GROUP_NAME):
|
||||
try:
|
||||
plugin = entrypoint.load()
|
||||
except Exception, e:
|
||||
logger.exception('failed to load entrypoint %s', entrypoint)
|
||||
raise PluginError('failed to load entrypoint %s' % entrypoint, e)
|
||||
plugins.append(plugin(*args, **kwargs))
|
||||
return plugins
|
||||
|
||||
def register_plugins_urls(urlpatterns):
|
||||
pre_urls = []
|
||||
post_urls = []
|
||||
for plugin in get_plugins():
|
||||
if hasattr(plugin, 'get_before_urls'):
|
||||
pre_urls.append(url('^', include(plugin.get_before_urls())))
|
||||
if hasattr(plugin, 'get_after_urls'):
|
||||
post_urls.append(url('^', include(plugin.get_after_urls())))
|
||||
pre_patterns = patterns('', *pre_urls)
|
||||
post_patterns = patterns('', *post_urls)
|
||||
return pre_patterns + urlpatterns + post_patterns
|
||||
|
||||
def register_plugins_apps(installed_apps):
|
||||
for plugin in get_plugins():
|
||||
if hasattr(plugin, 'get_apps'):
|
||||
apps = plugin.get_apps()
|
||||
for app in apps:
|
||||
if app not in installed_apps:
|
||||
installed_apps += (app, )
|
||||
return installed_apps
|
||||
|
||||
def register_plugins_middleware(middlewares):
|
||||
for plugin in get_plugins():
|
||||
if hasattr(plugin, 'get_before_middleware'):
|
||||
pre_middleware = plugin.get_before_middleware()
|
||||
middlewares = tuple(set(pre_middleware + middlewares))
|
||||
if hasattr(plugin, 'get_after_middleware'):
|
||||
post_middleware = plugin.get_after_middleware()
|
||||
middlewares = tuple(set(post_middleware + middlewares))
|
||||
return middlewares
|
||||
|
||||
def init():
|
||||
for plugin in get_plugins():
|
||||
if hasattr(plugin, 'init'):
|
||||
plugin.init()
|
|
@ -26,6 +26,8 @@ and to disable DEBUG mode in production.
|
|||
import os
|
||||
from django.conf import global_settings
|
||||
|
||||
from . import plugins
|
||||
|
||||
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
|
||||
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
|
||||
|
||||
|
@ -61,6 +63,8 @@ INSTALLED_APPS = (
|
|||
'combo.public',
|
||||
)
|
||||
|
||||
INSTALLED_APPS = plugins.register_plugins_apps(INSTALLED_APPS)
|
||||
|
||||
MIDDLEWARE_CLASSES = (
|
||||
'django.contrib.sessions.middleware.SessionMiddleware',
|
||||
'django.middleware.common.CommonMiddleware',
|
||||
|
@ -70,6 +74,8 @@ MIDDLEWARE_CLASSES = (
|
|||
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
||||
)
|
||||
|
||||
MIDDLEWARE_CLASSES = plugins.register_plugins_middleware(MIDDLEWARE_CLASSES)
|
||||
|
||||
# Serve xstatic files, required for gadjo
|
||||
STATICFILES_FINDERS = global_settings.STATICFILES_FINDERS + \
|
||||
('gadjo.finders.XStaticFinder',)
|
||||
|
|
|
@ -23,6 +23,8 @@ from .urls_utils import decorated_includes, manager_required
|
|||
from .public.views import login, logout
|
||||
from .manager.urls import urlpatterns as combo_manager_urls
|
||||
|
||||
from . import plugins
|
||||
|
||||
urlpatterns = patterns('',
|
||||
url(r'^manage/', decorated_includes(manager_required,
|
||||
include(combo_manager_urls))),
|
||||
|
@ -41,5 +43,7 @@ urlpatterns += staticfiles_urlpatterns()
|
|||
from django.conf.urls.static import static
|
||||
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
|
||||
|
||||
urlpatterns = plugins.register_plugins_urls(urlpatterns)
|
||||
|
||||
# other URLs are handled as public URLs
|
||||
urlpatterns += patterns('', url(r'', include('combo.public.urls')))
|
||||
|
|
Loading…
Reference in New Issue