跳转到主要内容

用于管理、创建、导出和重建Plone中关系的辅助工具

项目描述

用于管理、创建、导出、检查和重建Plone中关系的辅助工具

要了解更多关于关系的信息,请阅读https://training.plone.org/5/mastering-plone/relations.html

功能

处理单个对象上的关系

便捷方法

relations(obj, attribute=None, as_dict=False)

获取相关对象。

unrestricted_relations(obj, attribute=None, as_dict=False)

在不进行权限检查的情况下获取相关对象。

backrelations(obj, attribute=None, as_dict=False)

获取与该对象有关联的对象。

unrestricted_backrelations(obj, attribute=None, as_dict=False)

在不进行权限检查的情况下获取与该对象有关联的对象。

relation(obj, attribute)

获取相关对象。仅当属性是对象上的关系选择字段名称时才有效。

unrestricted_relation(obj, attribute)

获取相关对象而不进行权限检查。参见关系

backrelation(obj, attribute)

获取相关联对象。当一项有与此类型的关系与obj关联时才有意义。一个例子是父 -> 子,只有一个父对象可以存在。

unrestricted_backrelation(obj, attribute)

获取相关联对象而不进行权限检查。参见backrelation

relapi.link_objects(source, target, relationship)

链接对象:使用指定的关系在两个对象之间创建关系。从参数relationship中,该方法将根据源对象的模式字段检查您想创建什么类型的关系(关系选择,关系列表)。此方法也适用于链接完整性关系和工作副本之间的关系。

示例:要使用默认行为plone.relateditems,请使用字段名称relatedItems作为关系:relapi.link_objects(obj, anotherobj, 'relatedItems')

获取各种关系的主要方法

relapi.get_relations(obj, attribute=None, backrels=False, restricted=True, as_dict=False)

获取特定内容对象的传入或传出关系的列表。

如果您传递了属性,则仅获取该类型的关联。这与源对象的字段名和关系值上的from_attribute相同。您也可以传递属性列表以获取特定类型的关联。

默认情况下,结果是对象的列表。如果您设置as_dict=True,则将返回一个字典,其中关系的名称作为键,对象的列表作为值。

一次性处理所有关系

特别是在迁移期间(例如,在Archetypes和Dexterity之间或从Python 2到3之间)需要一次性处理所有关系。例如,关系目录和intid目录可能包含对已损坏或已删除对象的引用。

首先导入API:from collective.relationhelpers import api as relapi

relapi.rebuild_relations()

使用与@@rebuild-relations表单相同的代码重建所有关系。

relapi.get_all_relations()

将所有关系作为字典列表获取。

relapi.export_relations()

将所有关系导出为构建目录中的json文件all_relations.json

relapi.store_relations()

导出所有关系并将它们存储在门户的注释中IAnnotations(portal)['ALL_REFERENCES']

relapi.cleanup_intids()

从IntId目录中删除所有关系值和对已损坏对象的引用。

relapi.get_relations_stats()

记录所有现有关系的信息

relapi.restore_relations(all_relations=None)

从门户上的注释或字典列表(例如,由export_relations创建的json文件恢复)重新创建关系。这对于所有类型的关系(包括默认字段“相关项目”)、关系列表或关系选择字段以及链接完整性关系和工作副本之间的关系都适用。

重建所有关系

有一个表单https://#:8080/Plone/@@rebuild-relations可以重建所有关系。

它从关系目录中导出所有有效关系,清除关系目录(和intid目录),并恢复所有有效关系。

检查关系

有一个控制面板https://#:8080/Plone/@@inspect-relations允许您检查网站上所有关系。

https://raw.githubusercontent.com/collective/collective.relationhelpers/master/docs/relationinfo.png

处理Archetypes关系

此包不支持Archetypes,但它可以帮助迁移从Archetypes到Dexterity的关系。以下是两个升级步骤。

第一步将所有关系(AT和DX)存储为门户上的注释)。如果您仍然有AT内容,请在Plone 4或5中运行该操作。

def store_relations(context=None):
    from plone.app.contenttypes.migration.utils import store_references
    portal = api.portal.get()
    store_references(portal)

第二个可以恢复它们。在您将所有内容迁移到Dexterity之后运行它。

# Map References used with of Archetypes (Plone 4) to the ones used in Plone 5 with Dexterity
RELATIONSHIP_FIELD_MAPPING = {
    'Working Copy Relation': 'iterate-working-copy',
    'relatesTo': 'relatedItems',
}

IGNORE = [
    'translationOf',  # LinguaPlone relation
]

def restore_relations(context=None):
    portal = api.portal.get()
    all_stored_relations = IAnnotations(portal)['ALL_REFERENCES']
    log.info('Loaded {0} relations to restore'.format(
        len(all_stored_relations))
    )
    all_fixed_relations = []
    for rel in all_stored_relations:
        if rel['relationship'] in ignore:
            continue
        # plone.app.contenttypes exports references with 'relationship' but relationshelpers
        # expects 'from_attribute' which is what zc.relation uses.
        # Also some relationships have changed their name
        rel['from_attribute'] = RELATIONSHIP_FIELD_MAPPING.get(rel['relationship'], rel['relationship'])
        all_fixed_relations.append(rel)
    all_fixed_relations = sorted(all_fixed_relations, key=itemgetter('from_uuid', 'from_attribute'))
    relapi.restore_relations(all_relations=all_fixed_relations)

安装

通过将其添加到您的buildout来安装collective.relationhelpers。

[buildout]

...

eggs =
    collective.relationhelpers

然后运行 bin/buildout

贡献

支持

如果您遇到问题,请创建一个工单。

许可证

该项目采用GPLv2许可证。

贡献者

变更日志

1.6 (2021-10-11)

  • 不要在Plone 6上注册控制面板视图,因为它们已经被添加到Plone核心。[fredvd]

1.5 (2021-04-19)

  • 处理relationchoice字段值为None而不是空列表的情况。[pbauer]

  • 修复检查未明确损坏但引发IntIdMissingError (#7) 的关系。[fulv]

1.4 (2021-02-19)

  • 对于源或目标缺失的关系,永远不会返回None。[pbauer]

  • 使用title_or_id允许缺少标题。[pbauer]

1.3 (2021-02-12)

  • 将视图重命名为与Plone 6中相同的名称。见 https://github.com/plone/Products.CMFPlone/issues/3231 [pbauer]

  • 添加一些测试。[pbauer]

  • 添加对损坏关系的支持。[pbauer]

  • 删除对fti的非必要访问。[pbauer]

  • 修复中现有关系的查询。[maurits]

1.2 (2021-01-13)

  • 添加/@@relationinfo视图来检查关系。[pbauer]

1.1 (2020-12-15)

  • 在restore_relations期间删除它们时记录重复项。[pbauer]

  • 解决z3c.relationfield.event.updateRelations的问题,该问题阻止了与标记接口注册的行为的关系注册。[pbauer]

  • 在恢复关系时添加进度记录器。[pbauer]

1.0 (2020-10-02)

  • 添加清除和重建intids的功能。[krissik]

  • 添加将Archetypes关系迁移到Dexterity的示例。[pbauer]

  • 延迟调用modified,直到最后,如果您使用许多关系列表,这将大大加快导入关系的速度。[pbauer]

  • 在清除重复项时保持关系的原始顺序。[pbauer]

1.0a2 (2020-09-15)

  • API更改:默认返回对象,可选返回relationname的字典 [pbauer]

  • API更改:默认检查视图权限 [pbauer]

  • API更改:添加便利方法relations,backrelations,unrestricted_relations和unrestricted_backrelations [pbauer]

  • API更改:为relationChoice添加便利方法,只返回一个对象,而不是列表 [pbauer]

  • API更改:将参数backref重命名为backrel [pbauer]

  • API更改:允许查询多个关系 [pbauer]

1.0a1 (2020-05-29)

  • 初始发布。 [pbauer]

项目详情


下载文件

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

源分布

collective.relationhelpers-1.6.tar.gz (169.1 kB 查看哈希)

上传时间

由以下支持

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