How can I use zope.schema to group portal types into categories?

I need to create a Plone configlet that delivers this kind of structure:

types = {
    'News articles': ['NewsMediaType', 'News Item'], 
    'Images': ['Image'],
    'Pages': ['Page']
}

I made a prototype to show what I was thinking to have in the form:

mockup

So I need to group some portal_types together and let the user assign a name for this group. How can I do that? Any ideas?

edited:

I made a great progress with the problem, but when save the form, validation give me an error

enter image description here

    # -*- coding: utf-8 -*-
from plone.theme.interfaces import IDefaultPloneLayer

from z3c.form import interfaces

from zope import schema
from zope.interface import Interface

from plone.registry.field import PersistentField

class IThemeSpecific(IDefaultPloneLayer):
    """  """

class PersistentObject(PersistentField, schema.Object):
    pass

class IAjaxsearchGroup(Interface):
    """Global akismet settings. This describes records stored in the
    configuration registry and obtainable via plone.registry.
    """
    group_name = schema.TextLine(title=u"Group Name",
                                  description=u"Name for the group",
                                  required=False,
                                  default=u'',)

    group_types = schema.List(title=u"Portal Types",
                    description=u"Portal Types to search in this group",
                    value_type =schema.Choice(
                        title=u"Portal Types",
                        vocabulary=u"plone.app.vocabularies.ReallyUserFriendlyTypes",
                        required=False,
                    ),
                    required=False,)

class IAjaxsearchSettings(Interface):
    """Global akismet settings. This describes records stored in the
    configuration registry and obtainable via plone.registry.
    """
    group_info = schema.Tuple(title=u"Group Info",
                                  description=u"Informations of the group",
                                  value_type=PersistentObject(IAjaxsearchGroup, required=False),
                                  required=False,)

-

from plone.app.registry.browser import controlpanel

from collective.ajaxsearch.interfaces.interfaces import IAjaxsearchSettings
from collective.ajaxsearch.interfaces.interfaces import IAjaxsearchGroup

from z3c.form.object import registerFactoryAdapter

class AjaxsearchSettingsEditForm(controlpanel.RegistryEditForm):

    schema = IAjaxsearchSettings
    label = u"Ajaxsearch settings"
    description = u""""""

    def updateFields(self):
        super(AjaxsearchSettingsEditForm, self).updateFields()


    def updateWidgets(self):
        super(AjaxsearchSettingsEditForm, self).updateWidgets()

class AjaxsearchSettingsControlPanel(controlpanel.ControlPanelFormWrapper):
    form = AjaxsearchSettingsEditForm

Upvotes: 1

Views: 399

Answers (2)

I created the class for factory

class AjaxsearchGroup(object):
    """
    group of config
    """
    zope.interface.implements(IAjaxsearchGroup)

registerFactoryAdapter(IAjaxsearchGroup, AjaxsearchGroup)

To use the settings

# get groups config
registry = queryUtility(IRegistry)
settings = registry.forInterface(IAjaxsearchSettings, check=False)

for config in settings.group_info:
     types[config.group_name] = config.group_types

Thank you a lot!

Upvotes: 1

Martijn Pieters
Martijn Pieters

Reputation: 1123420

That's a CRUD (create-read-update-delete) pattern.

The plone.z3cform package has specific support for just such forms. Define a schema for a types group:

 class IAJAXTypesGroup(interface):
     name = ...
     types = ...

then use a CRUD form:

from plone.z3cform.crud import crud

class AJAXGroupsCRUDForm(crud.CrudForm):
    update_schema = IAJAXTypesGroup

    def get_items(self):
        # return a sequence of (id, IAJAXTypesGroup-implementer) tuples
        return self.context.getGroups()

    def add(self, data):
        # return a new IAJAXTypesGroup implementer; a IObjectCreatedEvent is generated
        # alternatively, raise zope.schema.ValidationError
        id = self.context.createGroup(**data)
        return self.context.getGroup(id)

    def remove(self, (id, item)):
        # Remove this specific entry from your list
        self.context.deleteGroup(id)

Groups need to have an id, items are shown in the order that get_items() returns them.

Upvotes: 1

Related Questions