fix _getBrainByValue to check if value is traversable

first so we can provide the correct token.
This commit is contained in:
Nathan Van Gheem 2011-10-03 20:36:34 -05:00
parent d1dff47768
commit 9c97e00245
3 changed files with 75 additions and 3 deletions

View File

@ -12,3 +12,65 @@ for collection fields (e.g. List, Tuple) with a value_type of Choice.
When using this widget, the vocabulary/source has to provide the IQuerySource
interface from z3c.formwidget.query and have a search() method.
Example Usage::
from zope.component import adapts
from zope.interface import Interface, implements
from zope import schema
from plone.z3cform import layout
from z3c.form import form, button, field
from plone.formwidget.contenttree import ContentTreeFieldWidget
from plone.formwidget.contenttree import MultiContentTreeFieldWidget
from plone.formwidget.contenttree import PathSourceBinder
class ITestForm(Interface):
buddy = schema.Choice(title=u"Buddy object",
description=u"Select one, please",
source=PathSourceBinder(portal_type='Document'))
friends = schema.List(
title=u"Friend objects",
description=u"Select as many as you want",
value_type=schema.Choice(
title=u"Selection",
source=PathSourceBinder(portal_type='Document')))
class TestAdapter(object):
implements(ITestForm)
adapts(Interface)
def __init__(self, context):
self.context = context
def _get_buddy(self):
return None
def _set_buddy(self, value):
print "setting", value
buddy = property(_get_buddy, _set_buddy)
def _get_friends(self):
return []
def _set_friends(self, value):
print "setting", value
friends = property(_get_friends, _set_friends)
class TestForm(form.Form):
fields = field.Fields(ITestForm)
fields['buddy'].widgetFactory = ContentTreeFieldWidget
fields['friends'].widgetFactory = MultiContentTreeFieldWidget
# To check display mode still works, uncomment this and hit refresh.
#mode = 'display'
@button.buttonAndHandler(u'Ok')
def handle_ok(self, action):
data, errors = self.extractData()
print data, errors
TestView = layout.wrap_form(TestForm)

View File

@ -4,6 +4,9 @@ Changelog
1.0.4 (unreleased)
------------------
* fix _getBrainByValue to check if value is traversable
first so we can provide the correct token.
[vangheem]
1.0.3 (2011-09-24)
------------------

View File

@ -25,6 +25,8 @@ from Products.ZCTextIndex.ParseTree import ParseError
from plone.formwidget.contenttree.interfaces import IContentSource
from plone.formwidget.contenttree.interfaces import IContentFilter
from OFS.interfaces import ITraversable
logger = logging.getLogger(__name__)
@ -104,7 +106,7 @@ class PathSource(object):
def __contains__(self, value):
try:
brain = self._getBrainByValue(value)
# If brain was not found, assume item is still good. This seems
# If brain was not found, assume item is still good. This seems
# somewhat nonsensical, but:-
# (a) If an item is invisible to the current editor, they should
# be able to keep the item there.
@ -173,15 +175,20 @@ class PathSource(object):
return self.catalog._catalog[rid]
def _getBrainByValue(self, value):
return self._getBrainByToken(self.portal_path + value)
if ITraversable.providedBy(value):
token = '/'.join(value.getPhysicalPath())
else:
token = self.portal_path + value
return self._getBrainByToken(token)
# Generate a term to persist the value, even when we can't resolve the
# brain. These will get hidden in the display templates
def _placeholderTerm(self, value):
return SimpleTerm(str(value),
token='#error-missing-'+value,
token='#error-missing-' + value,
title=u"Hidden or missing item '%s'" % value)
class ObjPathSource(PathSource):
def _getBrainByValue(self, value):