gpt4 book ai didi

plone - 如何以编程方式访问 Plone 5 注册表中的资源列表

转载 作者:行者123 更新时间:2023-12-01 04:57:00 25 4
gpt4 key购买 nike

我正在为一些包编写测试,我需要验证是否正确注册了一些静态资源。

在 Plone 5 之前,我可以通过访问资源注册表来做到这一点,如下所示:

self.portal.portal_javascripts.getResourceIds()
self.portal.portal_css.getResourceIds()

如何在 Plone 5 中完成该任务?

最佳答案

看来这个问题的答案在于 tests for resources in Products.CMFPlone .

具体来说,在该文件中的测试用例中,有许多测试使用配置注册表来访问已注册的包和资源,如下所示:

from Products.CMFPlone.interfaces import IBundleRegistry
from Products.CMFPlone.interfaces import IResourceRegistry
from plone.registry.interfaces import IRegistry
from zope.component import getUtility

resources = getUtility(IRegistry).collectionOfInterface(
IResourceRegistry, prefix="plone.resources"
)
bundles = getUtility(IRegistry).collectionOfInterface(
IBundleRegistry, prefix="plone.bundles"
)

这些调用的返回值是类似字典的对象,它们包含指向已使用 registry.xml 注册的捆绑包或资源的配置注册表条目的指针。通用设置导入步骤。

因此,例如,如果您使用以下 xml 在您的产品中注册了一个捆绑包:
<records prefix="plone.bundles/my-product"
interface='Products.CMFPlone.interfaces.IBundleRegistry'>
<value key="resources">
<element>my-resource</element>
</value>
<value key="enabled">True</value>
<value key="jscompilation">++plone++static/my-compiled.js</value>
<value key="csscompilation">++plone++static/my-compiled.css</value>
<value key="last_compilation">2014-08-14 00:00:00</value>
</records>

然后在 bundles由上面的资源注册表返回,您将能够使用斜杠 ( 'my-product' ) 后面的捆绑包的“前缀”部分来查找捆绑包的注册表记录代理,如下所示:
my_bundle = bundles['my-product']

该记录将提供对捆绑软件定义接口(interface)的属性访问(详见 Products.CMFPlone.interfaces.resources.IBundleRegistry)。所以你应该能够检查它是否为编译的 js 或 css 设置了正确的值:
assert my_bundle.jscompilation == '++plone++static/my-compiled.js'
assert my_bundle.csscompilation == '++plone++static/my-compiled.css'

注册资源的记录将以相同的方式工作,一个类似字典的对象,其键对应于 registry.xml 中资源注册的“前缀”部分。斜线之后。在这种情况下返回的记录将支持 Products.CMFPlone.interfaces.resources.IResourceRegistry反而。但是您仍然可以使用属性访问来验证您期望的值是否已正确注册。

如果您有使用已弃用的 portal_javascript 注册的资源或 portal_css工具(使用 jsregistry.xmlcssregistry.xml 通用设置导入步骤),找到它们的关键是 Plone 现在会自动将这些资源包含在一个名为 plone-legacy 的特殊包中.由于捆绑包具有 resources提供该捆绑包中包含的资源列表的属性,您应该能够执行以下操作:
bundles = getUtility(IRegistry).collectionOfInterface(
IBundleRegistry, prefix="plone.bundles"
)
legacy_bundle = bundles['plone-legacy']
assert "my-oldskool.js" in legacy_bundle.resources

这方面的例子也可以在 tests for resources in Products.CMFPlone 中找到。 .特别是在 TestResourceNodeImporter测试用例。

关于plone - 如何以编程方式访问 Plone 5 注册表中的资源列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36454395/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com