Skip to content

Commit

Permalink
[#2617] Fix to allow IGroupForm group_form use.
Browse files Browse the repository at this point in the history
This commit allows extensions implementing IGroupForm's group_form
method to define a custom form template that will be used for the
default group type, 'group'.
  • Loading branch information
brew committed Sep 7, 2015
1 parent 9026c30 commit 78ca144
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 8 deletions.
2 changes: 0 additions & 2 deletions ckan/controllers/group.py
Expand Up @@ -127,8 +127,6 @@ def _guess_group_type(self, expecting_name=False):
idx = -2

gt = parts[idx]
if gt == 'group':
gt = None

return gt

Expand Down
37 changes: 36 additions & 1 deletion ckanext/example_igroupform/plugin.py
Expand Up @@ -13,11 +13,17 @@

class ExampleIGroupFormPlugin(plugins.SingletonPlugin,
tk.DefaultGroupForm):
'''An example IGroupForm CKAN plugin.
'''An example IGroupForm CKAN plugin with custom group_type.
Doesn't do much yet.
'''
plugins.implements(plugins.IGroupForm, inherit=False)
plugins.implements(plugins.IConfigurer)

# IConfigurer

def update_config(self, config_):
tk.add_template_directory(config_, 'templates')

# IGroupForm

Expand All @@ -26,3 +32,32 @@ def group_types(self):

def is_fallback(self):
False

def group_form(self):
return 'example_igroup_form/group_form.html'


class ExampleIGroupFormPlugin_DefaultGroupType(plugins.SingletonPlugin,
tk.DefaultGroupForm):
'''An example IGroupForm CKAN plugin for default group_type.
Doesn't do much yet.
'''
plugins.implements(plugins.IGroupForm, inherit=False)
plugins.implements(plugins.IConfigurer)

# IConfigurer

def update_config(self, config_):
tk.add_template_directory(config_, 'templates')

# IGroupForm

def group_types(self):
return ('group',)

def is_fallback(self):
False

def group_form(self):
return 'example_igroup_form/group_form.html'
@@ -0,0 +1,7 @@
{% extends "group/new_group_form.html" %}

{# Add some custom text so we know the form was rendered. #}
{% block error_summary %}
{{ super() }}
My Custom Group Form!
{% endblock %}
104 changes: 99 additions & 5 deletions ckanext/example_igroupform/tests/test_controllers.py
Expand Up @@ -10,10 +10,11 @@
webtest_submit = helpers.webtest_submit
submit_and_follow = helpers.submit_and_follow

group_type = u'grup'
custom_group_type = u'grup'
group_type = u'group'


def _get_group_new_page(app):
def _get_group_new_page(app, group_type):
user = factories.User()
env = {'REMOTE_USER': user['name'].encode('ascii')}
response = app.get(
Expand All @@ -36,7 +37,43 @@ def teardown_class(cls):

def test_save(self):
app = self._get_test_app()
env, response = _get_group_new_page(app)
env, response = _get_group_new_page(app, custom_group_type)
form = response.forms['group-edit']
form['name'] = u'saved'

response = submit_and_follow(app, form, env, 'save')
# check correct redirect
assert_equal(response.req.url,
'http://localhost/%s/saved' % custom_group_type)
# check saved ok
group = model.Group.by_name(u'saved')
assert_equal(group.title, u'')
assert_equal(group.type, custom_group_type)
assert_equal(group.state, 'active')

def test_custom_group_form(self):
'''Our custom group form is being used for new groups.'''
app = self._get_test_app()
env, response = _get_group_new_page(app, custom_group_type)

assert_in('My Custom Group Form!', response,
msg="Custom group form not being used.")


class TestGroupControllerNew_DefaultGroupType(helpers.FunctionalTestBase):
@classmethod
def setup_class(cls):
super(TestGroupControllerNew_DefaultGroupType, cls).setup_class()
plugins.load('example_igroupform_default_group_type')

@classmethod
def teardown_class(cls):
plugins.unload('example_igroupform_default_group_type')
super(TestGroupControllerNew_DefaultGroupType, cls).teardown_class()

def test_save(self):
app = self._get_test_app()
env, response = _get_group_new_page(app, group_type)
form = response.forms['group-edit']
form['name'] = u'saved'

Expand All @@ -50,8 +87,16 @@ def test_save(self):
assert_equal(group.type, group_type)
assert_equal(group.state, 'active')

def test_custom_group_form(self):
'''Our custom group form is being used for new groups.'''
app = self._get_test_app()
env, response = _get_group_new_page(app, group_type)

assert_in('My Custom Group Form!', response,
msg="Custom group form not being used.")

def _get_group_edit_page(app, group_name=None):

def _get_group_edit_page(app, group_type, group_name=None):
user = factories.User()
if group_name is None:
group = factories.Group(user=user, type=group_type)
Expand All @@ -74,6 +119,47 @@ def teardown_class(cls):
plugins.unload('example_igroupform')
super(TestGroupControllerEdit, cls).teardown_class()

def test_group_doesnt_exist(self):
app = self._get_test_app()
user = factories.User()
env = {'REMOTE_USER': user['name'].encode('ascii')}
url = url_for('%s_edit' % custom_group_type,
id='doesnt_exist')
app.get(url=url, extra_environ=env,
status=404)

def test_save(self):
app = self._get_test_app()
env, response, group_name = \
_get_group_edit_page(app, custom_group_type)
form = response.forms['group-edit']

response = submit_and_follow(app, form, env, 'save')
group = model.Group.by_name(group_name)
assert_equal(group.state, 'active')
assert_equal(group.type, custom_group_type)

def test_custom_group_form(self):
'''Our custom group form is being used to edit groups.'''
app = self._get_test_app()
env, response, group_name = \
_get_group_edit_page(app, custom_group_type)

assert_in('My Custom Group Form!', response,
msg="Custom group form not being used.")


class TestGroupControllerEdit_DefaultGroupType(helpers.FunctionalTestBase):
@classmethod
def setup_class(cls):
super(TestGroupControllerEdit_DefaultGroupType, cls).setup_class()
plugins.load('example_igroupform_default_group_type')

@classmethod
def teardown_class(cls):
plugins.unload('example_igroupform_default_group_type')
super(TestGroupControllerEdit_DefaultGroupType, cls).teardown_class()

def test_group_doesnt_exist(self):
app = self._get_test_app()
user = factories.User()
Expand All @@ -85,10 +171,18 @@ def test_group_doesnt_exist(self):

def test_save(self):
app = self._get_test_app()
env, response, group_name = _get_group_edit_page(app)
env, response, group_name = _get_group_edit_page(app, group_type)
form = response.forms['group-edit']

response = submit_and_follow(app, form, env, 'save')
group = model.Group.by_name(group_name)
assert_equal(group.state, 'active')
assert_equal(group.type, group_type)

def test_custom_group_form(self):
'''Our custom group form is being used to edit groups.'''
app = self._get_test_app()
env, response, group_name = _get_group_edit_page(app, group_type)

assert_in('My Custom Group Form!', response,
msg="Custom group form not being used.")
1 change: 1 addition & 0 deletions setup.py
Expand Up @@ -96,6 +96,7 @@
'example_idatasetform_v3 = ckanext.example_idatasetform.plugin_v3:ExampleIDatasetFormPlugin',
'example_idatasetform_v4 = ckanext.example_idatasetform.plugin_v4:ExampleIDatasetFormPlugin',
'example_igroupform = ckanext.example_igroupform.plugin:ExampleIGroupFormPlugin',
'example_igroupform_default_group_type = ckanext.example_igroupform.plugin:ExampleIGroupFormPlugin_DefaultGroupType',
'example_iauthfunctions_v1 = ckanext.example_iauthfunctions.plugin_v1:ExampleIAuthFunctionsPlugin',
'example_iauthfunctions_v2 = ckanext.example_iauthfunctions.plugin_v2:ExampleIAuthFunctionsPlugin',
'example_iauthfunctions_v3 = ckanext.example_iauthfunctions.plugin_v3:ExampleIAuthFunctionsPlugin',
Expand Down

0 comments on commit 78ca144

Please sign in to comment.