|
1 # |
|
2 # Copyright (c) 2008-2019 Thierry Florac <tflorac AT ulthar.net> |
|
3 # All Rights Reserved. |
|
4 # |
|
5 # This software is subject to the provisions of the Zope Public License, |
|
6 # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. |
|
7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED |
|
8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS |
|
10 # FOR A PARTICULAR PURPOSE. |
|
11 # |
|
12 |
|
13 from z3c.form.browser.checkbox import SingleCheckBoxWidget |
|
14 from z3c.form.interfaces import IFieldWidget |
|
15 from z3c.form.widget import FieldWidget |
|
16 from zope.schema.interfaces import IBool, IList |
|
17 |
|
18 from pyams_content.shared.form.interfaces import IFormFieldDataConverter |
|
19 from pyams_skin.layer import IPyAMSUserLayer |
|
20 from pyams_utils.adapter import adapter_config, ContextRequestAdapter |
|
21 |
|
22 |
|
23 __docformat__ = 'restructuredtext' |
|
24 |
|
25 from pyams_default_theme import _ |
|
26 |
|
27 |
|
28 @adapter_config(context=(IBool, IPyAMSUserLayer), provides=IFieldWidget) |
|
29 def BooleanFieldWidget(field, request): |
|
30 return FieldWidget(field, SingleCheckBoxWidget(request)) |
|
31 |
|
32 |
|
33 @adapter_config(context=(IBool, IPyAMSUserLayer), provides=IFormFieldDataConverter) |
|
34 class BooleanFieldDataConverter(ContextRequestAdapter): |
|
35 """Boolean field user data converter""" |
|
36 |
|
37 def convert(self, value): |
|
38 return self.request.localizer.translate(_("yes") if value else _("no")) |
|
39 |
|
40 |
|
41 @adapter_config(context=(IList, IPyAMSUserLayer), provides=IFormFieldDataConverter) |
|
42 class ListFieldDataConverter(ContextRequestAdapter): |
|
43 """List field user data converter""" |
|
44 |
|
45 @staticmethod |
|
46 def convert(value): |
|
47 return ', '.join(value) if value is not None else '--' |