2013-02-14 17 views
1

我需要創建一個Plone configlet會,提供這種結構:如何使用zope.schema將門戶類型分組到類別中?

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 
+1

你真的不應該將你的解決方案添加到你的問題。您可以在下面添加一個答案;這樣你的問題就可以重新用於其他人,而且人們也可以給你答案投票。 – 2013-02-22 13:23:32

+0

謝謝你的提示。我做了另一個回答 http://stackoverflow.com/a/15031254/1935882 :) – 2013-03-07 19:38:56

回答

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 

謝謝你很多!

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()返回它們的順序顯示。

相關問題