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

Custom widgets for choice fields in the session @@start view #702

Merged
merged 1 commit into from
Feb 9, 2024
Merged
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
3 changes: 3 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Changelog
16.0.0 (unreleased)
-------------------

- Custom widgets for choice fields in the session @@start view
[ale-rt, maurits]

- Fix problem with the identification report not being generated for certain survey sessions.
Ref: scrum-1846
Fixes: #688
Expand Down
33 changes: 33 additions & 0 deletions src/euphorie/client/widgets/choice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from functools import cached_property
from z3c.form.browser.select import SelectWidget
from z3c.form.interfaces import IFieldWidget
from z3c.form.widget import FieldWidget
from zope.interface import implementer


class EuphorieChoiceWidget(SelectWidget):
"""Select widget for z3c.form in the Euphorie context: better display
mode."""

@cached_property
def title_value(self):
if self.name in self.request.form:
token = self.request.form[self.name]
vocabulary = self.field.source(self.context)
try:
return vocabulary.getTermByToken(token).value
except LookupError:
return

return getattr(self.context.session, self.field.__name__, None)

def isSelected(self, term):
return term.title == self.title_value


@implementer(IFieldWidget)
def EuphorieChoiceFieldWidget(field, request):
"""This is identicall to the equivalent in plone.app.z3cform but needs a
customization to use the EuphorieSelectWidget class."""
widget = FieldWidget(field, EuphorieChoiceWidget(request))
return widget
15 changes: 15 additions & 0 deletions src/euphorie/client/widgets/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
euphorie.client.interfaces.IClientSkinLayer"
/>

<adapter
factory=".choice.EuphorieChoiceFieldWidget"
for="zope.schema.interfaces.IChoice
euphorie.client.interfaces.IClientSkinLayer"
/>

<z3c:widgetTemplate
for="euphorie.client.adapters.session_traversal.ITraversedSurveySession"
view="euphorie.client.browser.session.Start"
Expand All @@ -36,4 +42,13 @@
mode="input"
/>

<z3c:widgetTemplate
for="euphorie.client.adapters.session_traversal.ITraversedSurveySession"
view="euphorie.client.browser.session.Start"
widget="z3c.form.interfaces.ISelectWidget"
template="templates/choice_widget.pt"
layer="euphorie.client.interfaces.IClientSkinLayer"
mode="input"
/>

</configure>
40 changes: 40 additions & 0 deletions src/euphorie/client/widgets/templates/choice_widget.pt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:i18n="http://xml.zope.org/namespaces/i18n"
xmlns:tal="http://xml.zope.org/namespaces/tal"
tal:omit-tag=""
i18n:domain="nuplone"
>
<div class="z3cFieldContainer ${view/@@dependencies}">
<label>
<select class="${view/klass}"
id="${view/id}"
multiple="${view/multiple}"
name="${view/name}"
size="${view/size}"
>
<tal:item repeat="item view/items"><option id="${item/id}"
selected="${python:'selected' if selected else None}"
value="${item/value}"
tal:define="
selected item/selected;
"
>${item/content}</option></tal:item>
</select>
<tal:error condition="view/error"
replace="structure view/error/render|nothing"
/>
</label>
<dfn class="infoPanel"
title="Information"
tal:define="
description view/field/description;
"
tal:condition="description"
i18n:attributes="title"
>${description}</dfn>
<input name="${view/name}-empty-marker"
type="hidden"
value="1"
/>
</div>
</html>
Loading