简体   繁体   English

如何动态地将敏捷行为字段的模式更改为只读?

[英]How to dynamically change the mode of a Dexterity behavior field to read-only?

collective.googlenews is a package that implements, among other things, a behavior do add 2 new fields to a Dexterity-based content type. Collector.googlenews是一个程序包,它实现(除其他外)行为会向基于敏捷的内容类型添加2个新字段。

One of this fields (called standout_journalism ) have some special requirements that have proved complicated to implement: 其中一个字段(称为standout_journalism )具有一些特殊要求,这些要求已被证明很难实施:

  • any site can only have 7 items marked as standout in the last week 任何网站在过去一周只能有7个项目标记为杰出
  • to be counted, and item must be declared as standout (a boolean field) and must be published 要计数,并且必须将项目声明为突出(布尔字段)并且必须发布

I have to implement 2 different validations then: 然后,我必须实现2种不同的验证:

  • on the workflow transition, avoid publication of items marked as standout when we already have 7 在工作流程过渡上,当我们已经有7个时,避免发布标记为杰出的项目
  • on the edit form, avoid marking a published item as standout when we already have 7 在编辑表单上,当我们已经有7个时,避免将已发布的项目标记为突出

I was able to solve first part using workflow guards and a viewlet that shows a warning explaining why the transition is disabled, but I'm not sure about the second one. 我能够使用工作流防护和一个显示警告的视图来解决第一部分,该警告解释了为什么禁用过渡,但是我不确定第二部分。

I have created an invariant and the form returns an error if the user try to change the value of the field on a published item. 我创建了一个不变式,如果用户尝试更改已发布项目中字段的值,则表单将返回错误。

The problem is we think that behavior should be different: we want to avoid changing the value of that field (marking it as read-only) when marking an item as standout should not be allowed. 问题在于我们认为行为应该有所不同:当不允许将某项标记为突出时,我们希望避免更改该字段的值(将其标记为只读)。

Should I create my own widget? 我应该创建自己的小部件吗? Should I override the update method in the field? 我应该在该字段中覆盖update方法吗? Should I override the updateWidgets method in the form? 我应该覆盖表单中的updateWidgets方法吗?

On any case I have no idea how to proceed because the documentation is not clear about fields added as with behaviors. 在任何情况下,我都不知道如何进行操作,因为文档尚不清楚与行为一起添加的字段。

Any pointers to code examples are much appreciated. 任何指向代码示例的指针都将受到赞赏。

Workflow guards: 工作流防护:

Just FYI instead of workflow guards, we used to point to a other workflow transition script. 过去只是FYI而不是工作流防护,我们曾经指向其他工作流转换脚本。 In our case a BrowserView which has much more possibilities than the very limited content_status_modify Script (at least in Plone 4). 在我们的例子中, BrowserView比非常有限的content_status_modify脚本(至少在Plone 4中)具有更多的可能性。

It's further also easier to test and I had some issues with workflow guards... but I can't remember :-( (Probably I missed some payload/context/request). 它也更容易测试,并且我在工作流防护方面存在一些问题...但是我不记得了:-((可能我错过了一些有效负载/上下文/请求)。

You can set the Workflow transition script with the definition.xml of you workflow (Example): 您可以使用工作流程的definition.xml设置工作流程转换脚本(示例):

  <transition new_state="somestate" title="Some Title" transition_id="transition_id" after_script="" before_script="" trigger="USER">
    <action category="workflow" icon="" url="%(content_url)s/some-other-modify-status-script?transition=transition_di">Some Title</action>
    <guard>
      <guard-role>Contributor</guard-role>
      <guard-role>Manager</guard-role>
      <guard-role>Reviewer</guard-role>
      <guard-role>Site Administrator</guard-role>
    </guard>
  </transition>

This example changes the url from content_status_modify to some-other-modify-status-script 本示例将URL从content_status_modify更改为some-other-modify-status-script

Here is a full example of an custom status modify script 这是自定义状态修改脚本的完整示例

The important part happens here on line 21. It checks for some constrains and if it's valid the transition will happen, if not, the transition is not even initialised. 重要的部分发生在此处的第21行。它检查一些约束,如果有效,则将进行转换,否则,转换不会被初始化。

I'm sure in your case you can short it to just a few, readable lines. 我确定在您的情况下,您可以将其缩短为几行可读的代码。

The full example has a "Constraint System" (Adapter) behind, which allows to register constraints, that's why there are several more lines of code... 完整的示例后面有一个“约束系统”(适配器),它允许注册约束,这就是为什么还要多行代码的原因...

Validating the form: 验证表格:

  • Invariant: Try to use simple invariants as far as possible. 不变式:请尽量使用简单的不变式。 I assume you're using the catalog to get the necessary data. 我假设您正在使用目录来获取必要的数据。 You can get the portal with hooks.getSite() and further also the catalog.This way all the logic is located directly in your behavior. 您可以使用hooks.getSite()以及hooks.getSite()来获取门户。这样,所有逻辑都直接位于您的行为中。
  • directives form validator decorator --> https://pypi.python.org/pypi/plone.directives.form#validators 指令形式验证器修饰器-> https://pypi.python.org/pypi/plone.directives.form#validators

If this is, for some reason, not possible: You need to do the validation on form level. 如果由于某种原因,这是不可能的:您需要在表单级别进行验证。 This means customising the dexterity edit form. 这意味着定制灵活性编辑表单。

In there I would hack into the handleApply method and raise a WidgetActionExecutionError or ActionExecutionError . 在这里,我将侵入handleApply方法并引发WidgetActionExecutionErrorActionExecutionError Depends if you want a Error message on the widget or a general error message on the form. 取决于是要在小部件上显示错误消息还是在表单上显示常规错误消息。 Check http://garbas.github.io/plone-z3c.form-tutorial/validation.html 检查http://garbas.github.io/plone-z3c.form-tutorial/validation.html

EDIT: In addition to the form validation (very hight level) you can make sure, also low level modifications are validated In this case you may write your own field setter in your behavior. 编辑:除了可以进行表单验证(非常高级别)之外,还可以验证低级别的修改。在这种情况下,您可以在行为中编写自己的字段设置器。

Working example of custom getter/setter for a DX type. DX类型的自定义获取/设置器的工作示例。 FYI: I recently also found this issue in the plone.app.dx repo 仅供参考: 我最近还在plone.app.dx存储库中发现了此问题

This should be easily adapted into a behavior, since by default you can choose between a PropertyStorage and AnnotationStorage for the behavior data. 这应该很容易适应行为,因为默认情况下,您可以在行为数据的PropertyStorage和AnnotationStorage之间进行选择。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TFS 构建工作流程将文件夹/文件属性从只读更改为读/写? - TFS Build Workflow Change Folder/File Attributes From Read-Only to Read/Write? [8.0]在将状态设置为确认后,如何将窗体视图中的所有字段设置为只读 - [8.0]How to set all fields in form view to read-only after setting state to confirmed MySQL shell / client:只读访问权限,还是“安全历史记录”选项? - MySQL shell/client: Read-only access, or “safe history” option? JBPM 7.43:如何动态处理 ListBox 中选定的更改项? - JBPM 7.43: How to handle selected change item in ListBox dynamically? 如何更改Alfresco FTL(自由标记)控件的基字段? - How to change the base field of an Alfresco ftl (freemarker) control? 如何在 Jira 的工作流屏幕上设置必填字段 - How to make a field required only on a Workflow Screen in Jira 具有工作副本(迭代)和工作流程的敏捷内容类型 - Dexterity Content type with working copy (iterate) and workflow 如何在SharePoint中设置工作流以仅在更改特定字段时才起作用,而不是在每次编辑该项目时才起作用? - How to set up an workflow in SharePoint to only work when a specific field is changed and not everytime the item is edited? 在MS Dynamics Crm 2011中使用工作流创建电子邮件,并动态填写“收件人”字段 - Create Email with a workflow in MS Dynamics Crm 2011 and dynamically fill the “To” field 如何用标准字段值更新客户字段? - how to update custome field with standard field value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM