Adds a keyword widget (similar to to Archetypes.Widget:KeywordWidget) to z3cform.
Project description
Introduction
============
This product adds a Keyword widget (similar to Archetypes.Widget:KeywordWidget)
for plone.z3cform.
How To Use (Doc Tests):
=======================
>>> from z3c.form import testing
>>> testing.setupFormDefaults()
>>> import zope.interface
>>> import zope.schema
>>> from zope.schema.fieldproperty import FieldProperty
Use the Keywords field your field type:
>>> from collective.z3cform.keywordwidget.field import Keywords
>>> class IFoo(zope.interface.Interface):
...
... keywords = Keywords(title=u'Keywords')
>>> class Foo(object):
... zope.interface.implements(IFoo)
... keywords = FieldProperty(IFoo['keywords'])
...
... def __init__(self, keywords):
... self.keywords = keywords
...
... def __repr__(self):
... return '<%s %r>' % (self.__class__.__name__, self.name)
We need to make sure that the keywords property is indexed in portal_catalog.
First, we write the indexer. The indexer is a special adapter that adapts the type of an object
and provides the value of the attribute to be indexed.
>>> from plone.indexer.decorator import indexer
>>> @indexer(IFoo)
... def keywords(obj):
... return IFoo(obj).keywords
We need to register our indexer as a named adapter, where the name corresponds to
the index name. In ZCML, that may be::
<adapter name="keywords" factory=".indexers.keywords" />
For testing purpoese, we will register it directly.
>>> from zope.component import provideAdapter
>>> provideAdapter(keywords, name='keywords')
Now we add a form in which the widget will be rendered:
Specify the KeywordWidget factory ('KeywordFieldWidget') as the field's widgetFactory.
>>> from z3c.form.testing import TestRequest
>>> from z3c.form import form, field
>>> from collective.z3cform.keywordwidget.widget import KeywordFieldWidget
>>> class FooAddForm(form.AddForm):
...
... fields = field.Fields(IFoo)
... fields['keywords'].widgetFactory = KeywordFieldWidget
...
... def create(self, data):
... return Foo(**data)
...
... def add(self, object):
... self.context[object.id] = object
...
... def nextURL(self):
... return 'index.hml'
Create, update and render the form:
>>> root = app
>>> request = TestRequest()
>>> addForm = FooAddForm(root, request)
>>> addForm.update()
>>> print addForm.render()
Changelog
=========
1.1.1 - 2012-02-24
----------------
- fixed broken 1.1 release [petschki]
1.1 (2012-02-13)
----------------
- fixed unicode errors for non-ascii keyword value support [petschki]
- ui improved to keep newly entered categories on form errors [petschki]
1.0.1 (2009-09-29)
------------------
- updated setup.cfg
1.0 (2009-09-29)
----------------
* Initial release
============
This product adds a Keyword widget (similar to Archetypes.Widget:KeywordWidget)
for plone.z3cform.
How To Use (Doc Tests):
=======================
>>> from z3c.form import testing
>>> testing.setupFormDefaults()
>>> import zope.interface
>>> import zope.schema
>>> from zope.schema.fieldproperty import FieldProperty
Use the Keywords field your field type:
>>> from collective.z3cform.keywordwidget.field import Keywords
>>> class IFoo(zope.interface.Interface):
...
... keywords = Keywords(title=u'Keywords')
>>> class Foo(object):
... zope.interface.implements(IFoo)
... keywords = FieldProperty(IFoo['keywords'])
...
... def __init__(self, keywords):
... self.keywords = keywords
...
... def __repr__(self):
... return '<%s %r>' % (self.__class__.__name__, self.name)
We need to make sure that the keywords property is indexed in portal_catalog.
First, we write the indexer. The indexer is a special adapter that adapts the type of an object
and provides the value of the attribute to be indexed.
>>> from plone.indexer.decorator import indexer
>>> @indexer(IFoo)
... def keywords(obj):
... return IFoo(obj).keywords
We need to register our indexer as a named adapter, where the name corresponds to
the index name. In ZCML, that may be::
<adapter name="keywords" factory=".indexers.keywords" />
For testing purpoese, we will register it directly.
>>> from zope.component import provideAdapter
>>> provideAdapter(keywords, name='keywords')
Now we add a form in which the widget will be rendered:
Specify the KeywordWidget factory ('KeywordFieldWidget') as the field's widgetFactory.
>>> from z3c.form.testing import TestRequest
>>> from z3c.form import form, field
>>> from collective.z3cform.keywordwidget.widget import KeywordFieldWidget
>>> class FooAddForm(form.AddForm):
...
... fields = field.Fields(IFoo)
... fields['keywords'].widgetFactory = KeywordFieldWidget
...
... def create(self, data):
... return Foo(**data)
...
... def add(self, object):
... self.context[object.id] = object
...
... def nextURL(self):
... return 'index.hml'
Create, update and render the form:
>>> root = app
>>> request = TestRequest()
>>> addForm = FooAddForm(root, request)
>>> addForm.update()
>>> print addForm.render()
Changelog
=========
1.1.1 - 2012-02-24
----------------
- fixed broken 1.1 release [petschki]
1.1 (2012-02-13)
----------------
- fixed unicode errors for non-ascii keyword value support [petschki]
- ui improved to keep newly entered categories on form errors [petschki]
1.0.1 (2009-09-29)
------------------
- updated setup.cfg
1.0 (2009-09-29)
----------------
* Initial release
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Close
Hashes for collective.z3cform.keywordwidget-1.1.1.zip
Algorithm | Hash digest | |
---|---|---|
SHA256 | b42c80b2bec689ca73e2d2c0bf7f3c9a53f4b2fcf6871abff204fc24e0ee3bb0 |
|
MD5 | 24010e28e6a636abbda0f074c375c95c |
|
BLAKE2b-256 | 9d6c632989643d1ce5e6841c92f28af9d6cc61a98a7c82faf522ba65a8ad20a5 |