[Solved] [collective.easyform] providing the current datetime as default for a date/datetime field

What is the recommended way to pass the current date/datetime as TALES expression to a date/datetime field inside an EasyForm? In PFG, you could use python: DateTime(). Is there something pre-defined for collective.easyform?

Or let me be more precise...this field configuration causes an error because the converter assumes that the underlying value is of type datetime, not DateTime.

    <field name="datum" type="zope.schema.Datetime" easyform:TDefault="python: DateTime()" easyform:serverSide="False" easyform:THidden="False">
      <description/>
      <title>Datum</title>
    </field>
Traceback (innermost last):
  Module ZPublisher.WSGIPublisher, line 181, in transaction_pubevents
  Module ZPublisher.WSGIPublisher, line 390, in publish_module
  Module ZPublisher.WSGIPublisher, line 284, in publish
  Module ZPublisher.mapply, line 98, in mapply
  Module ZPublisher.WSGIPublisher, line 68, in call_object
  Module plone.z3cform.layout, line 61, in __call__
  Module plone.z3cform.layout, line 45, in update
  Module collective.easyform.browser.view, line 336, in update
  Module plone.z3cform.fieldsets.extensible, line 62, in update
  Module plone.z3cform.patch, line 31, in GroupForm_update
  Module z3c.form.group, line 133, in update
  Module z3c.form.form, line 140, in updateWidgets
  Module z3c.form.field, line 278, in update
  Module plone.app.z3cform.widgets.base, line 141, in update
  Module plone.app.z3cform.widgets.base, line 125, in update
  Module z3c.form.browser.widget, line 162, in update
  Module Products.CMFPlone.patches.z3c_form, line 48, in _wrapped
  Module z3c.form.widget, line 131, in update
  Module plone.app.z3cform.converters, line 86, in toWidgetValue
TypeError: unsupported format string passed to method.__format__

The underlying converter does not seem to handle DateTime:


@adapter(IDatetime, IDatetimeWidget)
class DatetimeWidgetConverter(BaseDataConverter):
    """Data converter for datetime fields."""

    def toWidgetValue(self, value):
        """Converts from field value to widget.

        :param value: Field value.
        :type value: datetime

        :returns: Datetime in format `Y-m-d H:M`
        :rtype: string
        """
        if value is self.field.missing_value:
            return ""
        return (
            f"{value.year:}-{value.month:02}-{value.day:02}T"
            f"{value.hour:02}:{value.minute:02}"
        )

did you try datetime.datetime()?

No and it is not valid in TALES expressions (security).

You're right.<defaultFactory> is an option?

True but not helpful :slight_smile:
This setting is not exposed through the fields editor and for new forms, the user must be able to configure the default :upside_down_face:

Maybe that is a hint to write a simple function that returns the expected value and can be safely called from a TALES expression?

my_package/date_defaults/today ?

There is the DateTime().asdatetime() method ... maybe this helps here?

4 Likes

It does :heart_eyes:

1 Like