'Return the list of widgets that comprise a form definition,
including field contents if any.'

def get_widgets(self, user, contents={}, **kwd):

    params = Params(kwd)
    widgets = []
    for (index, field) in enumerate(self.fields):
        field_type = field['type']
        if ('name' in field):
            field_name = field['name']
        else:
            field_name = ('field_%i' % index)
        if (field_name in kwd):
            if (field_type == 'CheckboxField'):
                value = CheckboxField.is_checked(params.get(field_name, False))
            else:
                value = restore_text(params.get(field_name, ''))
        elif contents:
            try:
                value = str(contents[field['name']])
            except:
                if (field_type == 'AddressField'):
                    value = 'none'
                elif (field_type == 'CheckboxField'):
                    value = False
                else:
                    value = ''
        elif (field_type == 'AddressField'):
            value = 'none'
        elif (field_type == 'CheckboxField'):
            value = False
        else:
            value = field.get('default', '')
        field_widget = eval(field_type)(field_name)
        if (field_type in ['TextField', 'PasswordField']):
            field_widget.set_size(40)
            field_widget.value = value
        elif (field_type == 'TextArea'):
            field_widget.set_size(3, 40)
            field_widget.value = value
        elif (field_type in ['AddressField', 'WorkflowField', 'WorkflowMappingField', 'HistoryField']):
            field_widget.user = user
            field_widget.value = value
            field_widget.params = params
        elif (field_type == 'SelectField'):
            for option in field['selectlist']:
                if (option == value):
                    field_widget.add_option(option, option, selected=True)
                else:
                    field_widget.add_option(option, option)
        elif (field_type == 'CheckboxField'):
            field_widget.set_checked(value)
        if (field['required'] == 'required'):
            req = 'Required'
        else:
            req = 'Optional'
        if field['helptext']:
            helptext = ('%s    (%s)' % (field['helptext'], req))
        else:
            helptext = ('(%s)' % req)
        widgets.append(dict(label=field['label'], widget=field_widget, helptext=helptext))
    return widgets
