|
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 pyramid.events import subscriber |
|
14 from z3c.form import field |
|
15 from z3c.form.browser.checkbox import SingleCheckBoxFieldWidget |
|
16 from z3c.form.interfaces import IDataExtractedEvent |
|
17 from zope.interface import Invalid |
|
18 |
|
19 from pyams_content.shared.common.zmi.manager import SharedToolPropertiesEditForm |
|
20 from pyams_content.shared.form.interfaces import IFormsManager |
|
21 from pyams_form.group import NamedWidgetsGroup |
|
22 from pyams_form.interfaces.form import IInnerSubForm |
|
23 from pyams_skin.layer import IPyAMSLayer |
|
24 from pyams_utils.adapter import adapter_config |
|
25 from pyams_zmi.form import InnerAdminEditForm |
|
26 |
|
27 |
|
28 __docformat__ = 'restructuredtext' |
|
29 |
|
30 from pyams_content import _ |
|
31 |
|
32 |
|
33 @adapter_config(name='captcha-settings', |
|
34 context=(IFormsManager, IPyAMSLayer, SharedToolPropertiesEditForm), |
|
35 provides=IInnerSubForm) |
|
36 class FormManagerCaptchaSettingsEditForm(InnerAdminEditForm): |
|
37 """Form manager captcha settings edit form""" |
|
38 |
|
39 prefix = 'captcha_properties.' |
|
40 |
|
41 legend = _("Captcha settings") |
|
42 fieldset_class = 'bordered no-x-margin margin-y-10' |
|
43 |
|
44 fields = field.Fields(IFormsManager).select('use_captcha', |
|
45 'default_captcha_client_key', |
|
46 'default_captcha_server_key', |
|
47 'use_proxy', |
|
48 'proxy_proto', |
|
49 'proxy_host', |
|
50 'proxy_port', |
|
51 'proxy_username', |
|
52 'proxy_password', |
|
53 'proxy_only_from') |
|
54 fields['use_captcha'].widgetFactory = SingleCheckBoxFieldWidget |
|
55 fields['use_proxy'].widgetFactory = SingleCheckBoxFieldWidget |
|
56 |
|
57 weight = 1 |
|
58 |
|
59 def updateGroups(self): |
|
60 self.add_group(NamedWidgetsGroup(self, 'captcha', self.widgets, |
|
61 ('use_captcha', 'default_captcha_client_key', |
|
62 'default_captcha_server_key'), |
|
63 fieldset_class='inner bordered', |
|
64 legend=_("Use captcha"), |
|
65 css_class='inner', |
|
66 switch=True, |
|
67 checkbox_switch=True, |
|
68 checkbox_field=IFormsManager['use_captcha'])) |
|
69 self.add_group(NamedWidgetsGroup(self, 'proxy', self.widgets, |
|
70 ('use_proxy', 'proxy_proto', 'proxy_host', |
|
71 'proxy_port', 'proxy_username', 'proxy_password', |
|
72 'proxy_only_from'), |
|
73 fieldset_class='inner bordered', |
|
74 legend=_("Use proxy server"), |
|
75 css_class='inner', |
|
76 switch=True, |
|
77 checkbox_switch=True, |
|
78 checkbox_field=IFormsManager['use_proxy'])) |
|
79 super(FormManagerCaptchaSettingsEditForm, self).updateGroups() |
|
80 |
|
81 |
|
82 @subscriber(IDataExtractedEvent, form_selector=FormManagerCaptchaSettingsEditForm) |
|
83 def check_form_captcha_data(event): |
|
84 """Check captcha form input data""" |
|
85 data = event.data |
|
86 if data.get('use_captcha') and not (data.get('default_captcha_client_key') and |
|
87 data.get('default_captcha_server_key')): |
|
88 event.form.widgets.errors += (Invalid(_("You must define client and server key to " |
|
89 "activate a captcha")), ) |
|
90 if data.get('use_proxy') and not data.get('proxy_host'): |
|
91 event.form.widgets.errors += (Invalid(_("You must define hostname to use a proxy")), ) |