2012/02/04

Products.AutocompleteWidget Sample

Products.AutocompleWidget 有支援 Plone 4.x,範例程式碼並不多,因此特別記錄找到的:

測試結果,使用 multiValued=True 後,編輯介面才看得到變化。

"""Definition of the Wiki Doc content type
"""

from zope.interface import implements, directlyProvides

from Products.Archetypes import atapi
from Products.Archetypes.public import LinesField
from Products.Archetypes.public import DisplayList
from Products.ATContentTypes.content import base
from Products.ATContentTypes.content import schemata
from Products.AutocompleteWidget.AutocompleteWidget import AutocompleteWidget
from Products.CMFCore.utils import getToolByName


from tlc.kbase import kbaseMessageFactory as _
from tlc.kbase.interfaces import IWikiDoc
from tlc.kbase.config import PROJECTNAME

WikiDocSchema = schemata.ATContentTypeSchema.copy() + atapi.Schema((

          
     LinesField('tags',
                searchable=1,
                required=0,
                mutator='setTagSubject',
                accessor='getTagSubject',
                edit_accessor='getTagSubject',
                vocabulary='getSubjectVocab',
                widget=AutocompleteWidget(label='Tags'),
                enforceVocabulary=0,
     ),

))

# Set storage on fields copied from ATContentTypeSchema, making sure
# they work well with the python bridge properties.

WikiDocSchema['title'].storage = atapi.AnnotationStorage()
WikiDocSchema['description'].storage = atapi.AnnotationStorage()

schemata.finalizeATCTSchema(WikiDocSchema, moveDiscussion=False)

class WikiDoc(base.ATCTContent):
    """Description of the Example Type"""
    implements(IWikiDoc)

    meta_type = "Wiki Doc"
    schema = WikiDocSchema

    title = atapi.ATFieldProperty('title')
    description = atapi.ATFieldProperty('description')
    
    def setTagSubject(self, value):
        """Set tag widget contents to subject of object"""
        self.getField('subject').set(self, value)
 
    def getTagSubject(self):
        """Set tag widget contents to subject of object"""
        return self.Subject()
 
    def getSubjectVocab(self):
        """Get subject (keywords) vocabulary"""
        catalog = getToolByName(self, 'portal_catalog')
        return catalog.uniqueValuesFor('Subject')

atapi.registerType(WikiDoc, PROJECTNAME)

No comments: