86 lines
3.1 KiB
Python
86 lines
3.1 KiB
Python
# 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.apps import apps
|
|
from django.conf.urls import patterns, include, url
|
|
from django.conf import settings
|
|
|
|
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 apps.get_app_configs():
|
|
if hasattr(plugin, 'get_before_urls'):
|
|
urls = plugin.get_before_urls()
|
|
if urls:
|
|
pre_urls.append(url('^', include(urls)))
|
|
if hasattr(plugin, 'get_after_urls'):
|
|
urls = plugin.get_after_urls()
|
|
if urls:
|
|
post_urls.append(url('^', include(urls)))
|
|
pre_patterns = patterns('', *pre_urls)
|
|
post_patterns = patterns('', *post_urls)
|
|
return pre_patterns + urlpatterns + post_patterns
|
|
|
|
def register_plugins_apps(installed_apps):
|
|
installed_apps = tuple(installed_apps)
|
|
for plugin in get_plugins():
|
|
if hasattr(plugin, 'get_apps'):
|
|
installed_apps += tuple(app for app in plugin.get_apps()
|
|
if app not in installed_apps)
|
|
return installed_apps
|
|
|
|
def register_plugins_middleware(middlewares):
|
|
middlewares = tuple(middlewares)
|
|
for plugin in get_plugins():
|
|
if hasattr(plugin, 'get_before_middleware'):
|
|
middlewares = tuple(m for m in plugin.get_before_middleware()
|
|
if m not in middlewares) + middlewares
|
|
if hasattr(plugin, 'get_after_middleware'):
|
|
middlewares += tuple(m for m in plugin.get_after_middleware()
|
|
if m not in middlewares)
|
|
return middlewares
|
|
|
|
def get_extra_manager_actions():
|
|
'''This iterates over all appconfigs and returns a list of dictionaries,
|
|
with href and text keys.'''
|
|
actions = []
|
|
for plugin in apps.get_app_configs():
|
|
if hasattr(plugin, 'get_extra_manager_actions'):
|
|
actions.extend(plugin.get_extra_manager_actions())
|
|
return actions
|