Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

FIWARE Tech Summit - Empower Your CKAN

104 views

Published on

Presentation by Burak Karaboğa
Software Engineer, Atos

FIWARE Tech Summit
28-29 November, 2017
Malaga, Spain

Published in: Technology
  • Be the first to comment

  • Be the first to like this

FIWARE Tech Summit - Empower Your CKAN

  1. 1. Empower Your CKAN: How to Implement CKAN Extensions Burak Karaboga – Atos Research & Innovation
  2. 2. CKAN Extensions? § A Python package that modifies or extends CKAN § Extension = One or more plugins § For example: datastore, datapusher, disqus etc. § Extension catalogue¹ [1] http://extensions.ckan.org 1
  3. 3. Prerequisites § Source installation of CKAN § Basic Python knowledge § Patience 2
  4. 4. Our goals § Modify a core functionality § Change and restrict group creation process § Extend the default application § Add a new UI element to the organizations page 3
  5. 5. Assumptions 4 § By default, only system admins can create groups § For the sake of this tutorial, we assume everyone can § If you like, you can configure your CKAN to reflect this ckan.auth.user_create_groups = true
  6. 6. Let’s start § Run the paster command § Creates an empty extension from template 5 $ . /usr/lib/ckan/default/bin/activate $ cd /usr/lib/ckan/default/src $ paster --plugin=ckan create -t ckanext ckanext-fiwaredemo
  7. 7. What do we have? 6 ckanext-fiwaredemo/ |____ setup.py |____ ckanext_iauthfunctions.egg-info/ |____ ckanext/ |____ __init__.py |____ plugin.py |____ fiwaredemo/ |____ __init__.py |____ fantastic/ |____ i18n/ |____ public/ |____ templates/ |____ tests/ |____ test_plugin.py
  8. 8. Activate the plugin § setup.py § development.ini 7 entry_points, =''' [ckan.plugins] fiwaredemo=ckanext.iauthfunctions.plugin:FiwaredemoPlugin ''' ckan.plugins = stats text_view recline_view fiwaredemo
  9. 9. Modifying group logic § We have 2 steps: 1) Disallow creation of CKAN groups 2) Only allow the members of a group called “Curators” to create CKAN groups 8
  10. 10. plugin.py | initial state 9 class FiwaredemoPlugin(plugins.SingletonPlugin): plugins.implements(plugins.IConfigurer) # IConfigurer def update_config(self, config_): toolkit.add_template_directory(config_, 'templates') toolkit.add_public_directory(config_, 'public') toolkit.add_resource('fanstatic', 'fiwaredemo')
  11. 11. plugin.py | restricting access 10 def group_create(context, data_dict=None): return {'success': False, 'msg': 'No one is allowed to create groups'} class FiwaredemoPlugin(plugins.SingletonPlugin): plugins.implements(plugins.IConfigurer) plugins.implements(plugins.IAuthFunctions) # IConfigurer def update_config(self, config_): toolkit.add_template_directory(config_, 'templates') toolkit.add_public_directory(config_, 'public') toolkit.add_resource('fanstatic', 'fiwaredemo') # IAuthFunctions def get_auth_functions(self): return {'group_create': group_create}
  12. 12. plugin.py | getting smarter 11 def group_create(context, data_dict=None): user_name = context['user'] members = toolkit.get_action('member_list')(data_dict={'id': 'curators', 'object_type': 'user'}) member_ids = [member_tuple[0] for member_tuple in members] convert_user_name_or_id_to_id = toolkit.get_converter('convert_user_name_or_id_to_id') user_id = convert_user_name_or_id_to_id(user_name, context) if user_id in member_ids: return {'success': True} else: return {'success': False, 'msg': 'Only curators are allowed to create groups'}
  13. 13. Extending Organizations Page 12 § We have 4 steps: 1) Create the route for the new page 2) Extend the existing CKAN template 3) Add a new template 4) Create a controller
  14. 14. plugin.py | adding the route 13 class FiwaredemoPlugin(plugins.SingletonPlugin): plugins.implements(plugins.IConfigurer) plugins.implements(plugins.IAuthFunctions) plugins.implements(plugins.IRoutes) # IRoutes def before_map(self, map): with SubMapper(map, controller='ckanext.fiwaredemo.controller:FiwaredemoController') as m: m.connect('ckanext_fiwaredemo_users', '/organization/users/{id}', action='users', ckan_icon='users') return map # IConfigurer def update_config(self, config_): toolkit.add_template_directory(config_, 'templates') toolkit.add_public_directory(config_, 'public') toolkit.add_resource('fanstatic', 'fiwaredemo') # IAuthFunctions def get_auth_functions(self): return {'group_create': group_create}
  15. 15. CKAN templates 14 § CKAN UI extension with templates § {% extends “page.html” %} § {% ckan_extends %} § Rule: same path, same name
  16. 16. CKAN templates | read_base.html 15 {% extends "page.html" %} ... {% block content_primary_nav %} {{ h.build_nav_icon('organization_read', _('Datasets'), id=c.group_dict.name) }} {{ h.build_nav_icon('organization_activity', _('Activity Stream'), id=c.group_dict.name, offset=0) }} {{ h.build_nav_icon('organization_about', _('About'), id=c.group_dict.name) }} {% endblock %} ... ckan/templates/organization/read_base.html
  17. 17. CKAN templates | read_base.html 16 {% ckan_extends %} ... {% block content_primary_nav %} {{ super() }} {{ h.build_nav_icon('ckanext_fiwaredemo_users', _('Users'), id=c.group_dict.name) }} {% endblock %} ... ckanext-fiwaredemo/ckanext/fiwaredemo/templates/organization/read_base.html
  18. 18. Organizations page 17
  19. 19. CKAN templates | users.html 18 {% extends "organization/read_base.html" %} {% block subtitle %}{{ _('Users') }} - {{ super() }}{% endblock %} {% block primary_content_inner %} <h2 class="hide-heading">{% block page_heading %}{{ _('Users') }}{% endblock %}</h2> <h3 class="page-heading">{{ _('{0} member(s)'.format(c.members|length)) }}</h3> <table class="table table-header table-hover table-bordered" id="member-table"> <thead> <tr> <th>{{ _('User') }}</th> <th>{{ _('Role') }}</th> </tr> </thead> <tbody> {% for user_id, user, role in c.members %} <tr> <td class="media"> {{ h.linked_user(user_id, maxlength=20) }} </td> <td>{{ role }}</td> </tr> {% endfor %} </tbody> </table> {% endblock %}
  20. 20. controller.py 19 class FiwaredemoController(OrganizationController): def users(self, id): group_type = self._ensure_controller_matches_group_type(id) context = {'model': model, 'session': model.Session, 'user': c.user} try: data_dict = {'id': id} #check_access('group_edit_permissions', context, data_dict) c.members = self._action('member_list')( context, {'id': id, 'object_type': 'user'} ) data_dict['include_datasets'] = False c.group_dict = self._action('group_show')(context, data_dict) except NotFound: abort(404, _('Group not found')) #except NotAuthorized: # abort(403, _('User %r not authorized to edit members of %s') % (c.user, id)) return self._render_template('organization/users.html', group_type)
  21. 21. Organization Users 20
  22. 22. What’s next? § Source code: https://github.com/McMutton/ckanext-fiwaredemo § Official documentation § Tutorial: http://docs.ckan.org/en/latest/extensions/tutorial.html § Extension guide: http://docs.ckan.org/en/latest/extensions/index.html § Examples: https://github.com/ckan/ckan/tree/master/ckanext 21
  23. 23. Thank you! http://fiware.org Follow @FIWARE on Twitter

×