如何使用zope.schema将门户类型分组到类别中?

时间:2013-02-14 18:18:44

标签: plone zope

我需要创建一个提供这种结构的Plone configlet:

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

我做了一个原型来展示我想要的形式:

mockup

所以我需要将一些portal_types组合在一起,让用户为该组分配一个名称。我怎样才能做到这一点?有什么想法吗?

编辑:

我在这个问题上取得了很大的进步,但是当保存表单时,验证会给我一个错误

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

2 个答案:

答案 0 :(得分:1)

这是一种CRUD(创建 - 读取 - 更新 - 删除)模式。

plone.z3cform包仅对此类表单提供了特定支持。为类型组定义架构:

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

然后使用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)

群组需要拥有ID,项目按get_items()返回的顺序显示。

答案 1 :(得分:1)

我为工厂

创建了一个类
class AjaxsearchGroup(object):
    """
    group of config
    """
    zope.interface.implements(IAjaxsearchGroup)

registerFactoryAdapter(IAjaxsearchGroup, AjaxsearchGroup)

使用设置

# 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

非常感谢你!