Skip to content

Commit

Permalink
Merge pull request #65 from plone/fix-64
Browse files Browse the repository at this point in the history
fixes #64: SingleCheckBoxFieldWidget does not render value in view mode
  • Loading branch information
agitator authored Jun 13, 2017
2 parents 1f9e10a + e01d429 commit 87bbebe
Show file tree
Hide file tree
Showing 12 changed files with 295 additions and 17 deletions.
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ Bug fixes:
- Related Items Widget: In search mode, when no basePath was set, search site-wide.
Fixes: https://github.com/plone/mockup/issues/769
[thet]
- Fixes #64: SingleCheckBoxFieldWidget does not render value in view mode.
In order to fix this issue the hacky view was removed.
It is replaced by a new widget to render a single checkbox with bool values.
An appropiate data converter was added as well.
[jensens]


3.0 (2017-03-28)
Expand Down
36 changes: 27 additions & 9 deletions plone/app/z3cform/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,6 @@
class=".templates.RenderContentProvider"
permission="zope.Public"
/>
<browser:page
name="ploneform-render-widget"
for="z3c.form.interfaces.ISingleCheckBoxWidget"
layer="plone.app.z3cform.interfaces.IPloneFormLayer"
class=".templates.RenderSingleCheckboxWidget"
permission="zope.Public"
/>

<adapter factory=".templates.layout_factory" />
<adapter factory=".templates.form_factory" />
Expand Down Expand Up @@ -99,9 +92,34 @@
/>

<adapter
factory="z3c.form.browser.checkbox.SingleCheckBoxFieldWidget"
factory=".widget.SingleCheckBoxBoolFieldWidget"
for="zope.schema.interfaces.IBool
plone.app.z3cform.interfaces.IPloneFormLayer"
.interfaces.IPloneFormLayer"
/>
<adapter
factory=".converters.BoolSingleCheckboxDataConverter"
/>

<z3c:widgetTemplate
mode="display"
field="zope.schema.interfaces.IBool"
widget=".interfaces.ISingleCheckBoxBoolWidget"
layer=".interfaces.IPloneFormLayer"
template="templates/singlecheckboxbool_display.pt"
/>
<z3c:widgetTemplate
mode="input"
field="zope.schema.interfaces.IBool"
widget=".interfaces.ISingleCheckBoxBoolWidget"
layer=".interfaces.IPloneFormLayer"
template="templates/singlecheckboxbool_input.pt"
/>
<z3c:widgetTemplate
mode="hidden"
field="zope.schema.interfaces.IBool"
widget=".interfaces.ISingleCheckBoxBoolWidget"
layer=".interfaces.IPloneFormLayer"
template="templates/singlecheckboxbool_hidden.pt"
/>

<z3c:widgetTemplate
Expand Down
22 changes: 21 additions & 1 deletion plone/app/z3cform/converters.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# -*- coding: utf-8 -*-
from datetime import date
from datetime import datetime
from plone.app.z3cform import utils
from plone.app.z3cform.interfaces import IAjaxSelectWidget
from plone.app.z3cform.interfaces import IDatetimeWidget
from plone.app.z3cform.interfaces import IDateWidget
from plone.app.z3cform.interfaces import ILinkWidget
from plone.app.z3cform.interfaces import IQueryStringWidget
from plone.app.z3cform.interfaces import IRelatedItemsWidget
from plone.app.z3cform.interfaces import ISelectWidget
from plone.app.z3cform import utils
from plone.app.z3cform.interfaces import ISingleCheckBoxBoolWidget
from plone.uuid.interfaces import IUUID
from Products.CMFCore.utils import getToolByName
from Products.CMFPlone.utils import safe_callable
Expand All @@ -19,6 +20,7 @@
from z3c.relationfield.interfaces import IRelationList
from zope.component import adapter
from zope.component.hooks import getSite
from zope.schema.interfaces import IBool
from zope.schema.interfaces import ICollection
from zope.schema.interfaces import IDate
from zope.schema.interfaces import IDatetime
Expand Down Expand Up @@ -351,3 +353,21 @@ def toWidgetValue(self, value):
else:
result['external'] = value
return result


@adapter(IBool, ISingleCheckBoxBoolWidget)
class BoolSingleCheckboxDataConverter(BaseDataConverter):
"""Special converter between boolean fields and single checkbox widgets.
"""

def toWidgetValue(self, value):
"""Convert from Python bool to token sequence representation."""
if value:
return ['selected']
return ['unselected']

def toFieldValue(self, value):
"""See interfaces.IDataConverter"""
# consider all different from true as false,
# this way it works with one checkbox
return bool(value and value[0] == 'selected')
8 changes: 6 additions & 2 deletions plone/app/z3cform/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
from plone.app.textfield.widget import IRichTextWidget as patextfield_IRichTextWidget # noqa
from z3c.form.interfaces import ISelectWidget as IBaseSelectWidget
from z3c.form.interfaces import IFormLayer
from z3c.form.interfaces import ISingleCheckBoxWidget
from z3c.form.interfaces import ITextWidget
from zope.schema.interfaces import IDate
from zope.schema.interfaces import IDatetime


class IPloneFormLayer(IFormLayer):
"""Request layer installed via browserlayer.xml
"""
"""Request layer installed via browserlayer.xml"""


class IDateField(IDate):
Expand Down Expand Up @@ -50,3 +50,7 @@ class IRichTextWidget(patextfield_IRichTextWidget):

class ILinkWidget(ITextWidget):
"""Marker interface for the enhanced link widget."""


class ISingleCheckBoxBoolWidget(ISingleCheckBoxWidget):
"""Marker interface for the SingleCheckboxBoolWidget."""
2 changes: 2 additions & 0 deletions plone/app/z3cform/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import plone.z3cform.interfaces
import plone.z3cform.templates
import z3c.form.interfaces
import zope.deprecation


def path(filepart):
Expand Down Expand Up @@ -47,6 +48,7 @@ class RenderWidget(ViewMixinForTemplates, BrowserView):
index = ViewPageTemplateFile('templates/widget.pt')


@zope.deprecation.deprecate('No longer used, see widget.py for new solution')
class RenderSingleCheckboxWidget(ViewMixinForTemplates, BrowserView):
index = ViewPageTemplateFile('templates/singlecheckbox.pt')

Expand Down
2 changes: 1 addition & 1 deletion plone/app/z3cform/templates/singlecheckbox.pt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<!-- DEPRECATED -->
<div
metal:define-macro="widget-wrapper"
tal:define="widget nocall:context;
Expand All @@ -9,7 +10,6 @@
tal:attributes="class string:field pat-inlinevalidation ${fieldname_class}${error_class};
data-fieldname widget/name;
id string:formfield-${widget/id};">

<input type="text" tal:replace="structure widget/render"
metal:define-slot="widget" />

Expand Down
21 changes: 21 additions & 0 deletions plone/app/z3cform/templates/singlecheckboxbool_display.pt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:tal="http://xml.zope.org/namespaces/tal"
tal:omit-tag="">
<span tal:attributes="id view/id;
class view/klass;
style view/style;
title view/title;
lang view/lang;
onclick view/onclick;
ondblclick view/ondblclick;
onmousedown view/onmousedown;
onmouseup view/onmouseup;
onmouseover view/onmouseover;
onmousemove view/onmousemove;
onmouseout view/onmouseout;
onkeypress view/onkeypress;
onkeydown view/onkeydown;
onkeyup view/onkeyup">
<span tal:content="python:view.displayValue[0] if view.displayValue else ''" />
</span>
</html>
36 changes: 36 additions & 0 deletions plone/app/z3cform/templates/singlecheckboxbool_hidden.pt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:tal="http://xml.zope.org/namespaces/tal"
tal:omit-tag="">
<span class="option"
tal:repeat="item view/items">
<input id="" name="" value="" class="hidden-widget" title=""
tabindex="" accesskey=""
type="hidden"
tal:attributes="id item/id;
name item/name;
class view/klass;
value item/value;
style view/style;
title view/title;
lang view/lang;
onclick view/onclick;
ondblclick view/ondblclick;
onmousedown view/onmousedown;
onmouseup view/onmouseup;
onmouseover view/onmouseover;
onmousemove view/onmousemove;
onmouseout view/onmouseout;
onkeypress view/onkeypress;
onkeydown view/onkeydown;
onkeyup view/onkeyup;
disabled view/disabled;
tabindex view/tabindex;
onfocus view/onfocus;
onblur view/onblur;
onchange view/onchange;
readonly view/readonly;
alt view/alt;
accesskey view/accesskey;
onselect view/onselect" />
</span>
</html>
86 changes: 86 additions & 0 deletions plone/app/z3cform/templates/singlecheckboxbool_input.pt
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:tal="http://xml.zope.org/namespaces/tal"
tal:omit-tag=""
tal:define="items view/items;
items python:list(items);
single_checkbox python:len(items) == 1">
<span tal:attributes="id view/id"
tal:omit-tag="single_checkbox"
tal:condition="python:len(items) > 0">
<span class="option"
tal:repeat="item items"
tal:attributes="id python:single_checkbox and view.id or None">
<input type="checkbox" id="" name="" class="" alt="" title=""
tabindex="" disabled="" readonly="" accesskey="" value=""
checked="checked"
tal:condition="item/checked"
tal:attributes="id item/id;
name item/name;
class view/klass;
value item/value;
style view/style;
title view/title;
lang view/lang;
onclick view/onclick;
ondblclick view/ondblclick;
onmousedown view/onmousedown;
onmouseup view/onmouseup;
onmouseover view/onmouseover;
onmousemove view/onmousemove;
onmouseout view/onmouseout;
onkeypress view/onkeypress;
onkeydown view/onkeydown;
onkeyup view/onkeyup;
disabled view/disabled;
tabindex view/tabindex;
onfocus view/onfocus;
onblur view/onblur;
onchange view/onchange;
readonly view/readonly;
alt view/alt;
accesskey view/accesskey;
onselect view/onselect"
/><input id="" name="" class="" alt="" title="" tabindex=""
disabled="" readonly="" accesskey="" value=""
type="checkbox"
tal:condition="not:item/checked"
tal:attributes="id item/id;
name item/name;
class view/klass;
value item/value;
style view/style;
title view/title;
lang view/lang;
onclick view/onclick;
ondblclick view/ondblclick;
onmousedown view/onmousedown;
onmouseup view/onmouseup;
onmouseover view/onmouseover;
onmousemove view/onmousemove;
onmouseout view/onmouseout;
onkeypress view/onkeypress;
onkeydown view/onkeydown;
onkeyup view/onkeyup;
disabled view/disabled;
tabindex view/tabindex;
onfocus view/onfocus;
onblur view/onblur;
onchange view/onchange;
readonly view/readonly;
alt view/alt;
accesskey view/accesskey;
onselect view/onselect" />
<label for=""
tal:attributes="for item/id">
<span class="label" tal:content="item/label">Label</span>
<span class="required horizontal"
title="Required"
tal:condition="item/required"
i18n:attributes="title title_required;">&nbsp;</span>
<span class="formHelp" tal:content="item/description">Description</span>
</label>
</span>
</span>
<input name="field-empty-marker" type="hidden" value="1"
tal:attributes="name string:${view/name}-empty-marker" />
</html>
6 changes: 3 additions & 3 deletions plone/app/z3cform/templates/widget.pt
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
id string:formfield-${widget/id};">
<label for="" class="horizontal"
tal:attributes="for widget/id"
tal:condition="not:hidden">
tal:condition="python: not hidden and widget.label">
<span i18n:translate="" tal:replace="widget/label">label</span>

<span class="required horizontal" title="Required"
tal:condition="python:widget.required and widget.mode == 'input'"
i18n:attributes="title title_required;">&nbsp;</span>

<span class="formHelp"
tal:define="description widget/field/description"
tal:define="description python: getattr(widget, 'description', widget.field.description)"
i18n:translate=""
tal:content="structure description"
tal:condition="python:description and not hidden"
Expand All @@ -31,7 +31,7 @@
</label>

<div class="fieldErrorBox"
tal:content="structure error/render|nothing">
tal:content="structure error/render|nothing">
Error
</div>

Expand Down
Loading

1 comment on commit 87bbebe

@jenkins-plone-org
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@agitator Jenkins CI reporting about code analysis
See the full report here: http://jenkins.plone.org/job/package-plone.app.z3cform/39/violations

plone/app/z3cform/inline_validation.py:15:1: C901 'InlineValidationView.__call__' is too complex (15)

Follow these instructions to reproduce it locally.

Please sign in to comment.