如何在Plone中更改由Dexterity服务的对象的URL [英] How can I change the URL of an object serverd by Dexterity in Plone

查看:110
本文介绍了如何在Plone中更改由Dexterity服务的对象的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基于Plone Dexterity定义了一些内容类型,我希望任何内容类型都有自己的ID.因此,我使用了zope.schema.Id.

I defined some content types based on Plone Dexterity, I want any content type has his own ID. So I used the zope.schema.Id .

class IArticle(form.Schema, IImageScaleTraversable):
    """
    Article
    """

    form.model("models/article.xml")

    id = schema.Id(
        title=_(u"Identifier"),
        description=_(u"The unique identifier of object."),
        required=False
    )

当我创建一个新的文章对象时,它可以正常工作,但是当我想要修改一个现有文章的ID时,它总是向我显示错误:

When I create a new article object, it works fine, but when I want to modify the Id of an existed article, It always show me error:

 {'args': (<MultiContentTreeWidget 'form.widgets.IRelatedItems.relatedItems'>,),
 'context': <Article at /dev/articles/this-is-another-articles>,
 'default': <object object at 0x100266b20>,
 'loop': {},
 'nothing': None,
 'options': {},
 'repeat': {},
 'request': <HTTPRequest, URL=http://localhost:8083/dev/articles/this-is-another-article/@@edit>,
 'template': <zope.browserpage.viewpagetemplatefile.ViewPageTemplateFile object at 0x10588ecd0>,
 'view': <MultiContentTreeWidget 'form.widgets.IRelatedItems.relatedItems'>,
 'views': <zope.browserpage.viewpagetemplatefile.ViewMapper object at 0x10b309f50>}
 .......
 .......
 .......
Module zope.tales.expressions, line 217, in __call__
Module zope.tales.expressions, line 211, in _eval
Module plone.formwidget.contenttree.widget, line 147, in render_tree
Module plone.app.layout.navigation.navtree, line 186, in buildFolderTree
Module Products.CMFPlone.CatalogTool, line 427, in searchResults
Module Products.ZCatalog.ZCatalog, line 604, in searchResults
Module Products.ZCatalog.Catalog, line 907, in searchResults
Module Products.ZCatalog.Catalog, line 656, in search
Module Products.ZCatalog.Catalog, line 676, in sortResults
Module plone.app.folder.nogopip, line 104, in documentToKeyMap
Module plone.folder.ordered, line 102, in getObjectPosition
Module plone.folder.default, line 130, in getObjectPosition
ValueError: No object with id "this-is-another-articles" exists.

推荐答案

我不认为这是股票编辑表单中支持的用例.您可以从包含项目的文件夹的文件夹内容选项卡中重命名项目.您是否要通过修改表单执行此操作:

I don't think this is a use-case supported in the stock Edit form. You can rename items from the folder contents tab of the folder containing the item. Should you want to do this from the edit form:

  • 您可能需要使用自定义编辑表单(将现有表单归类);
  • 将您的id字段命名为'id'以外的其他名称,也许是'target_id'或'shortname'
  • 具有表单的__call __()方法更新/保存表单,只有成功后,才能在容器中重命名该项目.
  • 在编辑后触发名称更改的
  • __ call __()应该重定向到新项目的URL.
  • You may need to use a custom edit form (subclass the existing form);
  • name your id field something other than 'id', perhaps 'target_id' or 'shortname'
  • have the __call__() method of the form update / save the form, and only on success rename the item within its container.
  • __call__() should redirect to the new item's URL after edit that triggers a name change.

也许是这样(完全未经测试):

Maybe something like (completely untested):

from plone.dexterity.browser.edit import DefaultEditForm as BaseForm
from Products.statusmessages.interfaces import IStatusMessages

class RenameEnabledEditForm(BaseForm):
    def __call__(self, *args, **kwargs):
         self.update(*args, **kwargs)
         data, errors = self.extractData()
         existing, newid = self.context.getId(), data.get('target_id')
         if not errors and existing != newid:
             parent_folder = self.context.__parent__
             if newid in parent_folder.objectIds():
                 raise RuntimeError('Duplicate object id')  # collision!
             parent_folder.manage_renameObject(existing, newid)
             newurl = parent_folder[newid].absolute_url()
             IStatusMessages(self.context).addStatusMessage(
                 u'Renamed item from "%s" to "%s" in folder' % (existing, newid),
                 type='info',
                 )
             self.request.response.redirect(newurl)
         else:
             return self.index(*args, **kwargs)

这篇关于如何在Plone中更改由Dexterity服务的对象的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆