跳转到主要内容

未提供项目描述

项目描述

Tests

ckanext-editable-config

在运行时编辑CKAN配置。

此插件注册了一组API操作,用于覆盖配置选项并将更改应用到应用中,而无需重新启动Web服务器。

示例

更改应用的标题

  • 从Python

    tk.get_action("editable_config_update")(
      {"ignore_auth": True},
      {
        "change": {
          "ckan.site_title": "Updated title"
        },
      },
    )
    
  • 从CLI

    ckanapi action editable_config_update change:'{"ckan.site_title": "Updated via ckanapi"}'
    
  • 从浏览器

    await fetch("/api/action/editable_config_update", {
      method: "POST",
      body: JSON.stringify({change: {"ckan.site_title": "Updated from JS"}}),
      headers: {"content-type": "application/json"},
    })
    

内容

需求

与核心CKAN版本的兼容性

CKAN版本 兼容?
2.9及以下 不兼容
2.10 兼容
master 兼容

安装

要安装ckanext-editable-config

  1. 安装扩展

    pip install ckanext-editable-config
    
  2. editable_config添加到ckan.plugins

使用

所有声明带有editable: true标志的配置选项都可以在启用插件时覆盖。每次更新选项时,都会将新值传递给声明的选项验证器。如果任何验证器引发错误,则不会发生更改。只要选项配置了适当的验证器,更改其值就是安全的。

有3种更改类型

  • change: 选项获得新值。
  • revert:选项获取其上一个值。例如,ckan.site_title 初始值是 CKAN。然后你对其进行了 change 操作,将标题设置为 Updated titlerevertckan.site_title 恢复到 CKANrevert 本身也是一个变化,所以双重撤销不会改变任何内容。它不像 CTRL+Z,而是仅仅交换当前值和上一个值。只有最新的和最新之前的变化被保留。
  • reset:从配置选项中移除所有自定义设置,并将其重置为配置文件中定义的状态。

你真正需要记住的唯一操作是 editable_config_update。它接受 3 个集合

  • change:包含选项名称及其新值的字典
  • revert:将要撤销的选项列表
  • reset:将要重置为配置文件状态的所有选项的列表。

例如,如果你想将站点标题的值更改为 Updated title,将站点描述撤销到上一个值(无论是什么)并重置关于页面的所有自定义设置,请调用 editable_config_update

tk.get_action("editable_config_update")(
    {"ignore_auth": True},
    {
        "change": {"ckan.site_title": "Updated title"},
        "revert": ["ckan.site_description"],
        "reset": ["ckan.site_about"],
    },
)

注意:所有操作都需要系统管理员权限。

如果需要更复杂的操作,请查看下面的 API 操作故障排除 部分。

配置设置

# Additional options that can be changed via API even if they don't have
# `editable` flag enabled. Use it only when you are sure that changinging the
# option value won't break the application. For example, adding
# `ckan.datasets_per_page` here is relatively safe, because it is validated
# as an integer(but it's not 100% safe, because it's possible to assign
# negative number to this option and validators won't complain).
# (optional, default: )
ckanext.editable_config.options.extra_editable = ckan.datasets_per_page ckan.auth.user_create_groups

# Narrow down the list of editable config options. If this option is not
# empty, only `editable` options that are listed here can be changed via API.
# Attempt to change any other option, disregarding its `editable` flag, will
# cause a validation error.
# (optional, default: )
ckanext.editable_config.options.whitelist = ckan.site_title ckan.site_description

# Disable `editable` flag for the specified options. It's not possible to
# change the value of option mentioned here, even if it's `editable`.
# (optional, default: )
ckanext.editable_config.options.blacklist = ckan.site_title ckan.site_description

# Minimal number of seconds between two consequent change detection cycles.
# Basically, if you set 60 as a value, plugin will check config chnages once
# in a minute. In this way you can reduce number of DB queries and avoid
# unnecesarry checks when static files are served by CKAN.  Note, these
# queries are pretty fast(1ms), so you won't notice any significant
# performance improvement by setting any non-zero value here. And it means
# that any config overrides applied by API actions may not be visible
# immediately - you'll have to wait `charge_timeout` seconds in worst case.
# (optional, default: 0)
ckanext.editable_config.charge_timeout = 10

# Additional validators that are used when config option overrides are
# applied. Use this option if CKAN validators are not strict enough and you
# see the way to break the application by providing valid values for options.
# (optional, default: {})
ckanext.editable_config.additional_validators = {"ckan.site_title": "less_than_100 do_not_contain_exclamation_mark"}

# Remove "Config" tab from CKAN's Admin UI.
# (optional, default: false)
ckanext.editable_config.disable_admin_config_tab = True

# Replace "Config" tab from CKAN's Admin UI with basic form for editable
# config options.
# (optional, default: false)
ckanext.editable_config.replace_admin_config_tab = True

# Automatically convert any existing config overrides added via CKAN's Admin
# UI into editable config overrides.
# (optional, default: false)
ckanext.editable_config.convert_core_overrides = True

API操作

editable_config_last_check

上次变化检测周期的时间和日期。

返回

  • last_check(str):ISO 日期时间

示例

$ ckanapi action editable_config_last_check
{
  "last_check": "2023-07-26T08:24:48.013121"
}

editable_config_list

所有可编辑的配置选项。每个修改过的选项都包含包含覆盖详细信息的字典。

返回

  • 可编辑选项(dict[str, Any]):键:选项名称;值:包含当前值和可选修改详细信息的字典

示例

$ ckanapi action editable_config_list
{
  "ckan.site_about": {
    "option": null,
    "value": ""
  },
  "ckan.site_title": {
    "option": {
      "key": "ckan.site_title",
      "prev_value": "CKAN",
      "updated_at": "2023-07-25T21:44:52.434211",
      "value": "Updated title"
    },
    "value": "Updated title"
  }
}

editable_config_change

使用 options 映射对多个配置选项进行更改,该映射包含选项名称和选项值的配对。

返回

  • 更新的选项(dict[str, Any]):键:选项名称;值:包含更新选项详细信息的字典

示例

$ ckanapi action editable_config_change options='{"ckan.site_title": "Updated"}'
{
  "ckan.site_title": {
    "key": "ckan.site_title",
    "prev_value": "CKAN",
    "updated_at": "2023-07-26T08:28:04.988247",
    "value": "Updated"
  }
}

editable_config_revert

使用 keys 列表中的选项名称交换选项的当前值和上一个值。

返回

  • 更新的选项(dict[str, Any]):键:选项名称;值:包含撤销后选项详细信息的字典

示例

$ ckanapi action editable_config_revert keys=ckan.site_title
{
  "ckan.site_title": {
    "key": "ckan.site_title",
    "prev_value": "Updated",
    "updated_at": "2023-07-26T08:28:59.667917",
    "value": "CKAN"
  }
}

editable_config_reset

使用 keys 列表中的选项名称删除选项修改。

返回

  • 移除的选项(dict[str, Any]):键:选项名称;值:包含移除选项详细信息的字典

示例

$ ckanapi action editable_config_reset keys=ckan.site_title
{
  "ckan.site_title": {
    "key": "ckan.site_title",
    "prev_value": "Updated",
    "updated_at": "2023-07-26T08:28:59.667917",
    "value": "CKAN"
  }
}

editable_config_update

changerevertreset 操作合并为单个操作。接受 change 字典、revert 列表和 reset 列表。瑞士军刀,仅存在于批量操作中。

返回

  • 更新的选项(dict[str, Any]):键:选项名称;值:包含当前值和可选修改详细信息的字典

示例

$ ckanapi action editable_config_update \
$    change='{"ckan.site_about": "Updated via ckanapi"}' \
$    revert=ckan.site_title \
$    reset=ckan.site_custom_css
{
  "change": {
    "ckan.site_about": {
      "key": "ckan.site_about",
      "prev_value": "Updated via ckanapi",
      "updated_at": "2023-07-26T08:35:25.462359",
      "value": "Updated via ckanapi"
    }
  },
  "reset": {
    "ckan.site_custom_css": {
      "key": "ckan.site_custom_css",
      "prev_value": "",
      "updated_at": "2023-07-26T08:34:26.372150",
      "value": "body{color: red;}"
    }
  },
  "revert": {
    "ckan.site_title": {
      "key": "ckan.site_title",
      "prev_value": "CKAN",
      "updated_at": "2023-07-26T08:35:25.536150",
      "value": "Updated"
    }
  }
}

故障排除

更改 debug 或 beaker 相关的选项没有效果。

大多数选项在请求时由 CKAN 检查,但在运行时创建 WSGI 应用程序时使用 beaker 设置、调试标志和其他一些选项。目前无法在运行时更改这些选项。CKAN 核心需要在初始化工作流程中做出一些基本更改,并采取一些额外的安全措施,才能使其成为可能。

我们将在将来考虑这种可能性是否可行,但到目前为止,这个问题优先级较低,因此一些选项在运行时不应更改。

更改 ckan.plugins 与 actions/validators/helpers 一起工作,但不能与 middlewares 或 blueprints 一起工作。

它属于前面的问题。当应用程序初始化时使用了许多插件钩子,即:middlewares、blueprints、CLI 命令和翻译。在运行时没有可靠的方法来重置这些项。因此,只有不使用相应接口的插件才能通过可编辑的配置启用/禁用。

即使插件可以安全启用,也不建议这样做。通过可编辑的配置修改插件列表可能会导致插件顺序的改变,进而可能引发难以追踪的bug。最后,无法确定插件是否满足所有要求,因此在实际运行时启用/禁用插件可能会破坏整个应用程序。

一些选项(通过CKAN内置的AdminUI自定义的选项)无法更改。

此插件与CKAN内置的AdminUI配置表不兼容。当它们结合使用时,结果不可预测,因此建议使用内置的重置配置表,并且如果打算依赖此插件,则永远不要使用它。或者,至少不要使用插件API更改由AdminUI管理的配置选项。有一个选项会自动将来自原生AdminUI的任何修改保存为可编辑的配置选项,然后重置原生AdminUI:ckanext.editable_config.convert_core_overrides

保存的选项值会阻止进一步的修改/破坏应用程序。

您可以选择清除editable_config_option表中的所有自定义设置或仅清除“不良”设置。或者,您可以将ckanapi和环境变量CKANEXT_EDITABLE_CONFIG_DISABLE结合使用,以禁用editable_config效果并通过API移除覆盖项。

CKANEXT_EDITABLE_CONFIG_DISABLE=1 ckanapi action editable_config_revert keys=BAD_OPTION_NAME

许可

AGPL

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源分布

ckanext_editable_config-0.0.7.tar.gz (40.0 kB 查看哈希值)

上传时间

构建分布

ckanext_editable_config-0.0.7-py3-none-any.whl (43.6 kB 查看哈希值)

上传时间 Python 3

由以下机构支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面