我想在我的插件中覆盖ckan的url验证器。我已经按照教程(https://github.com/ckan/ckan/tree/master/ckanext/example_idatasetform)进行了操作,我的插件是:
import logging
import ckan.plugins as plugins
import ckan.plugins.toolkit as toolkit
def custom_url_validator(key, data, errors, context):
''' Checks that the provided value is a valid URL '''
import urlparse
import string
model = context['model']
session = context['session']
url = data.get(key, None)
if not url:
return
pieces = urlparse.urlparse(url)
if all([pieces.scheme, pieces.netloc]) and \
set(pieces.netloc) <= set(string.letters + string.digits + '-.:') and \
pieces.scheme in ['http', 'https']:
return
errors[key].append(_('Custom: Please provide a valid URL'))
class MyPlugin(plugins.SingletonPlugin, toolkit.DefaultDatasetForm):
plugins.implements(plugins.IConfigurer)
plugins.implements(plugins.IDatasetForm)
# IConfigurer
def update_config(self, config_):
toolkit.add_template_directory(config_, 'templates')
toolkit.add_public_directory(config_, 'public')
""" Redefine if this plugin is a default fallback"""
def is_fallback(self):
# Return True to register this plugin as the default handler for
# package types not handled by any other IDatasetForm plugin.
return True
""" Define package types this plugin should be used for has to overwritten because of TODO"""
def package_types(self):
# This plugin doesn't handle any special package types, it just
# registers itself as the default (above).
return []
def _modify_package_schema(self, schema):
schema.update({
# Custom extras
'image_url':[toolkit.get_validator('ignore_missing'), unicode, custom_url_validator]
})
schema.update({
# Custom extras
'url':[toolkit.get_validator('ignore_missing'), unicode, custom_url_validator]
})
return schema
def create_package_schema(self):
schema = super(MyPlugin, self).create_package_schema()
schema = self._modify_package_schema(schema)
return schema
def update_package_schema(self):
schema = super(MyPlugin, self).update_package_schema()
schema = self._modify_package_schema(schema)
return schema
def show_package_schema(self):
schema = super(MyPlugin, self).show_package_schema()
schema.update({
# Custom extras
# Yes, we really do need validators on show, or CKAN crashes
'image_url':[toolkit.get_validator('ignore_missing'), unicode, custom_url_validator]
})
schema.update({
# Custom extras
# Yes, we really do need validators on show, or CKAN crashes
'url':[toolkit.get_validator('ignore_missing'), unicode, custom_url_validator]
})
return schema
我不这样做。我还需要做点什么吗?
答案 0 :(得分:0)
您已将此新验证程序插入程序包架构,以便在创建或更新程序包(数据集)时使用。但是,您使用的是related_create API调用,它会创建一个相关的对象,解释它为什么不使用您的包验证器。
顺便说一下,我们在一个月之前就已经公布了相关的&#39;已弃用,将在下一个版本(CKAN 2.5)中删除,因此您应该转换为ckanext-showcase。
但是,您的custom_url_validator听起来似乎是对资源网址的一种受欢迎的改进,并鼓励您继续使用PR。
答案 1 :(得分:0)
我不确定unicode
调用,它看起来不像是有效的转换器或验证器。这应该是toolkit.get_converter('extras_unicode_convert')
,还是这个你写的东西没有显示?
您还错过了convert_to_extras
和convert_from_extras
来电。您不需要为每个自定义额外调用schema.update
,但这不会破坏任何内容。
尝试使用以下内容替换您的_modify_package_schema
和show_package_schema
,并注意_modify_package_schema
和show_package_schema
中验证器和转换器的排序:
def _modify_package_schema(self, schema):
schema.update({
# Custom extras
'image_url':[toolkit.get_validator('ignore_missing'),
toolkit.get_converter('extras_unicode_convert'),
custom_url_validator,
toolkit.get_converter('convert_to_extras')],
'url':[toolkit.get_validator('ignore_missing'),
toolkit.get_converter('extras_unicode_convert'),
custom_url_validator,
toolkit.get_converter('convert_to_extras')]
})
return schema
...
def show_package_schema(self):
schema = super(MyPlugin, self).show_package_schema()
schema.update({
# Custom extras
'image_url':[toolkit.get_converter('convert_from_extras'),
toolkit.get_validator('ignore_missing'),
toolkit.get_converter('extras_unicode_convert'),
custom_url_validator],
'url':[toolkit.get_converter('convert_from_extras'),
toolkit.get_validator('ignore_missing'),
toolkit.get_converter('extras_unicode_convert'),
custom_url_validator]
})
return schema