新(plone 4)plone.app.users iuserdataschema的展示。演示如何扩展可为注册窗体选择的用户数据字段。
collective.examples.userdata的Python项目详细描述
简介
使用PLONE 5,注册和个人信息表单是 z3c.form 表单。 这些数据可以进行扩展,以允许在表单上收集任何其他数据。
此产品旨在演示如何扩展或修改默认架构 由 plone.app.users提供,并向注册表添加新字段。
如果您使用的是plone 5之前的plone版本,那么您需要查看 collective.examples.userdata此处
升级用户注意事项
以前,您的 IENHancedUserDataSchema 是通过注册的。一 您在中注册的iuserdataschemaprovider实用程序 组件注册表.xml 。如果这是你做的,那么一定要删除 这个实用程序,最好是在升级之前。 通配符.fixPersistentUtilities 可以帮忙。
添加自定义用户数据字段
下面的代码是来自包的源代码片段。看那儿 请参阅更多示例。
创建模式
我们以与任何其他模式相同的方式为字段创建模式:
class IEnhancedUserDataSchema(model.Schema): country = schema.TextLine( title=_(u'label_country', default=u'Country'), description=_(u'help_country', default=u"Fill in the country you live in."), required=False, )
扩展用户数据窗体
要将此架构添加到表单,我们需要为 用户数据面板 允许我们注册任何想要的新字段:
class UserDataPanelExtender(extensible.FormExtender): adapts(Interface, IDefaultBrowserLayer, UserDataPanel) def update(self): fields = field.Fields(IEnhancedUserDataSchema) fields = fields.omit('accept') # Users have already accepted. self.add(fields)
并在configure.zcml:
<adapter factory=".userdataschema.UserDataPanelExtender" provides="plone.z3cform.fieldsets.interfaces.IFormExtender" />
存储/检索自定义字段
要将值与默认字段一起存储,我们需要将字段添加到 配置文件/default/memberdata\u properties.xml 。例如:
<?xml version="1.0"?> <object name="portal_memberdata" meta_type="Plone Memberdata Tool"> <property name="country" type="string"></property> </object>
我们不在这里定义"accept"字段,因为它只用于注册。 他们必须接受系统中有用户。
在表单可以读取和写入值之前,需要有一个数据 管理器获取值。默认管理器将读取/写入任何字段 在模式中定义,因此大部分工作都是为您完成的:
from plone.app.users.browser.account import AccountPanelSchemaAdapter class EnhancedUserDataSchemaAdapter(AccountPanelSchemaAdapter): schema = IEnhancedUserDataSchema
如果要执行其他操作,请将该字段的属性添加到 重写默认行为。源代码为 出生日期显示了这一点 字段
最后,在zcml中注册数据管理器:
<adapter provides=".userdataschema.IEnhancedUserDataSchema" for="plone.app.layout.navigation.interfaces.INavigationRoot" factory=".adapter.EnhancedUserDataSchemaAdapter" />
扩展注册表
要扩展注册表,您有两个选择。或者使用 @"成员注册"视图来操作默认字段中的哪一个 可见,或完全控制,您可以注册另一个窗体扩展器。
定义注册字段formextenders
注册baseregistrationform的扩展程序将允许我们添加 在注册表的任何位置的字段。这是用同样的方法做的 如前所述:
class RegistrationPanelExtender(extensible.FormExtender): adapts(Interface, IDefaultBrowserLayer, BaseRegistrationForm) def update(self): fields = field.Fields(IEnhancedUserDataSchema) #NB: Not omitting the accept field this time, we want people to check it self.add(fields)
并在configure.zcml:
<adapter factory=".userdataschema.RegistrationPanelExtender" provides="plone.z3cform.fieldsets.interfaces.IFormExtender" />
数据管理器已附加到架构,因此将与用户共享 数据形式。如果我们使用不同的模式,那么我们必须定义另一个 数据管理器也是。
各种其他现场示例
有很多其他的额外字段可以扩展用户的 轮廓。在 userdataschema.py 中,您将找到以下示例:
- 日期字段( 出生日期 )
- 布尔字段( 新闻稿 )
- 一个选择字段( 性别 )
接受条款字段
特例是 接受 字段。这是一个布尔字段,它是 需要注册。我们通过在模式中添加一个 约束来实现它:
def validateAccept(value): if not value == True: return False return True class IEnhancedUserDataSchema(IUserDataSchema): # ... accept = schema.Bool( title=_(u'label_accept', default=u'Accept terms of use'), description=_(u'help_accept', default=u"Tick this box to indicate that you have found," " read and accepted the terms of use for this site. "), required=True, constraint=validateAccept, )
因为注册完成后可以忽略此字段,所以我们不添加 它指向MemberData属性。我们还将其隐藏在用户数据表单中。