如何在敏捷类型 schema.datetime 中设置默认日期?

How to setup default date in Dexterity Type schema.datetime?

我正在使用 Python 2.7.x 到 Plone 4.3.10.

编写敏捷类型

我想为字段schema.Datetime设置一个默认日期。

import datetime
agoradatetime = datetime.datetime.now()

directives.read_permission(uploded_at="cmf.ManagePortal")
directives.write_permission(uploded_at="cmf.ManagePortal")
uploded_at = schema.Datetime(
    title=_(u"Data de Upload"),
    required=True,
    default=agoradatetime,
)

当我设置 default=agoradatetime 时出现此错误:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File     "/home/jafar/training/buildout/src/projetime.ged/src/projetime/ged/content/digitalfile.py", line 32, in <module>
    class IDigitalFile(model.Schema):
  File "/home/jafar/training/buildout/eggs/plone.supermodel-1.2.7-py2.7.egg/plone/supermodel/model.py", line 67, in __init__
    self._SchemaClass_finalize()
  File "/home/jafar/training/buildout/eggs/plone.supermodel-1.2.7-py2.7.egg/plone/supermodel/model.py", line 74, in _SchemaClass_finalize
adapter()
  File "/home/jafar/training/buildout/eggs/plone.supermodel-1.2.7-py2.7.egg/plone/supermodel/directives.py", line 84, in __call__
    for fieldName in self.check():
  File "/home/jafar/training/buildout/eggs/plone.supermodel-1.2.7-py2.7.egg/plone/supermodel/directives.py", line 76, in check
    if fieldName not in schema:
  File "/home/jafar/training/buildout/eggs/zope.interface-3.6.7-py2.7-linux-x86_64.egg/zope/interface/interface.py", line 564, in __contains__
    return self.get(name) is not None
  File "/home/jafar/training/buildout/eggs/zope.interface-3.6.7-py2.7-linux-x86_64.egg/zope/interface/interface.py", line 415, in get
    attr = attrs.get(name)
TypeError: Error when calling the metaclass bases
    unhashable type: 'list'

我认为这里的问题是 datetime.datetime.now() 的输出,就像它 datetime.datetime(2016, 9, 6, 14, 34, 51, 812271),我遵循这个提示 http://docs.plone.org/4/en/external/plone.app.dexterity/docs/advanced/defaults.html

我尝试使用:

from DateTime import DateTime
datenow = DateTime()

但出现错误 WrongType,见下文:

WrongType: (DateTime('2016/09/06 14:39:29.021494 GMT-3'), <type 'datetime.datetime'>, '')

[更新 2]

看新代码:

# -*- coding: utf-8 -*-
from plone.app.textfield import RichText
from plone.autoform import directives
from plone.namedfile import field as namedfile
from plone.supermodel.directives import fieldset
from plone.supermodel import model
from z3c.form.browser.radio import RadioFieldWidget
from zope import schema
from zope.schema.interfaces import IContextAwareDefaultFactory
from zope.schema.vocabulary import SimpleVocabulary
from zope.schema.vocabulary import SimpleTerm
from zope.interface import provider
from DateTime import DateTime
from datetime import datetime

from projetime.ged import _

TipoDeDocumentoVocabulary = SimpleVocabulary(
    [SimpleTerm(value=u'processo', title=_(u'Processos')),
     SimpleTerm(value=u'contratos', title=_(u'Contratos')),
     SimpleTerm(value=u'outros', title=_(u'Outros'))]
)

def nowDateTime():
    zope_dt = DateTime().asdatetime()
    python_dt = datetime.today()

    return python_dt

class IDigitalFile(model.Schema):
    """Dexterity-Schema
    """

    directives.widget(TipoDeDocumento=RadioFieldWidget)
    TipoDeDocumento = schema.Choice(
        title=_(u"Tipo de Documento"),
        vocabulary=TipoDeDocumentoVocabulary,
        required=True
    )

    titulo = schema.TextLine(
        title=_(u"Título"),
        required=True
    )

    codDoDocumento = schema.TextLine(
        title=_(u"Cód. do Documento"),
        required=False
    )

    CpfCnpj = schema.TextLine(
        title=_(u"CPF/CNPJ"),
        required=False
    )

    Assunto = schema.TextLine(
        title=_(u"Assunto"),
        required=True
        )

    Tipo = schema.TextLine(
        title=_(u"Tipo"),
        required=False
    )

    Descricao = schema.Text(
        title=_(u"Descrição"),
        required=True
    )

    fieldset('file', fields=['Arquivo'] )
    Arquivo = namedfile.NamedBlobFile(
        title=_(u"Arquivo Digitalizado"),
        required=True
    )

    directives.omitted(['Automatico', 'uploded_at'])
    directives.read_permission(Automatico="cmf.ManagePortal")
    directives.write_permission(Automatico="cmf.ManagePortal")
    directives.widget(Automatico=RadioFieldWidget)
    Automatico = schema.Bool(
        title=_(u"Upload via Script?"),
        required=True,
        default=True,
    )

    directives.read_permission(uploded_at="cmf.ManagePortal")
    directives.write_permission(uploded_at="cmf.ManagePortal")
    uploded_at = schema.Datetime(
        title=_(u"Data de Upload"),
        required=True,
        defaultFactory=nowDateTime,
    )

文档说:Python 日期时间:Here and Here

但是我得到了错误:

TypeError: Error when calling the metaclass bases
unhashable type: 'list'

所以我需要什么?当用户在 plone 中创建 Contenttype Dexterity 时,我需要使用 "date and time now".

构造字段 uploaded_at

[更新 3]

完整追溯

>>> from projetime.ged.content.digitalfile import IDigitalFile
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/jafar/training/buildout/src/projetime.ged/src/projetime/ged/content/digitalfile.py", line 37, in <module>
class IDigitalFile(model.Schema):
File "/home/jafar/training/buildout/eggs/plone.supermodel-1.2.7-py2.7.egg/plone/supermodel/model.py", line 67, in __init__
self._SchemaClass_finalize()
File "/home/jafar/training/buildout/eggs/plone.supermodel-1.2.7-py2.7.egg/plone/supermodel/model.py", line 74, in _SchemaClass_finalize
adapter()
File "/home/jafar/training/buildout/eggs/plone.supermodel-1.2.7-py2.7.egg/plone/supermodel/directives.py", line 84, in __call__
for fieldName in self.check():
File "/home/jafar/training/buildout/eggs/plone.supermodel-1.2.7-py2.7.egg/plone/supermodel/directives.py", line 76, in check
if fieldName not in schema:
File "/home/jafar/training/buildout/eggs/zope.interface-3.6.7-py2.7-linux-x86_64.egg/zope/interface/interface.py", line 564, in __contains__
return self.get(name) is not None
File "/home/jafar/training/buildout/eggs/zope.interface-3.6.7-py2.7-linux-x86_64.egg/zope/interface/interface.py", line 415, in get
attr = attrs.get(name)
TypeError: Error when calling the metaclass bases
unhashable type: 'list'

您使用的文档 link 清楚地使用了 defaultFactory 而不是 default,并且要使用的值必须是每次需要时都会调用以获取默认值的函数。 所以我的建议是也使用 defaultFactory

顺便说一句:我不知道为什么 default 不起作用,但即使它起作用了,结果也可能出乎意料:agoradatetime 是在 模块中计算的 级别,即在 Zope 启动时计算一次的日期时间。