Skip to content

Commit

Permalink
Merge pull request #4740 from usingsky/add_admin_tab
Browse files Browse the repository at this point in the history
add_ckan_admin_tab function with icon
  • Loading branch information
smotornyuk committed May 28, 2019
2 parents 451fa1b + 99b71c2 commit 1f4e37c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 19 deletions.
7 changes: 5 additions & 2 deletions ckan/lib/helpers.py
Expand Up @@ -934,8 +934,11 @@ def build_extra_admin_nav():
admin_tabs_dict = config.get('ckan.admin_tabs')
output = ''
if admin_tabs_dict:
for key in admin_tabs_dict:
output += build_nav_icon(key, admin_tabs_dict[key])
for k, v in admin_tabs_dict.iteritems():
if v['icon']:
output += build_nav_icon(k, v['label'], icon=v['icon'])
else:
output += build_nav(k, v['label'])
return output


Expand Down
10 changes: 8 additions & 2 deletions ckan/plugins/toolkit.py
Expand Up @@ -382,14 +382,20 @@ def _add_resource(cls, path, name):

@classmethod
def _add_ckan_admin_tabs(cls, config, route_name, tab_label,
config_var='ckan.admin_tabs'):
config_var='ckan.admin_tabs', icon=None):
'''
Update 'ckan.admin_tabs' dict the passed config dict.
'''
# get the admin_tabs dict from the config, or an empty dict.
admin_tabs_dict = config.get(config_var, {})
# update the admin_tabs dict with the new values
admin_tabs_dict.update({route_name: tab_label})
admin_tabs_dict.update({
route_name: {
'label': tab_label,
'icon': icon
}
}
)
# update the config with the updated admin_tabs dict
config.update({config_var: admin_tabs_dict})

Expand Down
8 changes: 4 additions & 4 deletions ckanext/example_iconfigurer/tests/test_example_iconfigurer.py
Expand Up @@ -63,22 +63,22 @@ def test_build_extra_admin_nav_config_option_present_but_empty(self):
response = app.get('/build_extra_admin_nav')
nosetools.assert_equal(response.body, expected)

@helpers.change_config('ckan.admin_tabs', {'ckanext_myext_config_one': 'My Label'})
@helpers.change_config('ckan.admin_tabs', {'ckanext_myext_config_one': {'label': 'My Label', 'icon': None}})
def test_build_extra_admin_nav_one_value_in_config(self):
'''
Correct string returned when ckan.admin_tabs option has single value in config.
'''
app = self._get_test_app()
expected = """<li><a href="/ckan-admin/myext_config_one"><i class="fa fa-picture-o"></i> My Label</a></li>"""
expected = """<li><a href="/ckan-admin/myext_config_one">My Label</a></li>"""
response = app.get('/build_extra_admin_nav')
nosetools.assert_equal(response.body, expected)

@helpers.change_config('ckan.admin_tabs', {'ckanext_myext_config_one': 'My Label', 'ckanext_myext_config_two': 'My Other Label'})
@helpers.change_config('ckan.admin_tabs', {'ckanext_myext_config_one': {'label': 'My Label', 'icon': 'picture-o'}, 'ckanext_myext_config_two': {'label': 'My Other Label', 'icon': None}})
def test_build_extra_admin_nav_two_values_in_config(self):
'''
Correct string returned when ckan.admin_tabs option has two values in config.
'''
app = self._get_test_app()
expected = """<li><a href="/ckan-admin/myext_config_two"><i class="fa fa-picture-o"></i> My Other Label</a></li><li><a href="/ckan-admin/myext_config_one"><i class="fa fa-picture-o"></i> My Label</a></li>"""
expected = """<li><a href="/ckan-admin/myext_config_two">My Other Label</a></li><li><a href="/ckan-admin/myext_config_one"><i class="fa fa-picture-o"></i> My Label</a></li>"""
response = app.get('/build_extra_admin_nav')
nosetools.assert_equal(response.body, expected)
22 changes: 11 additions & 11 deletions ckanext/example_iconfigurer/tests/test_iconfigurer_toolkit.py
Expand Up @@ -18,7 +18,7 @@ def test_add_ckan_admin_tab_updates_config_dict(self):

toolkit.add_ckan_admin_tab(config, 'my_route_name', 'my_label')

nosetools.assert_equal({'ckan.admin_tabs': {'my_route_name': 'my_label'}}, config)
nosetools.assert_equal({'ckan.admin_tabs': {'my_route_name': {'label': 'my_label', 'icon': None}}}, config)

def test_add_ckan_admin_tab_twice(self):
'''
Expand All @@ -31,7 +31,7 @@ def test_add_ckan_admin_tab_twice(self):
toolkit.add_ckan_admin_tab(config, 'my_route_name', 'my_label')

expected_dict = {
'ckan.admin_tabs': {'my_route_name': 'my_label'}
'ckan.admin_tabs': {'my_route_name': {'label': 'my_label', 'icon': None}}
}

nosetools.assert_equal(expected_dict, config)
Expand All @@ -47,7 +47,7 @@ def test_add_ckan_admin_tab_twice_replace_value(self):
toolkit.add_ckan_admin_tab(config, 'my_route_name', 'my_replacement_label')

expected_dict = {
'ckan.admin_tabs': {'my_route_name': 'my_replacement_label'}
'ckan.admin_tabs': {'my_route_name': {'label': 'my_replacement_label', 'icon': None}}
}

nosetools.assert_equal(expected_dict, config)
Expand All @@ -63,8 +63,8 @@ def test_add_ckan_admin_tab_two_routes(self):

expected_dict = {
'ckan.admin_tabs': {
'my_other_route_name': 'my_other_label',
'my_route_name': 'my_label'
'my_other_route_name': {'label': 'my_other_label', 'icon': None},
'my_route_name': {'label': 'my_label', 'icon': None}
}
}

Expand All @@ -75,17 +75,17 @@ def test_add_ckan_admin_tab_config_has_existing_admin_tabs(self):
Config already has a ckan.admin_tabs option.
'''
config = {
'ckan.admin_tabs': {'my_existing_route': 'my_existing_label'}
'ckan.admin_tabs': {'my_existing_route': {'label': 'my_existing_label', 'icon': None}}
}

toolkit.add_ckan_admin_tab(config, 'my_route_name', 'my_label')
toolkit.add_ckan_admin_tab(config, 'my_other_route_name', 'my_other_label')

expected_dict = {
'ckan.admin_tabs': {
'my_existing_route': 'my_existing_label',
'my_other_route_name': 'my_other_label',
'my_route_name': 'my_label'
'my_existing_route': {'label': 'my_existing_label', 'icon': None},
'my_other_route_name': {'label': 'my_other_label', 'icon': None},
'my_route_name': {'label': 'my_label', 'icon': None}
}
}

Expand All @@ -105,8 +105,8 @@ def test_add_ckan_admin_tab_config_has_existing_other_option(self):
expected_dict = {
'ckan.my_option': 'This is my option',
'ckan.admin_tabs': {
'my_other_route_name': 'my_other_label',
'my_route_name': 'my_label'
'my_other_route_name': {'label': 'my_other_label', 'icon': None},
'my_route_name': {'label': 'my_label', 'icon': None}
}
}

Expand Down

0 comments on commit 1f4e37c

Please sign in to comment.