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

Dan*_*dez 1 plone dexterity

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

class IArticle(form.Schema):

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

    code = schema.TextLine(
        title=_(u"Code"),
    )
Run Code Online (Sandbox Code Playgroud)

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

Mar*_*ers 5

您想要做的是影响名称选择器,您可以使用您的界面的自定义行为.

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
Run Code Online (Sandbox Code Playgroud)

默认名称选择器尝试将新添加的对象调整到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>
Run Code Online (Sandbox Code Playgroud)

并添加此行为的Article类型定义代替INameFromTitle行为.