diff --git a/ckan/controllers/group.py b/ckan/controllers/group.py index 7c5706b361d..4add68958ac 100644 --- a/ckan/controllers/group.py +++ b/ckan/controllers/group.py @@ -127,8 +127,6 @@ def _guess_group_type(self, expecting_name=False): idx = -2 gt = parts[idx] - if gt == 'group': - gt = None return gt diff --git a/ckanext/example_igroupform/plugin.py b/ckanext/example_igroupform/plugin.py index bdc724b0ebd..a95a4fd1538 100644 --- a/ckanext/example_igroupform/plugin.py +++ b/ckanext/example_igroupform/plugin.py @@ -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 @@ -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' diff --git a/ckanext/example_igroupform/templates/example_igroup_form/group_form.html b/ckanext/example_igroupform/templates/example_igroup_form/group_form.html new file mode 100644 index 00000000000..55d51396640 --- /dev/null +++ b/ckanext/example_igroupform/templates/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 %} diff --git a/ckanext/example_igroupform/tests/test_controllers.py b/ckanext/example_igroupform/tests/test_controllers.py index 844f674395b..a73e6944f8d 100644 --- a/ckanext/example_igroupform/tests/test_controllers.py +++ b/ckanext/example_igroupform/tests/test_controllers.py @@ -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( @@ -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' @@ -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) @@ -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() @@ -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.") diff --git a/setup.py b/setup.py index 52db9b1ec66..e2ce81ae465 100644 --- a/setup.py +++ b/setup.py @@ -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',