首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Plone 5: Dexterity内容类型不提供IContentType

Plone 5: Dexterity内容类型不提供IContentType
EN

Stack Overflow用户
提问于 2017-03-27 03:55:21
回答 2查看 133关注 0票数 2

我创建了一个Dexterity内容类型并定义了一个简单的模式:

代码语言:javascript
复制
....
....
class IMyType(model.Schema):
    myField = schema.TextLine(
            title=_(u"My Field:"),
        )
....
....

在Plone 4.3下,Dexterity内容类型提供了IContentType。然而,在Plone 5.0.6下,Dexterity内容类型似乎不提供IContentType,因此queryContentType(IMyType)返回'None‘。

另外:

代码语言:javascript
复制
IContentType.providedBy(IMyType)

返回'False‘。

对默认内容类型尝试相同的方法也会产生相同的负面结果。

显然,对于我的自定义内容类型,这可以按如下方式解决:

代码语言:javascript
复制
....
from zope.interface import alsoProvides
....
class IMyType(model.Schema):
    myField = schema.TextLine(
            title=_(u"My Field:"),
        )

alsoProvides(IMyType, IContentType)

queryContentType(IMyType)现在返回预期的架构,IContentType.providedBy(IMyType)返回'True‘。

但是,我希望所有Dexterity内容类型都能自动提供IContentType。我是不是期望太高了,或者这是plone.dexterity和/或Plone 5.x中的一个错误?

EN

回答 2

Stack Overflow用户

发布于 2017-03-28 02:48:05

plone.dexterity 2.0+在zope.app.content上只有一个“软依赖项”,并且您的构建版本没有安装它。如果您将zope.app.content添加到setup.py install_requires并在构建中固定一个版本(3.5.1),则您的附加组件可以在内容类型接口上提供所需的接口。如果这样做,所有内容类型模式/接口类都将提供IContentType。

票数 1
EN

Stack Overflow用户

发布于 2017-06-01 12:32:56

很抱歉我回复得太晚了。之所以需要这样做,是因为我已经创建了一个CSV/PDF导出实用程序,用户可以根据自己的需求导出对象。因此,我需要获取内容类型的模式,以创建CSV文件的标题行并填充表。在我的Plone 4.3站点中,我通过以下方式做到了这一点:

代码语言:javascript
复制
from zope.app.content import queryContentType
from zope.schema import getFieldsInOrder
....

schema = queryContentType(obj)
fields = getFieldNamesInOrder(schema)
# Write header
headerrow=[]
    for field in fields:
        headerrow.append(field)
    writer.writerow(headerrow,)
        fields = getFieldNamesInOrder(schema)

    # Write body
    for item in results:
        obj = item.getObject()
        newrow=[]
        for field in fields:
            value = getattr(obj, field)

            ## some conditioning stuff here....
            if type(value) not in (str, bool, list, unicode, datetime.date, datetime.datetime):
                value = ''
            if type(value) is datetime.date:
                value = str(value)
            if type(value) is datetime.datetime:
                value = str(value)
                value = value[:-7]
            if type(value) is bool:
                value = str(value)
            if type(value) is list:
                newvalue = ''
                for item in value:
                    newvalue = newvalue + str(item) + ';'
                value = newvalue
            if value is None:
                value = ''

            valuestring = value.encode('utf-8')
            newrow.append(valuestring)
        writer.writerow(newrow,)
....

只有当对象提供IContentType,因此需要zope.app.content时,这才能起作用。请参阅https://docs.plone.org/external/plone.app.dexterity/docs/reference/manipulating-content-objects.html#object-introspection

更新文档可能会有所帮助,这表明zope.app.content不再是现成的。如果有更新的方法来获得不需要zope.app.content的对象模式,请让我知道!

干杯,

埃里克

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43033779

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档