通过JSON创建Plone内容
项目描述
kitconcept.contentcreator
此包负责通过plone.restapi serializers/creators自动化内容创建。
最初基于由Johannes Raggam(@thet)编写的collective.contentcreator
,并在此基础上进行发展和改进。
使用方法
基本
它允许您在策略包中有一个结构,例如
|-content_creator
|- content.json
|- siteroot.json
|- de.mysection.json
|- ...
|- images
|-content_images
使用这些名称(对于文件和文件夹)作为合理的默认值。这是推荐的方法,尽管您可以指定自定义JSON文件的运行者(见下文)。
并使用运行者从content.json
创建树状内容,并使用独立的json文件逐个对象地创建。
{file}images
文件夹被列入黑名单,以支持图像文件夹位于创建器文件夹内。
在您的setuphandlers.py中,您需要
from kitconcept.contentcreator.creator import content_creator_from_folder
content_creator_from_folder()
content_creator_from_folder
方法有以下签名
def content_creator_from_folder(
folder_name=os.path.join(os.path.dirname(__file__), "content_creator"),
base_image_path=os.path.join(os.path.dirname(__file__), "content_images"),
default_lang=None,
default_wf_state=None,
ignore_wf_types=["Image", "File"],
logger=logger,
temp_enable_content_types=[],
custom_order=[],
do_not_edit_if_modified_after=None,
exclude=[],
):
如果创建(或编辑)时任何对象出错,创建器将退出(引发异常)。有一些环境变量可以控制此行为:CREATOR_DEBUG
和CREATOR_CONTINUE_ON_ERROR
。
可以使用exclude
kwargs排除元素(id,没有.json后缀)。
您可以通过提供元素的修改日期,如果该日期在do_not_edit_if_modified_after
kwargs指定的日期之后,来控制是否为给定元素编辑。
给定单个文件的创建器运行者
给定一个包含要创建的对象数组的JSON文件,此运行者将此数组内容(应具有plone.restapi语法兼容的结构)转换为内容。您可以使用方法:load_json
加载它。
from kitconcept.contentcreator.creator import load_json
content_structure = load_json('testcontent/content.json', __file__)
然后您可以通过方法create_item_runner
调用运行者
from kitconcept.contentcreator.creator import create_item_runner
create_item_runner(
api.portal.get(),
content_structure,
default_lang='en',
default_wf_state='published'
)
给定包含多个文件的文件夹的创建器运行者
每个文件应包含单个p.restapi JSON兼容对象(非数组,不能包含其他对象)。它从文件名中获取树层次结构的放置位置和对象ID(例如,de.beispiele.bildergroessen.json)
从外部模块/包设置运行者
或者,您可以在其他包中创建自定义内容创建器,并通过自定义适配器同时调用它们
from kitconcept.contentcreator.interfaces import ICreateTestContent
for name, provider in getAdapters((api.portal.get(), ), ICreateTestContent):
provider()
其他包中的声明应该是这样的
@implementer(ICreateTestContent)
@adapter(IPloneSiteRoot)
class CreatePFGContent(object):
"""Adapter to create PFG initial content."""
def __init__(self, context):
self.context = context
def __call__(self):
content_structure = load_json('testcontent/content.json', __file__)
create_item_runner(
api.portal.get(),
content_structure,
default_lang='en',
default_wf_state='published',
ignore_wf_types=[
'FormBooleanField',
'FormDateField',
'FormFileField',
'FormFixedPointField',
'FormIntegerField',
'FormLabelField',
'FormLinesField',
'FormPasswordField',
],
)
另一个常见用法是从文件夹调用
from kitconcept.contentcreator.creator import content_creator_from_folder
content_creator_from_folder(
folder_name=os.path.join(os.path.dirname(__file__), "content_creator"),
base_image_path=os.path.join(os.path.dirname(__file__), "images"),
default_lang='en',
default_wf_state='published',
ignore_wf_types=[
'FormBooleanField',
'FormDateField',
'FormFileField',
'FormFixedPointField',
'FormIntegerField',
'FormLabelField',
'FormLinesField',
'FormPasswordField',
],
logger=logger,
temp_enable_content_types=[],
custom_order=[
'object-id-2.json',
'object-id-3.json',
'object-id-1.json',
],
)
图像和文件
对于图像的创建,您可以使用以下序列化映射,包含文件数据和一些附加元数据,使用plone.restapi方法
- data - 文件的base64编码内容
- encoding - 您用于编码数据所使用的编码,通常为
base64
- content-type - 文件的MIME类型
- filename - 文件名,包括扩展名
{
"...": "",
"@type": "File",
"title": "My file",
"file": {
"data": "TG9yZW0gSXBzdW0uCg==",
"encoding": "base64",
"filename": "lorem.txt",
"content-type": "text/plain"
}
}
或者,您可以为图像提供一个额外的属性set_dummy_image
,它是一个包含(图像)字段名的数组,将在要创建的内容类型中指定的字段创建一个占位符图像
{
"id": "an-image",
"@type": "Image",
"title": "Test Image",
"set_dummy_image": ["image"]
}
也支持已弃用的语法形式(它将在image
字段中创建图像):
{
"id": "an-image",
"@type": "Image",
"title": "Test Image",
"set_dummy_image": true
}
您还可以使用在set_local_image
JSON属性中的dict指定一个真实图像,其中包含字段名和真实图像的文件名
{
"id": "another-image",
"@type": "Image",
"title": "Another Test Image",
"set_local_image": {"image": "image.png"}
}
同样,也支持已弃用的形式(它将在image
字段中创建图像)
{
"id": "another-image",
"@type": "Image",
"title": "Another Test Image",
"set_local_image": "image.png"
}
默认情况下,将立即生成图像缩放。要禁用此操作,请设置环境变量CREATOR_SKIP_SCALES
。
相同的语法对于文件也是有效的
{
"id": "an-file",
"@type": "File",
"title": "Test File",
"set_dummy_file": ["file"]
}
也支持已弃用的形式(它将在file
字段中创建文件)
{
"id": "an-file",
"@type": "File",
"title": "Test File",
"set_dummy_file": true
}
您还可以使用在set_local_file
JSON属性中的dict指定一个真实文件,其中包含字段名和真实文件的文件名
{
"id": "another-file",
"@type": "File",
"title": "Another Test File",
"set_local_file": {"file": "file.png"}
}
也支持已弃用的形式(它将在file
字段中创建文件)
{
"id": "another-file",
"@type": "File",
"title": "Another Test File",
"set_local_file": "file.png"
}
对于所有指定的本地图像和文件,您可以在create_item_runner
中指定图像的base_path
create_item_runner(
api.portal.get(),
content_structure,
default_lang='en',
default_wf_state='published',
base_image_path=__file__
)
翻译
如果您正在使用plone.app.multilingual,并从文件夹创建项目,您可以使用以下格式的translations.csv
链接翻译:
canonical,translation
/de/path/to/canonical,/en/path/to/translation
开发
需求
- Python 3
- venv
设置
make
运行静态代码分析
make lint
运行单元/集成测试
make test
贡献者
- kitconcept GmbH, info@kitconcept.com
变更日志
5.1.0 (2022-09-05)
-
将README、CHANGES、CONTRIBUTOR转换为markdown。@ericof
-
使用plone/code-analysis-action@v1进行代码分析 @ericof
-
直接在根目录中更改blocks和blocks_layout,适用于Plone6及以上版本 @steffenri
-
改进对Plone版本的检查 @steffenri
5.0.5 (2022-07-28)
-
content_creator_from_folder
现在接受一个可选的列表types_order
,以优先加载特定的内容类型 @davisagli -
改进日志记录,包括内容类型。@davisagli
-
重构以内部使用
pathlib
。@davisagli
5.0.4 (2022-07-26)
-
添加使用
translations.csv
链接翻译的功能 @davisagli -
添加通过设置
CREATOR_SKIP_SCALES
环境变量来禁用立即创建图像缩略图的功能 @davisagli -
如果JSON中的
id
与文件名中的id不匹配,则优先使用JSON中的id。@davisagli
5.0.3 (2022-06-23)
- 在导入内容项时不要吞食ValueError。@davisagli
5.0.2 (2022-04-21)
- 当反序列化字典不包含
language
键时,修复语言。这被之前删除的通用reindexObject
所隐藏。@sneridagh
5.0.1 (2022-04-20)
-
从完成对象中删除"始终重新索引",因为那是在两次发生(在创建和编辑时)@sneridagh
-
修复编辑内容问题通知事件(内容在反序列化时没有被重新索引)@sneridagh
5.0.0 (2022-04-13)
-
包含kitconcept.api作为依赖项 @ericof
-
停止支持Python 3.7 @ericof
-
删除buildout @ericof
-
更新Github Actions @ericof
4.0.0 (2022-01-21)
破坏性更改
-
移除对Archetypes和Python 2的支持 @sneridagh
-
重构创建器以默认破坏。添加了一个逃生舱,以便在不破坏的情况下,通过
CREATOR_DEBUG
和CREATOR_CONTINUE_ON_ERROR
环境变量在失败时打开调试会话。@sneridagh
3.3.0 (2022-01-11)
特性
- 添加从文件列表中排除文件的能力(content_creator_from_folder)@sneridagh
内部
- 移动到最新的5.2.6版本 @sneridagh
3.2.0 (2021-12-03)
- 如果内容ID缺失,则猜测内容ID,考虑到内容可能已经存在。@sneridagh
3.1.0 (2021-12-03)
- 使用改进的日志基础设施 @sneridagh
3.0.2 (2021-11-28)
-
在生成图像缩略图时使用调试日志级别 @timo
-
使日志消息更一致 @timo
-
不要在信息消息中使用颜色 @timo
3.0.1 (2021-11-11)
-
为Python 3.8、3.9和成熟度添加分类器到setup.py @timo
-
如果内容的
review_state
是published
,则设置有效日期 @sneridagh
3.0.0 (2021-11-10)
-
显式包含依赖项(支持pip安装)@ericof
-
在Github actions中使用plone/setup-plone@v1.0.0 @ericof
-
要求plone.restapi 7.5.0或更高版本(volto-slate blocks:resolveuid链接、转换器支持)@ericof
2.1.0 (2021-10-13)
- 添加新的
do_not_edit_if_modified_after
选项。允许在给定日期小于对象修改日期时不要编辑。@sneridagh
2.0.0 (2021-07-09)
破坏性更改
- 使用Slate作为默认文本块 @sneridagh
1.2.1 (2021-07-09)
错误修复
- 添加创建的内容刷新,以更新序列化的块中的resolveuid信息 @sneridagh
内部
- 删除一些未使用的导入[timo]
- 在CI中添加flake8检查 [timo]
1.2.0 (2021-04-08)
- 在创建内容文件夹内黑名单
images
文件夹 @sneridagh - 改进错误检测和报告 @sneridagh
1.1.0 (2021-01-26)
- 如果字段不存在,则改进内容语言检测 @sneridagh
- 修复并改进现有内容编辑中的语言推断 @sneridagh
1.0.6 (2020-05-08)
-
在pypi上发布包。@timo
-
添加从文件夹创建内容的功能。@sneridagh
1.0.5 (2019-11-22)
- 改进create_item_runner中的错误报告 @timo
1.0.4 (2019-11-21)
- 重新发布。@timo
1.0.3 (2019-05-06)
- 重新发布。@sneridagh
1.0.2 (2019-05-06)
- 尚未更改任何内容。
1.0.1(未发布)
-
移植到Python 3 @sneridagh
-
文档。@sneridagh
1.0.0 (2019-03-26)
- 首次发布。@kitconcept
项目详情
下载文件
下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。
源代码分发
构建分发
kitconcept.contentcreator-5.1.0.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 061bd5938813b88ec193754c53bff3d9cf69c786fa4387bbbcf9980d3139c287 |
|
MD5 | e4aa736f6cbc9d03243d82ec45fbe280 |
|
BLAKE2b-256 | b4df560cf171ba4508ba8392bd8db812d3e0dc3fc5e1d21d0b3944152116c070 |
kitconcept.contentcreator-5.1.0-py3-none-any.whl的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | c6d03cdfca3d0d7a48cb0410b25e13840a9f27701ee155328612cffcd8bbbf99 |
|
MD5 | d9ade32a922b3f84d886eaf50c3e8b42 |
|
BLAKE2b-256 | 9b00adf3aec563749d370554d30d31d1c126f40978842bae82dce5a0109b2f1b |