如何使用Dexterity类型的属性作为url部分

时间:2012-12-23 19:29:31

标签: plone dexterity

我正在使用Dexterity编写类似这样的内容类型:

class IArticle(form.Schema):

    title = schema.TextLine(
        title=_(u"Name"),
    )

    code = schema.TextLine(
        title=_(u"Code"),
    )

如果某个用户创建了一个新文章,将“foo”设置为标题,将“bar”设置为代码,则标题将为“foo”,文章URL将为“... / foo”。如何才能将内容网址设为“... / bar”?

2 个答案:

答案 0 :(得分:5)

您要做的是影响name chooser,您可以使用custom behavior为您的界面做到这一点。

INameForTitle接口进行子类化是最简单的:

from plone.app.content.interfaces import INameFromTitle
from zope import interface, component

from ..types.interfaces import IArticle


class INameFromCode(INameFromTitle):
    pass

class ArticleCodeAsTitle(object):
    component.adapts(IArticle)
    interface.implements(INameFromCode)

    def __init__(self, context):
        self.context = context

    @property
    def title(self):
        return self.context.code

默认名称选择器尝试将新添加的对象调整到INameForTitle接口,如果成功,将使用.title属性为对象构建新名称。通过将该接口的子类实现为IArticle对象的适配器,您可以替换.code字段的标题,从而确保将其用于新名称。

将此注册为:

<configure
    xmlns="http://namespaces.zope.org/zope"
    xmlns:plone="http://namespaces.plone.org/plone"
    i18n_domain="your.i18n.domain"
    >

<plone:behavior
    title="ArticleCode"
    description="Use .code as the title when choosing a new object name"
    provides=".articlecode.INameFromCode"
    factory=".articlecode.ArticleCodeAsTitle"
    for="..types.interfaces.IArticle"
    />

</configure>

并将此行为添加到Article行为的INameFromTitle类型定义,而不是

答案 1 :(得分:0)

相关问题