[SOLVED] Programmatic configuration of collective.easyform mail action?

Inside a browser view I create an EasyForm form instance and I need to set some mailer configurations like

def create_mh(self):

    form = plone.api.content.create(type='EasyForm', ...)

    mailer = get_actions(form)['mailer']
    mailer.to_field = 'email'
    mailer.bcc_recipients = 'foo@bar.de'

    return 'DONE'

The form instance is correctly correctly however the mailer settings are not assigned.

Anything missing here?

We are doing the same but use the fields_model and actions_model which works perfectly.

        new = api.content.create(
            type='EasyForm',
            ...
            fields_model=CONTACT_FORM_MODEL,
            actions_model=CONTACT_FORM_ACTIONS.format(
                email=my_custom_email,
            ),
        )

I am aware that actions_model takes a supermodel like

<model xmlns:easyform="http://namespaces.plone.org/supermodel/easyform" xmlns:form="http://namespaces.plone.org/supermodel/form" xmlns:i18n="http://xml.zope.org/namespaces/i18n" xmlns:lingua="http://namespaces.plone.org/supermodel/lingua" xmlns:marshal="http://namespaces.plone.org/supermodel/marshal" xmlns:security="http://namespaces.plone.org/supermodel/security" xmlns:users="http://namespaces.plone.org/supermodel/users" xmlns="http://namespaces.plone.org/supermodel/schema">
  <schema>
    <field name="mailer" type="collective.easyform.actions.Mailer">
      <description>E-Mails Form Input</description>
      <replyto_field>replyto</replyto_field>
      <subject_field>topic</subject_field>
      <title>Mailer</title>
    </field>
    <field name="xxxxxx" type="collective.easyform.actions.SaveData">
      <description/>
      <title>xxx</title>
    </field>
  </schema>
</model>

This is used here to define a mailer and a save adapter but the supermodel itself does not specify a particular configuration.

In your example you wanted to change the to_field and bcc_recipients which can be done:

<model xmlns:easyform="http://namespaces.plone.org/supermodel/easyform" xmlns:form="http://namespaces.plone.org/supermodel/form" xmlns:i18n="http://xml.zope.org/namespaces/i18n" xmlns:lingua="http://namespaces.plone.org/supermodel/lingua" xmlns:marshal="http://namespaces.plone.org/supermodel/marshal" xmlns:security="http://namespaces.plone.org/supermodel/security" xmlns:users="http://namespaces.plone.org/supermodel/users" xmlns="http://namespaces.plone.org/supermodel/schema">
  <schema>
    <field name="mailer" type="collective.easyform.actions.Mailer">
      <bcc_recipients>foo@example.com&#13;
bar@example.com</bcc_recipients>
      <body_footer/>
      <body_post/>
      <body_pre/>
      <body_pt>&lt;html xmlns="http://www.w3.org/1999/xhtml"&#13;
      xmlns:tal="http://xml.zope.org/namespaces/tal"&gt;&#13;
  &lt;head&gt;&lt;title&gt;&lt;/title&gt;&lt;/head&gt;&#13;
  &lt;body&gt;&#13;
    &lt;p tal:content="structure body_pre | nothing" /&gt;&#13;
    &lt;dl&gt;&#13;
        &lt;tal:block repeat="field data | nothing"&gt;&#13;
            &lt;dt tal:content="python:fields[field]" /&gt;&#13;
            &lt;dd tal:content="structure python:widgets[field]" /&gt;&#13;
        &lt;/tal:block&gt;&#13;
    &lt;/dl&gt;&#13;
    &lt;p tal:content="structure body_post | nothing" /&gt;&#13;
    &lt;p tal:content="structure body_footer | nothing" /&gt;&#13;
  &lt;/body&gt;&#13;
&lt;/html&gt;</body_pt>
      <description>Kontaktformular</description>
      <msg_subject>Kontaktformular</msg_subject>
      <recipient_email>info@example.com</recipient_email>
      <recipient_name>Example Recipient</recipient_name>
      <replyto_field>replyto</replyto_field>
      <showFields/>
      <title>Mailer</title>
      <to_field>replyto</to_field>
    </field>
  </schema>
</model>
1 Like

Got it..but it is weird that you can only set the configuration using the XML model..the persistence problem on the Python is strange

Just a suspect because I do not know the internals of easyform, but maybe mayler is just a default value not bound to any object and thus modifying it will not trigger a write in the ZODB.
I have already seen this issue in dx schemas.
Let's say you have a field 'people' Choice(..., default=[]), whenever you do:

obj.people.append('johndoe') 

you will trigger no write in the ZODB is obj.people in not yet an attribute of obj and if you do not set obj._p_changed=1.

Hope that this can help.

I checked of course the internals and tried the ZODB magic...but it did not help...anyway...generating the actions_field XML model from a template solved the issue of our purposes.

Thanks, generating the XML from a template solved the problem.