python - 覆盖 CKAN 中的 url 验证器

标签 python ckan

我想在我的插件中覆盖 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

我没有让它工作。我还需要做其他事情吗?

最佳答案

当您创建或更新(数据集)时,您已将此新验证器插入到 架构中。但是,您正在使用 related_create API 调用,它会创建一个相关对象,解释为什么它不使用您的包验证器。

顺便说一句,我们一个月前在列表中宣布“相关”已弃用,将在下一个版本 (CKAN 2.5) 中删除,因此您应该过渡到 ckanext-showcase。

但是,您的 custom_url_validator 听起来像是对资源 URL 的可喜改进,并鼓励您继续为此进行 PR。

关于python - 覆盖 CKAN 中的 url 验证器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33151215/

相关文章:

python - 属性错误 : module 'sys' has no attribute 'setdefaultencoding'

python - 我一直在尝试使用 pygame 每 20 秒生成一个新敌人

ckan - CKAN 在哪里存储推送到数据存储/文件存储的文件?

datastore - 在 CKAN 中收集的同时将 CSV 数据保存到数据存储中

ckan - Paster db init -c XXXX/development.ini 不适用于 CKAN 命令 'db' 不知道

python - 在其他类方法中定义新的类实例 - Python

Python音频信号分类MFCC特征神经网络

python - Trello API ~ 只是获取列表的内容?

opendata - CKAN:收获特殊数据集