Skip to content
Permalink
Browse files

Added RelationListDictDataManager.

  • Loading branch information...
datakurre committed Dec 19, 2011
1 parent 2f0ce3c commit cd18d5cb2a4dceb37135a441ad6e820ae954cf81
Showing with 48 additions and 1 deletion.
  1. +46 −0 plone/app/relationfield/widget.py
  2. +2 −1 plone/app/relationfield/widget.zcml
@@ -143,6 +143,52 @@ def set(self, value):
super(RelationListDataManager, self).set(new_relationships)


class RelationListDictDataManager(DictionaryField):
"""A data manager which sets a list of relations on dictionary"""
adapts(dict, IRelationList)

def get(self):
"""Gets the target"""
rel_list = []

# Calling query() here will lead to infinite recursion!
try:
rel_list = super(RelationListDictDataManager, self).get()
except AttributeError:
rel_list = None

if not rel_list:
return []

resolved_list = []
for rel in rel_list:
if rel.isBroken():
# XXX: should log or take action here
continue
resolved_list.append(rel.to_object)
return resolved_list

def query(self, default=NO_VALUE):
"""See z3c.form.interfaces.IDataManager"""
try:
return self.get()
except ForbiddenAttribute, e:
raise e
except AttributeError:
return default

def set(self, value):
"""Sets the relationship target"""
value = value or []
new_relationships = []
intids = getUtility(IIntIds)
for item in value:
# otherwise create one
to_id = intids.getId(item)
new_relationships.append(RelationValue(to_id))
super(RelationListDictDataManager, self).set(new_relationships)


# plone.supermodel schema import/export handlers

RelationChoiceHandler = BaseHandler(RelationChoice)
@@ -10,6 +10,7 @@
<adapter factory=".widget.RelationDataManager" />
<adapter factory=".widget.RelationDictDataManager" />
<adapter factory=".widget.RelationListDataManager" />
<adapter factory=".widget.RelationListDictDataManager" />

<utility component=".source.CMFContentSearchSource"
name="plone.formwidget.relations.cmfcontentsearch"
@@ -34,4 +35,4 @@
name="plone.app.relationfield.RelationList"
/>

</configure>
</configure>

0 comments on commit cd18d5c

Please sign in to comment.
You can’t perform that action at this time.