Updated plugin utils. Enabled plugins to override urlpatterns.

This commit is contained in:
Norman Jäckel 2015-07-10 15:07:11 +02:00
parent 66f45ecd1f
commit 1cf8a1f222
3 changed files with 40 additions and 14 deletions

View File

@ -44,6 +44,7 @@ Other:
now. now.
- Updated to Bootstrap 3. - Updated to Bootstrap 3.
- Used SockJS for automatic update of AngularJS driven single page frontend. - Used SockJS for automatic update of AngularJS driven single page frontend.
- Refactored plugin API.
- Refactored start script and management commands. - Refactored start script and management commands.
- Refactored tests. - Refactored tests.
- Used Bower and gulp to manage third party JavaScript and Cascading Style - Used Bower and gulp to manage third party JavaScript and Cascading Style

View File

@ -1,9 +1,12 @@
from django.conf.urls import include, patterns, url from django.conf.urls import include, patterns, url
from django.views.generic import RedirectView from django.views.generic import RedirectView
from openslides.utils.plugins import get_all_plugin_urlpatterns
from openslides.utils.rest_api import router from openslides.utils.rest_api import router
urlpatterns = patterns( urlpatterns = get_all_plugin_urlpatterns()
urlpatterns += patterns(
'', '',
url(r'^(?P<url>.*[^/])$', RedirectView.as_view(url='/%(url)s/', permanent=True)), url(r'^(?P<url>.*[^/])$', RedirectView.as_view(url='/%(url)s/', permanent=True)),
url(r'^rest/', include(router.urls)), url(r'^rest/', include(router.urls)),
@ -12,6 +15,7 @@ urlpatterns = patterns(
url(r'^motions/', include('openslides.motions.urls')), url(r'^motions/', include('openslides.motions.urls')),
url(r'^users/', include('openslides.users.urls')), url(r'^users/', include('openslides.users.urls')),
# The urls.py for the core app has to be the last entry in the urls.py # The urls.py of the core app has to be the last entry. It contains the
# main entry points for OpenSlides' browser clients.
url(r'^', include('openslides.core.urls')), url(r'^', include('openslides.core.urls')),
) )

View File

@ -2,6 +2,7 @@ import os
import pkgutil import pkgutil
import sys import sys
from django.conf import settings
from django.utils.importlib import import_module from django.utils.importlib import import_module
from pkg_resources import iter_entry_points from pkg_resources import iter_entry_points
@ -14,7 +15,9 @@ from openslides.utils.main import (
plugins = {} plugins = {}
def get_plugins_from_entry_points(): # Methods to collect plugins.
def collect_plugins_from_entry_points():
""" """
Collects all entry points in the group openslides_plugins from all Collects all entry points in the group openslides_plugins from all
distributions in the default working set and returns their module names as distributions in the default working set and returns their module names as
@ -23,10 +26,10 @@ def get_plugins_from_entry_points():
return tuple(entry_point.module_name for entry_point in iter_entry_points('openslides_plugins')) return tuple(entry_point.module_name for entry_point in iter_entry_points('openslides_plugins'))
def get_plugins_from_path(path): def collect_plugins_from_path(path):
""" """
Collects all modules/packages in the given `path` Collects all modules/packages in the given `path` and returns a tuple
and returns a tuple of their names of their names.
""" """
return tuple(x[1] for x in pkgutil.iter_modules([path])) return tuple(x[1] for x in pkgutil.iter_modules([path]))
@ -35,16 +38,21 @@ def collect_plugins():
""" """
Collect all plugins that can be automatically discovered. Collect all plugins that can be automatically discovered.
""" """
plugins = get_plugins_from_entry_points() # Collect plugins from entry points.
# add all modules in plugins/ dir of portable automatically collected_plugins = collect_plugins_from_entry_points()
# Collect plugins in plugins/ directory of portable.
if detect_openslides_type() == WINDOWS_PORTABLE_VERSION: if detect_openslides_type() == WINDOWS_PORTABLE_VERSION:
plugins_path = os.path.join( plugins_path = os.path.join(
get_win32_portable_user_data_path(), "plugins") get_win32_portable_user_data_path(), 'plugins')
if plugins_path not in sys.path: if plugins_path not in sys.path:
sys.path.append(plugins_path) sys.path.append(plugins_path)
plugins += get_plugins_from_path(plugins_path) collected_plugins += collect_plugins_from_path(plugins_path)
return plugins
return collected_plugins
# Methods to retrieve plugins and their metadata.
def get_plugin(plugin): def get_plugin(plugin):
""" """
@ -93,7 +101,7 @@ def get_plugin_description(plugin):
def get_plugin_version(plugin): def get_plugin_version(plugin):
""" """
Returns the version string of a plugin. The plugin argument must be a# Returns the version string of a plugin. The plugin argument must be a
python dotted module path. python dotted module path.
""" """
plugin = get_plugin(plugin) plugin = get_plugin(plugin)
@ -107,17 +115,30 @@ def get_plugin_version(plugin):
return version return version
def get_urlpatterns(plugin): def get_plugin_urlpatterns(plugin):
""" """
Returns the urlpatterns object for a plugin. The plugin argument must be Returns the urlpatterns object for a plugin. The plugin argument must be
a python dotted module path. a python dotted module path.
""" """
plugin = get_plugin(plugin) plugin = get_plugin(plugin)
try: try:
urlpatterns = plugin.urlpatterns urlpatterns = plugin.get_urlpatterns()
except AttributeError: except AttributeError:
try: try:
urlpatterns = plugin.urls.urlpatterns urlpatterns = plugin.urls.urlpatterns
except AttributeError: except AttributeError:
urlpatterns = None urlpatterns = None
return urlpatterns return urlpatterns
def get_all_plugin_urlpatterns():
"""
Helper function to return all urlpatterns of all plugins listed in
settings.INSTALLED_PLUGINS.
"""
urlpatterns = []
for plugin in settings.INSTALLED_PLUGINS:
plugin_urlpatterns = get_plugin_urlpatterns(plugin)
if plugin_urlpatterns:
urlpatterns += plugin_urlpatterns
return urlpatterns