https://github.com/ckan/ckan/blob/2.9/ckanext/example_iconfigurer/plugin.py
參考 CKAN Extensions Tutorial(v2.9):一個空的 CKAN extension,建立 ckanext-example_iconfigurer 擴充套件。
建立:ckanext-example_iconfigurer
-
進入Python Virtual Environment:
. /usr/lib/ckan/default/bin/activate - 切換目錄
cd /usr/lib/ckan/default/src - 執行 ckan generate extension,輸入:ckanext-example_iconfigurer。
撰寫 plugin 類別:ExampleIConfigurerPlugin
修改 ckanext-example_iconfigurer/ckanext/example_iconfigurer/plugin.py。參考完整程式,主要內容如下:
略...
import ckanext.example_iconfigurer.blueprint as blueprint
class ExampleIConfigurerPlugin(plugins.SingletonPlugin):
略...
plugins.implements(plugins.IConfigurer)
plugins.implements(plugins.IBlueprint)
# IConfigurer
def update_config(self, config):
# Add extension templates directory
toolkit.add_template_directory(config, u'templates')
# Add a new ckan-admin tabs for our extension
toolkit.add_ckan_admin_tab(
config, u'example_iconfigurer.config_one',
u'My First Custom Config Tab'
)
toolkit.add_ckan_admin_tab(
config, u'example_iconfigurer.config_two',
u'My Second Custom Config Tab'
)
撰寫 blueprint.py
參考完整程式,主要內容如下:
# encoding: utf-8
import ckan.lib.base as base
import ckan.lib.helpers as helpers
from flask import Blueprint
render = base.render
example_iconfigurer = Blueprint(u'example_iconfigurer', __name__)
def config_one():
u'''Render the config template with the first custom title.'''
return render(
u'admin/myext_config.html',
extra_vars={u'title': u'Custom Config Page A'}
)
def config_two():
u'''Render the config template with the second custom title.'''
return render(
u'admin/myext_config.html',
extra_vars={u'title': u'Custom Config Page B'}
)
def build_extra_admin_nav():
u'''Return results of helpers.build_extra_admin_nav for testing.'''
return helpers.build_extra_admin_nav()
example_iconfigurer.add_url_rule(
u'/ckan-admin/myext_config_one', view_func=config_one
)
example_iconfigurer.add_url_rule(
u'/ckan-admin/myext_config_two', view_func=config_two
)
example_iconfigurer.add_url_rule(
u'/build_extra_admin_nav', view_func=build_extra_admin_nav
)
修改 setup.py:設定 entry_points
修改 ckanext-example_iconfigurer/setup.py 的 entry_points:
entry_points='''
[ckan.plugins]
example_iconfigurer=ckanext.example_iconfigurer.plugin:ExampleIConfigurerPlugin
修改 ckan.ini:啟用 plugin
修改 /etc/ckan/default/ckan.ini。CKAN 的 extension plugins 必須加到 CKAN
設定檔的 ckan.plugins 中,CKAN 才會呼叫。
ckan.plugins = stats text_view 略... example_iconfigurer
安裝
-
進入Python Virtual Environment:
. /usr/lib/ckan/default/bin/activate -
切換目錄:
cd /usr/lib/ckan/default/src/example_iconfigurer - 安裝 Plugin:
python setup.py develop - 重啟 CKAN 服務:
sudo supervisorctl restart ckan-uwsgi:*
測試
瀏覽 http://CKAN web site]/ckan-admin/ 可看到兩個新增的管理頁籤。
沒有留言:
張貼留言