引入了实验性的模式。Object支持plone.autoform和plone.app.z3cform。
项目描述
schema.Object简化了
此软件包引入了对对象字段(zope.schema.Object)在z3c.form和Plone中的实验性概念性支持,用于需要将基于模式的分层数据保存到对象中的用例。
我无法说为什么有人会想这样做(而不是将数据映射到容器和项),但希望这个软件包提供如何使用plone.autoform和Plone的KSS-validation来使对象字段工作的示例。
此软件包
为IAutoExtensibleForm中的对象小部件提供ISubformFactory
实现简单Plone风格的输入和显示小部件对象字段
引入了重构的KSS-validation集成,支持对象字段
修改了plone.z3cform的控件遍历以支持对象控件
覆盖了ObjectWidget的默认DataConverter,使用一个不那么侵入性的
提供一个简单的抽象工厂类,将对象字段的值存储为SimpleItem属性。
请注意,此软件包依赖于plone.app.z3cform和它注册的IPloneFormLayer。
使用示例
首先,我们定义一个简单的模式,我们希望将其作为其他模式的一部分重复使用
from zope import schema from zope.interface import invariant, Invalid from plone.directives import form from zope.i18nmessageid import MessageFactory as ZopeMessageFactory _= ZopeMessageFactory("my.package") class StartBeforeEnd(Invalid): __doc__ = _(u"The start or end date is invalid") class IPeriod(form.Schema): start = schema.Date( title=_(u"period_start_label", default=u"Period began"), required=True ) end = schema.Date( title=_(u"period_end_label", default=u"Period ended"), required=True ) @invariant def validateStartEnd(data): if data.start is not None and data.end is not None: if data.start > data.end: raise StartBeforeEnd(\ _(u"The start date must be before the end date."))
然后定义主模式,它重复使用第一个模式
class IWorkPeriod(form.Schema): title = schema.TextLine( title=_(u"work_title_label", default=u"Title"), required=True ) description = schema.TextLine( title=_(u"work_description_label", default=u"Description"), required=False ) period = schema.Object( title=_(u"work_period", default=u"Period"), schema=IPeriod, required=True )
最后,我们注册一个对象因子,该因子创建与我们的模式匹配的SimpleItem,以便z3c.form可以对其进行验证并将其存储为正在创建或编辑的实际对象的属性
from five import grok from zope.interface import Interface from z3c.form.interfaces import IObjectFactory from jyu.formwidget.object.factory import AbstractBaseFactory from my.package.schemas import IPeriod class PeriodFactory(AbstractBaseFactory, grok.MultiAdapter): grok.provides(IObjectFactory) grok.name("my.package.schemas.IPeriod") grok.adapts(Interface, Interface, Interface, Interface)
要能够测试此功能,您当然还应该根据主模式定义并注册一个新的内容类型(例如,使用Dexterity)。
变更日志
1.0b7 - 2011-02-23
修复datamanager以默认将其作为属性字段工作,以解决与 plone.app.textfield 和 plone.namedfile 的问题。
1.0b6 - 2011-01-28
增加了关于 plone.app.textfield 和 plone.namedfile 问题的说明。
1.0b5 - 2011-01-24
将来自 plone.app.z3form (0.5.3) 的更改合并到 validation.py。
1.0b4 - 2011-01-19
修复datamanager,使其在对象属性已存在且为None时也能初始化。
1.0b3 - 2011-01-07
修复了验证器和数据转换器以处理子对象字段。
1.0b2 - 2010-12-23
重构以使用自定义 DataConverter 和 DataManager,而不是使用随 z3c.form 一起提供的。
1.0b1 - 2010-12-20
初始发布
项目详情
jyu.formwidget.object-1.0b7.zip 的散列
算法 | 散列摘要 | |
---|---|---|
SHA256 | cf80d3b420df13c4c44ee6c5406678508197e1c6ca3655f0a3077414a7d118cf |
|
MD5 | 13d420042368c4c910c1d517533c9d26 |
|
BLAKE2b-256 | 821bb0a80881892beffdceaffd31617988ef9bea16e6e203e23fb75a2a10fd49 |