general: use django.apps to register URLs (#7512)
This commit is contained in:
parent
e9565bfbe3
commit
1ef4cb7c63
|
@ -1,9 +1,6 @@
|
|||
import sys
|
||||
import os.path
|
||||
from . import plugins
|
||||
|
||||
apps_dir = os.path.join(os.path.dirname(__file__), 'apps')
|
||||
if apps_dir not in sys.path:
|
||||
sys.path.append(apps_dir)
|
||||
|
||||
plugins.init()
|
||||
|
|
|
@ -13,102 +13,26 @@
|
|||
#
|
||||
# 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/>.
|
||||
"""
|
||||
Use setuptools entrypoints to find plugins
|
||||
Propose helper methods to load urls from plugins or modify INSTALLED_APPS,
|
||||
MIDDLEWARE_CLASSES, etc
|
||||
"""
|
||||
import pkg_resources
|
||||
import logging
|
||||
|
||||
from django.apps import apps
|
||||
from django.conf.urls import patterns, include, url
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
DEFAULT_GROUP_NAME = 'passerelle.plugin'
|
||||
|
||||
__ALL__ = ['get_plugins']
|
||||
|
||||
PLUGIN_CACHE = {}
|
||||
|
||||
class PluginError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def get_plugins(group_name=DEFAULT_GROUP_NAME, use_cache=True, *args, **kwargs):
|
||||
'''Traverse all entry points for group_name and instantiate them using args
|
||||
and kwargs.
|
||||
def register_apps_urls(urlpatterns):
|
||||
'''Call get_before_urls and get_after_urls on all apps providing them,
|
||||
add those urls to the given urlpatterns (before or after).
|
||||
'''
|
||||
global PLUGIN_CACHE
|
||||
if group_name in PLUGIN_CACHE and use_cache:
|
||||
return PLUGIN_CACHE[group_name]
|
||||
plugins = []
|
||||
for entrypoint in pkg_resources.iter_entry_points(group_name):
|
||||
try:
|
||||
plugin_callable = entrypoint.load()
|
||||
except Exception, e:
|
||||
logger.exception('unable to load entrypoint %s', entrypoint)
|
||||
raise PluginError('unable to load entrypoint %s' % entrypoint, e)
|
||||
plugins.append(plugin_callable(*args, **kwargs))
|
||||
PLUGIN_CACHE[group_name] = plugins
|
||||
return plugins
|
||||
|
||||
def register_plugins_urls(urlpatterns,
|
||||
group_name=DEFAULT_GROUP_NAME):
|
||||
'''Call get_before_urls and get_after_urls on all plugins providing them
|
||||
and add those urls to the given urlpatterns.
|
||||
|
||||
URLs returned by get_before_urls() are added to the head of urlpatterns
|
||||
and those returned by get_after_urls() are added to the tail of
|
||||
urlpatterns.
|
||||
'''
|
||||
plugins = get_plugins(group_name)
|
||||
before_urls = []
|
||||
after_urls = []
|
||||
for plugin in plugins:
|
||||
if hasattr(plugin, 'get_before_urls'):
|
||||
urls = plugin.get_before_urls()
|
||||
for app in apps.get_app_configs():
|
||||
if hasattr(app, 'get_before_urls'):
|
||||
urls = app.get_before_urls()
|
||||
if urls:
|
||||
before_urls.append(url('^', include(urls)))
|
||||
if hasattr(plugin, 'get_after_urls'):
|
||||
urls = plugin.get_after_urls()
|
||||
if hasattr(app, 'get_after_urls'):
|
||||
urls = app.get_after_urls()
|
||||
if urls:
|
||||
after_urls.append(url('^', include(urls)))
|
||||
before_patterns = patterns('', *before_urls)
|
||||
after_patterns = patterns('', *after_urls)
|
||||
return before_patterns + urlpatterns + after_patterns
|
||||
|
||||
def register_plugins_installed_apps(installed_apps, group_name=DEFAULT_GROUP_NAME):
|
||||
'''Call get_apps() on all plugins of group_name and add the returned
|
||||
applications path to the installed_apps sequence.
|
||||
|
||||
Applications already present are ignored.
|
||||
'''
|
||||
installed_apps = tuple(installed_apps)
|
||||
for plugin in get_plugins(group_name):
|
||||
if hasattr(plugin, 'get_apps'):
|
||||
apps = 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(middleware_classes,
|
||||
group_name=DEFAULT_GROUP_NAME):
|
||||
middleware_classes = list(middleware_classes)
|
||||
for plugin in get_plugins(group_name):
|
||||
if hasattr(plugin, 'get_before_middleware'):
|
||||
apps = plugin.get_before_middleware()
|
||||
for app in reversed(apps):
|
||||
if app not in middleware_classes:
|
||||
middleware_classes.insert(0, app)
|
||||
if hasattr(plugin, 'get_after_middleware'):
|
||||
apps = plugin.get_after_middleware()
|
||||
for app in apps:
|
||||
if app not in middleware_classes:
|
||||
middleware_classes.append(app)
|
||||
return tuple(middleware_classes)
|
||||
|
||||
def init():
|
||||
for plugin in get_plugins():
|
||||
if hasattr(plugin, 'init'):
|
||||
plugin.init()
|
||||
|
|
|
@ -4,8 +4,6 @@ from django.conf import global_settings
|
|||
import os
|
||||
import logging
|
||||
|
||||
from .plugins import register_plugins_middleware, register_plugins_installed_apps
|
||||
|
||||
try:
|
||||
from logging.handlers import NullHandler
|
||||
except ImportError:
|
||||
|
@ -71,7 +69,6 @@ MIDDLEWARE_CLASSES = global_settings.MIDDLEWARE_CLASSES + (
|
|||
'django.contrib.messages.middleware.MessageMiddleware',
|
||||
'passerelle.base.middleware.SearchApiUser'
|
||||
)
|
||||
MIDDLEWARE_CLASSES = register_plugins_middleware(MIDDLEWARE_CLASSES)
|
||||
|
||||
ROOT_URLCONF = 'passerelle.urls'
|
||||
|
||||
|
@ -114,7 +111,6 @@ INSTALLED_APPS = (
|
|||
# backoffice templates and static
|
||||
'gadjo',
|
||||
)
|
||||
INSTALLED_APPS = register_plugins_installed_apps(INSTALLED_APPS)
|
||||
|
||||
# active applications
|
||||
PASSERELLE_APPS = INSTALLED_APPS
|
||||
|
|
|
@ -10,7 +10,7 @@ from .views import (HomePageView, ManageView, ManageAddView,
|
|||
LEGACY_APPS_PATTERNS, LegacyPageView, login, logout)
|
||||
from .urls_utils import decorated_includes, required, app_enabled
|
||||
from .base.urls import access_urlpatterns
|
||||
from .plugins import register_plugins_urls
|
||||
from .plugins import register_apps_urls
|
||||
|
||||
import choosit.urls
|
||||
import clicrdv.urls
|
||||
|
@ -131,8 +131,8 @@ urlpatterns += required(
|
|||
)
|
||||
)
|
||||
|
||||
# add patterns from plugins
|
||||
urlpatterns = register_plugins_urls(urlpatterns)
|
||||
# add patterns from apps
|
||||
urlpatterns = register_apps_urls(urlpatterns)
|
||||
|
||||
# add authentication patterns
|
||||
urlpatterns += patterns('',
|
||||
|
|
Loading…
Reference in New Issue