Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make relationchoice field works (Plone 5) #70

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/collective/z3cform/datagridfield/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from .interfaces import IDataGridField # noqa # pylint: disable=unused-import
from .interfaces import IRow # noqa # pylint: disable=unused-import
from .row import DictRow # noqa # pylint: disable=unused-import
from .row import RowList # noqa # pylint: disable=unused-import
from zope.i18nmessageid import MessageFactory


Expand Down
1 change: 1 addition & 0 deletions src/collective/z3cform/datagridfield/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<adapter factory=".datagridfield.GridDataConverter" />
<adapter factory=".datagridfield.DataGridFieldSubformAdapter" />
<adapter factory=".datagridfield.DataGridValidator" />
<adapter factory=".row.RowDataManager" />
<adapter
factory=".converters.DictRowSerializer"
zcml:condition="installed transmogrify.dexterity"
Expand Down
8 changes: 7 additions & 1 deletion src/collective/z3cform/datagridfield/datagridfield.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,10 @@ class DataGridFieldObjectSubForm(AutoExtensibleSubForm):
"""
def __init__(self, context, request, parentWidget):
# copied from z3c.form 3.2.10
self.context = context
if context:
self.context = context
else:
self.context = parentWidget.form.context
self.request = request
self.__parent__ = parentWidget
self.parentForm = parentWidget.form
Expand Down Expand Up @@ -427,6 +430,9 @@ def updateWidgets(self):
self.widgets,
self.__parent__.__parent__
)
#Add context to field widgets in sub form
for widget in self.widgets.values():
widget.context = self.context
return rv

def setupFields(self):
Expand Down
6 changes: 6 additions & 0 deletions src/collective/z3cform/datagridfield/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from z3c.form.interfaces import IMultiWidget
from plone.app.z3cform.interfaces import IPloneFormLayer
from zope.schema.interfaces import IList
from zope.schema.interfaces import IObject
from zope.schema.interfaces import ValidationError

Expand Down Expand Up @@ -29,3 +30,8 @@ def __init__(self, fieldname, schema):
class IRow(IObject):
"""A row. The schema defines dict keys.
"""


class IRowList(IList):
"""A List schema field contain the row.
"""
16 changes: 16 additions & 0 deletions src/collective/z3cform/datagridfield/overrides.zcml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<configure
xmlns="http://namespaces.zope.org/zope"
xmlns:browser="http://namespaces.zope.org/browser"
xmlns:zcml="http://namespaces.zope.org/zcml"
>

<configure zcml:condition="have plone-5">
<browser:page
name="getSource"
for="z3c.form.interfaces.IWidget"
class=".patch.SourceView"
permission="zope.Public"
/>
</configure>

</configure>
19 changes: 19 additions & 0 deletions src/collective/z3cform/datagridfield/patch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from plone.app.content.browser.vocabulary import SourceView


class SourceView(SourceView):
""" Patch Plone SourceView's get_context() method
since context of the subform is a dictionary (which is correct)
"""

def get_context(self):
#Get form object
form = self.context.form
try:
parent_form = form.__parent__.form
except:
parent_form = None
if parent_form:
return parent_form.context
else:
return form.context
53 changes: 53 additions & 0 deletions src/collective/z3cform/datagridfield/row.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@
"""
from interfaces import AttributeNotFoundError
from interfaces import IRow
from interfaces import IRowList
from z3c.form.interfaces import NO_VALUE
from z3c.form.interfaces import IDataManager
from z3c.form.datamanager import AttributeField
from zope.interface import Interface
from zope.interface import implementer
from zope.component import adapter
from zope.component import getMultiAdapter
from zope.schema import Field
from zope.schema import getFields
from zope.schema import Object
from zope.schema import List
from zope.schema.interfaces import IChoice
from zope.schema.interfaces import WrongContainedType

Expand Down Expand Up @@ -57,3 +64,49 @@ def _validate(self, value):
def set(self, object, value):
# Override Object field logic
Field.set(self, object, value)


@implementer(IRowList)
class RowList(List):
__doc__ = IRowList.__doc__


@adapter(Interface, IRowList)
class RowDataManager(AttributeField):
"""Proxy to call datamanager for each field in the subform."""

@property
def subfields(self):
return getFields(self.field.value_type.schema)

def get(self):
"""Gets the target"""
raw_value = []
# Calling query() here will lead to infinite recursion!
try:
raw_value = super(RowDataManager, self).get()
except AttributeError:
raw_value = None
if not raw_value:
return []

resolved_value = []
for item in raw_value:
new_item = {}
for name, field in self.subfields.items():
dm = getMultiAdapter((item, field), IDataManager)
new_item[name] = dm.get()
resolved_value.append(new_item)
return resolved_value

def set(self, value):
"""Sets the relationship target"""
value = value or []
new_value = []
for item in value:
new_item = {}
for name, field in self.subfields.items():
dm = getMultiAdapter((new_item, field), IDataManager)
dm.set(item[name])
new_value.append(new_item)
super(RowDataManager, self).set(new_value)