Skip to content

Commit

Permalink
[#4031] Allow registering of fallback org plugins
Browse files Browse the repository at this point in the history
Right now if an organization plugin implementing `IGroupForm` set
is_fallback to True, the plugin was added as the default *group* plugin,
not the organization one.
  • Loading branch information
amercader committed Feb 22, 2018
1 parent 38cdf62 commit 7ac5546
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions ckan/lib/plugins.py
Expand Up @@ -23,6 +23,7 @@
_group_plugins = {}
# The fallback behaviour
_default_group_plugin = None
_default_organization_plugin = None
# Mapping from group-type strings to controllers
_group_controllers = {}

Expand All @@ -34,6 +35,8 @@ def reset_package_plugins():
_package_plugins = {}
global _default_group_plugin
_default_group_plugin = None
global _default_organization_plugin
_default_organization_plugin = None
global _group_plugins
_group_plugins = {}
global _group_controllers
Expand Down Expand Up @@ -133,22 +136,21 @@ def register_group_plugins(map):
This method will setup the mappings between group types and the
registered IGroupForm instances. If it's called more than once an
exception will be raised.
It will register IGroupForm instances for both groups and organizations
"""
global _default_group_plugin
global _default_organization_plugin

# This function should have not effect if called more than once.
# This should not occur in normal deployment, but it may happen when
# running unit tests.
if _default_group_plugin is not None:
if (_default_group_plugin is not None or
_default_organization_plugin is not None):
return

# Create the mappings and register the fallback behaviour if one is found.
for plugin in plugins.PluginImplementations(plugins.IGroupForm):
if plugin.is_fallback():
if _default_group_plugin is not None:
raise ValueError("More than one fallback IGroupForm has been "
"registered")
_default_group_plugin = plugin

# Get group_controller from plugin if there is one,
# otherwise use 'group'
Expand All @@ -157,6 +159,24 @@ def register_group_plugins(map):
except AttributeError:
group_controller = 'group'

if plugin.is_fallback():
if hasattr(plugin, 'is_organization'):
is_organization = plugin.is_organization
else:
is_organization = group_controller == 'organization'

if is_organization:
if _default_organization_plugin is not None:
raise ValueError("More than one fallback IGroupForm for "
"organizations has been registered")
_default_organization_plugin = plugin

else:
if _default_group_plugin is not None:
raise ValueError("More than one fallback IGroupForm for "
"groups has been registered")
_default_group_plugin = plugin

for group_type in plugin.group_types():
# Create the routes based on group_type here, this will
# allow us to have top level objects that are actually
Expand Down Expand Up @@ -231,6 +251,8 @@ def register_group_plugins(map):
# Setup the fallback behaviour if one hasn't been defined.
if _default_group_plugin is None:
_default_group_plugin = DefaultGroupForm()
if _default_organization_plugin is None:
_default_organization_plugin = DefaultOrganizationForm()
if 'group' not in _group_controllers:
_group_controllers['group'] = 'group'
if 'organization' not in _group_controllers:
Expand Down Expand Up @@ -549,8 +571,6 @@ def edit_template(self):
def activity_template(self):
return 'organization/activity_stream.html'

_default_organization_plugin = DefaultOrganizationForm()


class DefaultTranslation(object):
def i18n_directory(self):
Expand Down

0 comments on commit 7ac5546

Please sign in to comment.