Skip to main content

Showcase for the new (Plone 4) plone.app.users IUserDataSchema. Shows how to extend the user data fields that can be selected for the registration form.

Project description

Introduction

Since Plone 4, the registration form for new users is a Zope formlib form, defined in plone.app.users. plone.app.users allows the site administrator to select fields from this schema to appear on the registration form.

This product aims to show how you could extend or modify the default schema provided by plone.app.users, and add new fields to the registration form.

How it works

Overriding the default schema

The default schema is defined in plone.app.users, and is provided by a utility. We override this utility in the file overrides.zcml:

<utility
    provides="plone.app.users.userdataschema.IUserDataSchemaProvider"
    factory=".userdataschema.UserDataSchemaProvider"/>

Our userdataschema.py contains:

from plone.app.users.userdataschema import IUserDataSchemaProvider

class UserDataSchemaProvider(object):
    implements(IUserDataSchemaProvider)

    def getSchema(self):
        """
        """
        return IEnhancedUserDataSchema

And, also in userdataschema.py, we subclass the default schema:

from plone.app.users.userdataschema import IUserDataSchema

class IEnhancedUserDataSchema(IUserDataSchema):
    """ Use all the fields from the default user data schema, and add various
    extra fields.
    """

Adding fields to the schema

The “Country” field

We can now add a schema field to our schema class:

class IEnhancedUserDataSchema(IUserDataSchema):
    # ...
    country = schema.TextLine(
        title=_(u'label_country', default=u'Country'),
        description=_(u'help_country',
                      default=u"Fill in which country you live in."),
        required=False,
        )

Various other fields

There are various other extra fields with which you could extend your users’ profile. In userdataschema.py you will find examples for:

  • a Date field (birthdate)

  • a Boolean field (newsletter)

  • a Choice field (gender)

The “Accept Terms” field

A special case is the accept field. This is a Boolean field which is required for signup. We implement it by adding a constraint to the schema:

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,
        )

Because this field can be ignored once registration is complete, we don’t add it to the memberdata properties (see below).

Adding fields to the memberdata properties

In profiles/default/memberdata_properties.xml, we add the fields that we want to store as properties on the member. These are all the fields we defined, except the “accept” field, which is wanted only for signup.

Default settings for registration fields

We can automatically select some fields to go on the registration form. The fields we define in profiles/default/propertiestool.xml will be on the form once the product is installed.

Of course, the site manager can modify this after installation.

Changelog

0.1 (2010-02-28)

  • Initial release

To do

  • Make the custom fields available only when the product is installed through Generic Setup. (Currently, the fields in userdataschema.py are available even when the product is not yet installed.)

  • Translate options for the gender field

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

collective.examples.userdata-0.1.tar.gz (12.7 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page