我可以在 Plone portlet 上使用 z3c.form 而不是 zope.formlib 吗?

发布于 2024-10-19 18:28:41 字数 176 浏览 6 评论 0 原文

考虑到我有一个普通的 ZopeSkel plone3_portlet formlib 生成的包,我需要进行哪些修改?即:

  • 我应该从哪些类继承?
  • 我必须提供哪些挂钩?

我可以一直使用它,包括 Five.grok 和 plone.directives.form 吗?

What modifications do I need to make considering I have a vanilla ZopeSkel plone3_portlet formlib generated package? Ie:

  • From which classes should I inherit?
  • Which hooks must I provide?

Can I use it all the way down including five.grok and plone.directives.form?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

倾其所爱 2024-10-26 18:28:41

是的,你可以做到这一点,你需要的是添加/编辑表单...这是我为 Jarn 项目所做的,你可以使用 AddForm 和 EditForm ,就像使用它们的 formlib 等效项一样:

from Acquisition import aq_parent, aq_inner
from plone.app.portlets import PloneMessageFactory as _
from plone.app.portlets.browser.interfaces import IPortletAddForm
from plone.app.portlets.browser.interfaces import IPortletEditForm
from plone.app.portlets.interfaces import IPortletPermissionChecker
from z3c.form import button
from z3c.form import form
from zope.component import getMultiAdapter
from zope.interface import implements


class AddForm(form.AddForm):
    implements(IPortletAddForm)

    label = _(u"Configure portlet")

    def add(self, object):
        ob = self.context.add(object)
        self._finishedAdd = True
        return ob

    def __call__(self):
        IPortletPermissionChecker(aq_parent(aq_inner(self.context)))()
        return super(AddForm, self).__call__()

    def nextURL(self):
        addview = aq_parent(aq_inner(self.context))
        context = aq_parent(aq_inner(addview))
        url = str(getMultiAdapter((context, self.request),
                                  name=u"absolute_url"))
        return url + '/@@manage-portlets'

    @button.buttonAndHandler(_(u"label_save", default=u"Save"), name='add')
    def handleAdd(self, action):
        data, errors = self.extractData()
        if errors:
            self.status = self.formErrorsMessage
            return
        obj = self.createAndAdd(data)
        if obj is not None:
            # mark only as finished if we get the new object
            self._finishedAdd = True

    @button.buttonAndHandler(_(u"label_cancel", default=u"Cancel"),
                             name='cancel_add')
    def handleCancel(self, action):
        nextURL = self.nextURL()
        if nextURL:
            self.request.response.redirect(nextURL)
        return ''


class EditForm(form.EditForm):
    """An edit form for portlets.
    """

    implements(IPortletEditForm)

    label = _(u"Modify portlet")

    def __call__(self):
        IPortletPermissionChecker(aq_parent(aq_inner(self.context)))()
        return super(EditForm, self).__call__()

    def nextURL(self):
        editview = aq_parent(aq_inner(self.context))
        context = aq_parent(aq_inner(editview))
        url = str(getMultiAdapter((context, self.request),
                                  name=u"absolute_url"))
        return url + '/@@manage-portlets'

    @button.buttonAndHandler(_(u"label_save", default=u"Save"), name='apply')
    def handleSave(self, action):
        data, errors = self.extractData()
        if errors:
            self.status = self.formErrorsMessage
            return
        changes = self.applyChanges(data)
        if changes:
            self.status = "Changes saved"
        else:
            self.status = "No changes"

        nextURL = self.nextURL()
        if nextURL:
            self.request.response.redirect(self.nextURL())
        return ''

    @button.buttonAndHandler(_(u"label_cancel", default=u"Cancel"),
                             name='cancel_add')
    def handleCancel(self, action):
        nextURL = self.nextURL()
        if nextURL:
            self.request.response.redirect(nextURL)
        return ''

Yes, you can do that, what you need is the Add/Edit forms... Here's what I did it for a Jarn project, you can use AddForm and EditForm the same way you would use their formlib equvalent:

from Acquisition import aq_parent, aq_inner
from plone.app.portlets import PloneMessageFactory as _
from plone.app.portlets.browser.interfaces import IPortletAddForm
from plone.app.portlets.browser.interfaces import IPortletEditForm
from plone.app.portlets.interfaces import IPortletPermissionChecker
from z3c.form import button
from z3c.form import form
from zope.component import getMultiAdapter
from zope.interface import implements


class AddForm(form.AddForm):
    implements(IPortletAddForm)

    label = _(u"Configure portlet")

    def add(self, object):
        ob = self.context.add(object)
        self._finishedAdd = True
        return ob

    def __call__(self):
        IPortletPermissionChecker(aq_parent(aq_inner(self.context)))()
        return super(AddForm, self).__call__()

    def nextURL(self):
        addview = aq_parent(aq_inner(self.context))
        context = aq_parent(aq_inner(addview))
        url = str(getMultiAdapter((context, self.request),
                                  name=u"absolute_url"))
        return url + '/@@manage-portlets'

    @button.buttonAndHandler(_(u"label_save", default=u"Save"), name='add')
    def handleAdd(self, action):
        data, errors = self.extractData()
        if errors:
            self.status = self.formErrorsMessage
            return
        obj = self.createAndAdd(data)
        if obj is not None:
            # mark only as finished if we get the new object
            self._finishedAdd = True

    @button.buttonAndHandler(_(u"label_cancel", default=u"Cancel"),
                             name='cancel_add')
    def handleCancel(self, action):
        nextURL = self.nextURL()
        if nextURL:
            self.request.response.redirect(nextURL)
        return ''


class EditForm(form.EditForm):
    """An edit form for portlets.
    """

    implements(IPortletEditForm)

    label = _(u"Modify portlet")

    def __call__(self):
        IPortletPermissionChecker(aq_parent(aq_inner(self.context)))()
        return super(EditForm, self).__call__()

    def nextURL(self):
        editview = aq_parent(aq_inner(self.context))
        context = aq_parent(aq_inner(editview))
        url = str(getMultiAdapter((context, self.request),
                                  name=u"absolute_url"))
        return url + '/@@manage-portlets'

    @button.buttonAndHandler(_(u"label_save", default=u"Save"), name='apply')
    def handleSave(self, action):
        data, errors = self.extractData()
        if errors:
            self.status = self.formErrorsMessage
            return
        changes = self.applyChanges(data)
        if changes:
            self.status = "Changes saved"
        else:
            self.status = "No changes"

        nextURL = self.nextURL()
        if nextURL:
            self.request.response.redirect(self.nextURL())
        return ''

    @button.buttonAndHandler(_(u"label_cancel", default=u"Cancel"),
                             name='cancel_add')
    def handleCancel(self, action):
        nextURL = self.nextURL()
        if nextURL:
            self.request.response.redirect(nextURL)
        return ''
被你宠の有点坏 2024-10-26 18:28:41

开源 Collective.dancing.browser.portlets.channelsubscribe 模块具有以 z3c.form 编写的 Portlet 实现。

但这是一个巨大的混乱。我建议不要对 Plone portlet 做任何花哨的事情,因为它的复杂性会在你面前爆炸。

请参阅 http://dev。 plone.org/collective/browser/collective.dancing/trunk/collective/dancing/browser/portlets/channelsubscribe.pyhttp://pypi.python.org/pypi/collective.dancing

The open source collective.dancing.browser.portlets.channelsubscribe moudle has implementations of portlets written in z3c.form.

It's a huge mess though. I'd recommend against doing anything fancy with Plone portlets because of its complexity will blow up in your face big time.

See http://dev.plone.org/collective/browser/collective.dancing/trunk/collective/dancing/browser/portlets/channelsubscribe.py or http://pypi.python.org/pypi/collective.dancing

夜光 2024-10-26 18:28:41

这当然是可以做到的。我们已经在 4.0 Plone 项目中使用了它,其中一位同事使用 z3c.form.form.AddForm 创建了基本的 IPortletAddFormIPortletEditForm 实现,并且分别是z3c.form.form.EditForm基类。

请注意,这是 Plone 4.0,而不是 3.x,因此您的情况可能会有所不同。

该实现是对其 zope.formlib 原始版本的基本重新实现,使用简单的 buttonAndHandler 处理程序来处理“添加”(添加表单)、“保存”(编辑表单)和“取消”(两者)按钮。

我相信我们计划将基本表单实现贡献回 plone.app.portlets,我会询问他。

This certainly can be done. We already use this in a 4.0 Plone project, where a colleague created a base IPortletAddForm and IPortletEditForm implementations using z3c.form.form.AddForm and z3c.form.form.EditForm base classes respectively.

Note that this is Plone 4.0, not 3.x, so your mileage may vary.

The implementation is a basic reimplementation of their zope.formlib originals, with simple buttonAndHandler handlers to handle the Add (add form), Save (edit form) and Cancel (both) buttons.

I believe we have plans to contribute the base form implementations back to plone.app.portlets, I'll ask him about it.

你怎么敢 2024-10-26 18:28:41

如果像我一样,您在 2 年后发现了这个问题,那么了解一下可能会很方便:

  1. ggozad 的解决方案已集成到 plone.app.portlets

  2. 您仍然需要在他的解决方案(我的解决方案)之上编写 portlet
    发现很难解决)

  3. 我已经整理了我的工作代码的变体
    此处

  4. 除非您使用 Plone 5,否则您需要保留
    plone.app.portlets < 3.0

非常感谢这个包的作者(注意这是在 ggozad 的解决方案集成到 plone.app.portlets 之前编写的)

If like me you've found this question 2 years later, then it might be handy to know that:

  1. ggozad's solution has been integrated into plone.app.portlets

  2. You still need to write the portlet on top of his solution (which I
    found hard to work out)

  3. I've put together a variation of my working code
    here

  4. Unless you are using Plone 5 you will need to keep
    plone.app.portlets < 3.0

A lot of credit to the author of this package (note that this was written BEFORE ggozad's solution was integrated into plone.app.portlets)

多情出卖 2024-10-26 18:28:41

我认为理论上是可能的,是的,但我不确定是否有人尝试过。这可能是我们在某一时刻需要在 Plone 中做的事情,所以如果你能成功让它发挥作用那就太好了。

我首先查看现有的 portlet 表单基类的作用,并尝试在 z3c.form 中模拟它。我现在也可能会在没有 plone.directives.form 和 plone.autoform 的情况下开始,因为这可能会让你有点困惑。最好稍后再添加。

我怀疑,主要的事情是为新表单注册一个新的默认模板,然后根据 plone.app.portlets 的基本表单为实际的“添加”和“编辑”操作添加一些挂钩。

I think it's theoretically possible, yes, but I'm not sure anyone's tried it. It's probably something we'll need to do in Plone at one point, so it'd be great if you managed to make it work.

I'd start by looking at what the existing portlet form base classes do and try to emulate that in z3c.form. I'd also probably start without plone.directives.form and plone.autoform for now, as that will probably confuse you a bit to start with. Better to add those later.

The main thing, I suspect, will be to register a new default template for the new forms, and then add some hooks for the actual "add" and "edit" operations as per plone.app.portlets's base forms.

獨角戲 2024-10-26 18:28:41

我相信 David Glick 已经通过 Carousel 实现了这一点。他的文档指向已知-很好的一套,对我有用。

I believe that David Glick has accomplished this with Carousel. His documentation points to a known-good set that's worked for me.

清引 2024-10-26 18:28:41

我知道这是一个不完整的答案,但我相信它会为您指明正确的方向。 plonezohointegration 产品使用 z3cforms 作为其 portlet,您可以看看它是如何做到的。

有关于如何完成的文档 在 plone 社区开发者文档中

I know this is an incomplete answer, but I believe it will point you in the right direction. The plonezohointegration product uses z3cforms for its portlets you can look at how the did it.

There's documentation on how it is done in the plone community developer documentation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文